Tuesday, May 28, 2019

windows - Syntax for success code of "Where" command

What is the syntax to get a success code when running the "Where" command? I need that to end a loop.



And what is the syntax to get the directory of the found file?



This command:



WHERE /r "C:\Dir\Dir2 FindMe.txt"




correctly shows the file in the command window:



C:\Dir1\Dir2\Dir3\FindMe.txt



But now what do I do?
Please be kind, I am less than a novice.



EDIT:
My question is similar to https://stackoverflow.com/questions/7562506, but in my case the file I am looking for is not in the direct path but up, over, and then down. Taking the example given, and starting at the same directory, my file could be in C:\Temp\Dir1\Dir2\Dir3**\Dir14\Dir15**\FindMe.txt. Meaning I need to go up to Dir3, there find my file with the WHERE command and stop my loop.




EDIT 2: I stripped everything off I could. Make a txt file "FindMe.txt" somewhere, put this code snipped as a .cmd someplace else and then drag another .txt file onto the .cmd file. With any luck it should stop with the directory of FindMe.txt as answer. What I need help with is the pseudo code:



@ECHO OFF

SET "cmdPath=%~dp0"
SET "cmdPath=%cmdPath:~0,-1%" ::without back slash
SET "searchPath=%cmdPath%" ::start search here

:loop

IF EXIST "%searchPath%\FindMe.txt" (
set "txtPath=%searchPath%
ECHO txtPath%searchPath%\FindMe.txt
GOTO :EOF
)
IF "%searchPath:~1%" == ":" (
ECHO FindMe.txt not found.
pause
GOTO :EOF
)

echo searchPath=%searchPath%
rem run: WHERE /r "%searchPath%" /q FindMe.txt
rem if successful (found) (
rem run: WHERE /r "%searchPath%" FindMe.txt
rem just keep the path and rename to txtPath
rem goto :eof
CALL :getPath "%searchPath%"
GOTO loop
:end


:getPath
SET "searchPath=%~dp1"
SET "searchPath=%searchPath:~0,-1%"
:end
:eof

No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...