Recursive directory processing in a BAT file with a twist

Richard Schaefer

OK, I apologize ahead of time for a) using an old, crappy technology (BAT files) and b) asking what seems to be a redundant question. I'm limited in the technology I'm allowed to use in this particular case and after looking at dozens of posts on the subject I can't find anything I can adapt to what I need.

I have a directory structure that looks like this: A B C D etc... XYZ more folders

My BAT file is located outside this files system. I need to inspect it starting at level "C" and need to find the "XYZ" directory. The folders between C and XYZ can have variable names depending on the environment in which the files were created. I need to end up with a string that consists of the directory names from C through XYZ (i.e. "C\D\E\F....\XYZ") that I can put into a variable so when my BAT file is completed I can reference the variable and run another command.

I've looked at posts using FIND and FOR but I can't seem to figure out how to a) limit the string to the starting directory (for example when I combine FOR with DIR I get "A\B\C...") and how to stop when I get to "XYZ"...

Any help is greatly appreciated.

dbenham

This should work in most situations:

@echo off
setlocal enableDelayedExpansion

set "root=c:\a\b\c"
set "target=xyz"

for %%R in ("%root%") do for /f "delims=" %%F in (
  'dir /b /s /ad "%root%\%target%"'
) do (
  set "fullPath=%%F"
  set "relpath=!fullPath:%%~dpR=!"
)
echo !relpath!

It can fail if any of your paths contain ! or =. There are solutions for this, but the code is significantly more complicated.

EDIT

Actually, there is a relatively simple solution using FORFILES that should work in all situations. (Assuming your version of Windows has FORFILES)

@echo off
setlocal disableDelayedExpansion

set "root=c:\a\b\c"
set "target=xyz"

for /f "delims=" %%F in (
  'forfiles /p "%root%" /m "%target%" /s /c "cmd /c if @isdir==TRUE echo @relpath"'
) do set "relpath=%%~F"
for %%R in ("%root%") do set "relpath=%%~nxR%relpath:~1%"
echo %relpath%

The only restriction is the code has to change slightly if your result contains poison characters like &. In that case you need to add quotes to the final ECHO statement, or else enable delayed expansion at the end and use echo !relpath!

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

recursive file/directory verification on linux

분류에서Dev

Open corresponding remote directory with bat file

분류에서Dev

Excel VBA How to Execute BAT file in folders in current directory

분류에서Dev

Is a symlink to a directory a file or a directory?

분류에서Dev

Launching a .reg within a .bat file

분류에서Dev

WMIC command not working in .bat file

분류에서Dev

Processing file with VM endpoint

분류에서Dev

file processing in python

분류에서Dev

How to sign a Windows batch (.bat) file?

분류에서Dev

Bat file returns error but works correctly

분류에서Dev

What is wrong with my if parameter syntax of this .bat file?

분류에서Dev

How to start an application Maximized using a .bat file?

분류에서Dev

Text File processing - using java

분류에서Dev

Simple File processing in C++

분류에서Dev

Android timer with a twist

분류에서Dev

bash: No such file or directory

분류에서Dev

Copy file to directory

분류에서Dev

Autoload Error: No such file or directory

분류에서Dev

select returns "No such file or directory"

분류에서Dev

Move every file that is not a directory

분류에서Dev

No such file/directory warning

분류에서Dev

Comparing File Dates in a Directory

분류에서Dev

fatal error no such file or directory

분류에서Dev

Output Directory Contents to File

분류에서Dev

How to launch a .bat file in VB without associating the file with the program that launched it?

분류에서Dev

Text file Side by Side Merge not working via Bat file

분류에서Dev

Replace a middle string in .bat file using AutoHotKey without deleting file

분류에서Dev

boost :: filesystem :: recursive_directory_iterator 필터 포함

분류에서Dev

Does change of a file in a directory not change the time of the directory?

Related 관련 기사

뜨겁다태그

보관