Solved batch file for search file in all drives

August 18, 2015 at 00:50:45
Specs: Windows 7
Can someone help correct below batch file ? Somehow the script below only search on c drive. It doesn't search on Map Drive. So can someone help correct my batch file below in order to search my local and map drive. Appreciate your help.

1. Ask for enter file name such as docx, doc and xlm...etc then enter.
2. Start search file on C drive (local drive) if not exist, continue search file on map drive (M, N, L and O)
3. if found the document from exact input then auto open the document.
4, if not exist then show error

*****************************************************************************************
@echo off

set "Word=C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"

:start
SETLOCAL EnableDelayedExpansion
SET /P CTOID=Enter CTO file name:
FOR /F %%a IN ('MOUNTVOL^|FINDSTR [A-Z]:\\') do (
FOR /F "tokens=*" %%b in ('DIR /B /S "%%a%CTOID%" 2^>nul') do (
SET "CTOID=%%b" & GOTO FOUND
)
)

ECHO ERROR: File not found.
pause
goto start

:FOUND
ECHO File found in "%CTOID%".
"C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE" "%CTOID%"

ENDLOCAL
PAUSE
goto start


See More: batch file for search file in all drives

Report •


#1
August 18, 2015 at 07:19:03
I'm guessing.. no time to test

instead of:
GOTO FOUND

use call :FOUND

end your :start and your :FOUND
with goto :eof

=====================

M2 Get custom script or take private lessons


Report •

#2
August 18, 2015 at 07:21:34
If you want to do a search in ALL drives, you can check out a recursive file search script at http://www.computing.net/howtos/sho... (It searches through all connected drives.)

If you only want to do the C, M, N, L and O drives, you might just want to define those in a variable or string and use that instead of

MOUNTVOL^|FINDSTR[A-Z]:\\
Maybe like this:
FOR /F "tokens=1-5 delims=," %%a IN ("C,M,N,L,O")
FOR %%a IN (C M N L O)

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.

message edited by RainBawZ


Report •

#3
August 18, 2015 at 08:32:49
✔ Best Answer
RainBawZ: FOR /F "tokens=1-5 delims=," %%a IN ("C,M,N,L,O")

You're probably thinking of

FOR %%a IN (C M N L O)

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
August 18, 2015 at 09:37:31
Yes, that's what I meant, thanks.

99 little bugs in the code,
99 little bugs.
Take one down, patch it around,
129 little bugs in the code.


Report •

#5
August 20, 2015 at 00:35:06
Thanks, the other search script works fine, but it only list out all the document if exist and it doesn't open the file once it found it. However, thanks all of you support.

Report •

#6
August 20, 2015 at 01:49:56
I have revised the syntax that you suggest, but non of them work.

The script below only work on C drive for sure it will open docx once it found it that file exist in C drive.

The key point is how can I correct the script below, because I want to run this batch file for search my document it might located in M, N, L and O drives. If it found that document exist in there then it should auto open the document immediately. it works only on C drive that I tested before. If possible make it work on other drive then my life get easier.

Your great support is greatly appreciated Thanks all of you help again,

********************

@echo off

set "Word=C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE"

:start
SETLOCAL EnableDelayedExpansion
SET /P CTOID=Enter CTO file name:
FOR /F %%a IN ('MOUNTVOL^|FINDSTR [A-Z]:\\') do (
FOR /F "tokens=*" %%b in ('DIR /B /S "%%a%CTOID%" 2^>nul') do (
SET "CTOID=%%b" & GOTO FOUND
)
)

ECHO ERROR: File not found.
pause
goto start

:FOUND
ECHO File found in "%CTOID%".
"C:\Program Files (x86)\Microsoft Office\Office14\WINWORD.EXE" "%CTOID%"

ENDLOCAL
PAUSE
goto start



Report •


Ask Question