windows - Recursive directory processing in a BAT file with a twist -


ok, apologize ahead of time a) using old, crappy technology (bat files) , b) asking seems redundant question. i'm limited in technology i'm allowed use in particular case , after looking @ dozens of posts on subject can't find can adapt need.

i have directory structure looks this: b c d etc... xyz more folders

my bat file located outside files system. need inspect starting @ level "c" , need find "xyz" directory. folders between c , xyz can have variable names depending on environment in files created. need end string consists of directory names c through xyz (i.e. "c\d\e\f....\xyz") can put variable when bat file completed can reference variable , run command.

i've looked @ posts using find , can't seem figure out how a) limit string starting directory (for example when combine dir "a\b\c...") , how stop when "xyz"...

any appreciated.

this should work in situations:

@echo off setlocal enabledelayedexpansion  set "root=c:\a\b\c" set "target=xyz"  %%r in ("%root%") /f "delims=" %%f in (   'dir /b /s /ad "%root%\%target%"' ) (   set "fullpath=%%f"   set "relpath=!fullpath:%%~dpr=!" ) echo !relpath! 

it can fail if of paths contain ! or =. there solutions this, code more complicated.

edit

actually, there relatively simple solution using forfiles should work in situations. (assuming version of windows has forfiles)

@echo off setlocal disabledelayedexpansion  set "root=c:\a\b\c" set "target=xyz"  /f "delims=" %%f in (   'forfiles /p "%root%" /m "%target%" /s /c "cmd /c if @isdir==true echo @relpath"' ) set "relpath=%%~f" %%r in ("%root%") set "relpath=%%~nxr%relpath:~1%" echo %relpath% 

the restriction code has change if result contains poison characters &. in case need add quotes final echo statement, or else enable delayed expansion @ end , use echo !relpath!


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -