Page 1 of 1

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

Posted: 15 Apr 2012 05:21
by pstein
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

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

Posted: 15 Apr 2012 06:05
by dbenham
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

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

Posted: 15 Apr 2012 06:20
by foxidrive
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
)

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

Posted: 15 Apr 2012 22:12
by dbenham
@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

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

Posted: 16 Apr 2012 00:29
by foxidrive
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.