folder - Iterate Directories with Batch -
the objective display directory name in myfolder. my.exe exists in folder, curiously, without wildcard in ...\desktemp*, "@echo showing subfolders" never displayed, "@echo g working" is. my.exe never found when moved 1 of subfolders.
otoh current code never finds my.exe , never displays "@echo g working" lists each subfolder: "@echo showing subfolders".
the other problem pauses @ end of block never reached.
substituting inner with
cd \users\%username%\desktop /d /r %%g in ("desktemp*") (
gets same result. my.exe isn't found if moved 1 of subfolders of desktemp.
setlocal enabledelayedexpansion set currdrive=c set myfolder= :searchdrives rem block %%b in (c) ( if exist %%b: ( pushd %%b:\ if not defined myfolder ( echo "%cd%" rem displays path batch run from. rem above pushd doesn't change c:\ /f "tokens=*" %%g in ('dir /b /s /a:d "%%b:\users\%username%\desktop\desktemp*" ^| find "\"') ( @echo showing subfolders @echo %%g pause if exist "%%g\my.exe" ( call set myfolder=%%g @echo %%g @echo g working call echo %myfolder% pause goto gotmyfolder ) ) ) ) rem exist drive ) rem drives loop :gotmyfolder cd /d %currdrive%:\ echo %myfolder% cd %myfolder% pause
the above chunk whittled larger code block: ultimate aim folder names"\users\new\desktop\desktemp" variable via prompt. escape characters, delimiters , quotes in nested blocks implemented properly?
the answer escaped poor little brain until cottoned on "dir" , "for /d /r" to. sought in addition of new "for /d" (no /r).
this first (extra) "for /d" determines folder names iterate from. (specifically windows directory run problems >260 filenames.)
this locates my.exe file somewhere in users folder (more precisely in root folder beginning u):
setlocal enabledelayedexpansion set currdrive=c set myfolder= :searchdrives rem block %%b in (c) ( if exist %%b: ( pushd %%b:\ if not defined myfolder ( echo "%cd%" rem displays path batch run from. rem above pushd doesn't change c:\ /d %%z in (u*) ( cd \%%z /d /r %%g in ("*") ( if exist "%%g\my.exe" ( call set myfolder=%%g @echo %%g @echo g working call echo %myfolder% pause goto gotmyfolder ) ) ) ) ) rem exist drive ) rem drives loop :gotmyfolder cd /d %currdrive%:\ echo %myfolder% cd %myfolder% pause
edit:
the source of error spam in comment below command:
insert batch code elevate uac privileges [code][1] tanisdl setlocal enabledelayedexpansion & pushd "%cd%" & cd /d "%~dp0" set currdrive=c
for /f "usebackq delims==" %%g in (dir %currdrive%:\ /a:d /o:g /s /b ^| find /i "mystring"
) (set "foundmystring=%%~pg")
Comments
Post a Comment