Scan whole dir tree and do something which each (sub)folder?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Scan whole dir tree and do something which each (sub)folder?

#1 Post by pstein » 15 Apr 2012 05:21

How can I scan a whole directory tree beginning with top node

D:\test\path to base folder\

and do something with each folder? It must be something like

set basefolder=D:\test\path to base folder\
set targetfolder=D:\target\
for %%a in (recursive("%basefolder%/*")) do (
mkdir "%targetfolder%\%%a"
<do more>)


Look at the pseudo code above: How can I get the RELATIVE path in %aa (without leading path to basefolder)?

The code above should NOT be performed for files (only for folders).

Peter

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Scan whole dir tree and do something which each (sub)fol

#2 Post by dbenham » 15 Apr 2012 06:05

You are going to groan (and smile) when you see how simple it is to reproduce an empty directory tree :D

Code: Select all

xcopy /t /e "D:\test\path to base folder\*"  "D:\target\"
Folder "D:\target\" may or may not already exist - it works either way.


If you want to do other things besides create the directories, then you need a FOR /R /D loop to iterate through each folder in the tree.

Code: Select all

for /d /r "D:\test\path to base folder" %%F in (*) do (
  rem %%F contains the full path of a folder in the tree
  echo %%F
)


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Scan whole dir tree and do something which each (sub)fol

#3 Post by foxidrive » 15 Apr 2012 06:20

dbenham wrote:

Code: Select all

for /d /r "D:\test\path to base folder" %%F in (*) do (
)



Dave, the FOR /D command has issues in some cases when long directory names are used.


It's always safer to use FOR /F as it hasn't demonstrated any flaws in many years of use.
(code below is untested)

Code: Select all

@echo off
for /f "delims=" %%a in ('dir "D:\test\path to base folder" /ad /b /s') do (
echo %%a
echo %%~nxa
)

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Scan whole dir tree and do something which each (sub)fol

#4 Post by dbenham » 15 Apr 2012 22:12

@foxidrive: Any more details on what Windows versions or what specific scenarios have problems :?: What are the problematic results :?:

I haven't personally run into any problem on Vista (32 or 64), but then again, I don't use FOR /D /R very often.

The command is kind of interesting.

This does a breadth first iteration of the directory tree, starting with the children of the root folder:

Code: Select all

for /d /r %%D in (*) do @echo %%D 

This does a depth first iteration of the directory tree, starting with the root folder.

Code: Select all

for /r %%D in (.) do @echo %%~fD

Are you aware of any problems with FOR /R :?:


Dave Benham

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Scan whole dir tree and do something which each (sub)fol

#5 Post by foxidrive » 16 Apr 2012 00:29

dbenham wrote:The command is kind of interesting.

This does a breadth first iteration of the directory tree, starting with the children of the root folder:

Code: Select all

for /d /r %%D in (*) do @echo %%D 




That's curious.

Are you aware of any problems with FOR /R :?:


No, Dave, and I don't have details of the issues. They were discussed in alt.msdos.batch.nt some years ago and the consensus was that any form other than a straight for-in-do and for /F and for /L were to be avoided.

Post Reply