Solved Help improve batch file for search word document.

August 24, 2015 at 12:36:10
Specs: Windows 7
Last time, I was asking for help and thanks for helper help answer regarding to searching batch file.

I want to this link http://www.computing.net/howtos/sho... and tried ran test.

Q1 = When I ran test on this batch file was working great, but it doesn't work the document name with space in middle. How to fix it?

Q2 = How to change the option to open word document instead of save log file.

Q3 = This batch file run all the way to the end drive. Is this possible stop searching file once it found the file then open the file immediate.

Please let me know if you don't understand my question.

I try to get it done before I start work on something else.

Please see script below: Thanks all of you help again.

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


:: ---SCRIPT START
@Echo off
setlocal enabledelayedexpansion
if "%1"=="" (
goto :error
)
if "%1"=="/?" (
goto help
)
set quiet=_
set log=_
set csv=_
set end_arg=
:params
shift
if "%~0"=="" goto :search
if /i "%~0"=="/L" (
set log=1
set searchlog=%time::=%
set searchlog=!searchlog:,=!
set searchlog=!searchlog:.=!
goto :params
)
if /i "%~0"=="/CSV" (
set csv=1
goto :params
)
if /i "%~0"=="/Q" (
set quiet=1
goto :params
)
Echo "%~0" | find /i "/" > nul
if !errorlevel!==1 (
set file=%~0
goto :params
)
:search
if not defined file goto :error
set cnt=0
for /f "tokens=2 delims==" %%Z in ('wmic logicaldisk get name /value') do (
Echo **%%Z**>tmp
for /f %%C in (tmp) do (
set disk=%%C
set disk=!disk:~2,-3!
if not !quiet!==1 (
Echo Searching !disk!\ drive...
)
for /f "tokens=*" %%A in ('dir "!disk!\!file!" /b /s 2^> nul') do (
for /f "tokens=*" %%I in ("%%A") do (
set pDrive=%%~dI
set pPath=%%~pI
set /a cnt+=1
if not !quiet!==1 (
if not !csv!==1 (
Echo Result !cnt!
Echo.
Echo File: %%I
Echo Disk: !pDrive!
Echo Disk location: !pPath!
Echo.
Echo ----------
) else (
Echo !cnt!,%%~dI,%%~pI,%%~nI%%~xI
)
)
if !log!==1 (
if !csv!==1 (
Echo !cnt!,%%~dI,%%~pI,%%~nI%%~xI>> !searchlog!.csv
) else (
Echo !cnt!: %%I >> !searchlog!.log
)
)
)
)
)
del /q tmp > nul
if not !quiet!==1 (
Echo Search complete
Echo.
)
)
Echo.
Echo Found !cnt! hits on !file!
Echo.
if !cnt! GTR 0 (
exit /b 0 & @Echo on
) else (
exit /b 1 & @Echo on
)

:help
Echo.
Echo FILESEARCH [/L] [/CSV] [/Q] ^<"file"^>
Echo.
Echo [/L] Writes the results to a .log file. The file is named after
Echo the time of the search.
Echo.
Echo [/CSV] Outputs results in CSV format for later parsing.
Echo If /L is provided, the CSV strings will be output to
Echo its own .csv file.
Echo.
Echo The CSV consists of the following:
Echo ^,^,^,^
Echo.
Echo [/Q] Enables quiet searching.
Echo.
Echo ^ Specifies the search target.
Echo.
Echo If no file extension is provided, it will search folder names instead of
Echo file names.
Echo.
Echo FILESEARCH modifies the ERRORLEVEL variable.
Echo.
Echo ERRORLEVEL 0: Result found
Echo ERRORLEVEL 1: No result
Echo.
exit /b & @Echo on

:error
Echo.
Echo Syntax error. Use ^ for help.
exit /b & @Echo on
:: ---SCRIPT END


See More: Help improve batch file for search word document.

Report •


#1
August 24, 2015 at 23:58:40
✔ Best Answer
Try this.
Slightly modified version of the file search script in the how to. Script is not tested as I do not have Microsoft Office installed.
Shoulr loop through every disk until result is found. Starts on the C drive.

Replace YOUR FILE HERE with the file you are searching for.
You might also want to change the winword.exe command, like in your previous post.

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

replace winword.exe with !Word!

@Echo off
setlocal enabledelayedexpansion
for /f "tokens=2 delims==" %%Z in ('wmic logicaldisk get name /value') do (
    Echo **%%Z**>tmp
    for /f %%C in (tmp) do (
        set disk=%%C
        set disk=!disk:~2,-3!
        Echo Searching !disk!\ drive...
        del /q tmp > nul
        for /f "tokens=*" %%A in ('dir "!disk!\YOUR FILE HERE" /b /s 2^> nul') do (
            for /f "tokens=*" %%I in ("%%A") do (
                winword.exe "%%~fI"
                goto end
            )
        )
    )
)
:end
exit

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 •

#2
August 26, 2015 at 02:02:02
HI RainBawZ, I had revised the following script and it works great. Thanks for your help.

Two questions:

a. The file that i want to search is in my map drive such as M, N, O and P drive only. So, It won't search the file on C and D drive every single time.

b. If i want to search a PDF type, is this possible work that way too? How can i add this function into the script.

Thanks again.

Have a nice day.

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

@Echo off

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

setlocal enabledelayedexpansion
SET /P CTOID=Enter CTO file name:
for /f "tokens=2 delims==" %%Z in ('wmic logicaldisk get name /value') do (
Echo **%%Z**>tmp
for /f %%C in (tmp) do (
set disk=%%C
set disk=!disk:~2,-3!
Echo Searching !disk!\ drive...
del /q tmp > nul
for /f "tokens=*" %%A in ('dir "!disk!\%CTOID%" /b /s 2^> nul') do (
for /f "tokens=*" %%I in ("%%A") do (
SET "CTOID=%%A" & GOTO FOUND
)
)
)
)

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


:end
exit


Report •
Related Solutions


Ask Question