HELP - How to recurse a directory structure ?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

HELP - How to recurse a directory structure ?

#1 Post by alan_b » 27 Apr 2013 04:46

I need to obtain the Full path and name of all the files that match a chosen pattern that are within an archive that may have 3 (possibly 4) levels of sub-directories.

This is the code which deals with every file that is within a single folder
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\D\"

I need that fixing so that it will recurse through every child of the parent
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\

This is what I have so far for the single folder :-

Code: Select all

@ECHO OFF
SET P=password

CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\D\"
PAUSE
GOTO :EOF

:PATH
ECHO TESTING %1
FOR %%a IN (%1*-00-*.mrimg) DO SET ITEM=%%a

ECHO VERIFYING "%ITEM%"

REM ------------ CODE BELOW NOT RELEVANT TO RECURSION  ------------
GOTO :EOF

SET OOPS=FAILED - A verification error %errorlevel% has occurred - ? is %P% a valid archive password ?
"verify.exe" "%ITEM%" -v --password "%P%" && SET OOPS=OK
ECHO %OOPS%


Regards
Alan

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

Re: HELP - How to recurse a directory structure ?

#2 Post by foxidrive » 27 Apr 2013 08:44

alan_b wrote:I need to obtain the Full path and name of all the files that match a chosen pattern that are within an archive that may have 3 (possibly 4) levels of sub-directories.

This is the code which deals with every file that is within a single folder
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\D\"

I need that fixing so that it will recurse through every child of the parent
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\


Can you explain further without code?

You archive is on a hard drive, yes?
You wish to match a filespec anywhere within the archive and get the entire path?

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: HELP - How to recurse a directory structure ?

#3 Post by alan_b » 27 Apr 2013 10:48

I have many archives in several folders on an external HDD,
and I wish to visit every subdirectory which holds a particular type of file

This identifies every subdirectory that is of interest today
R:\LS-3.3.1>DIR *-00-*.MRIMG /S | FIND "R:\"
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\C_xp
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\H
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\S
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\S - Copy
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\OCZ
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\OCZ\Zap
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\OCZ\_H
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Seven
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\D
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\T
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\T0
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\T1
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5\Boot_CD_images
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5\Initial_Trial_Backups
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5_0_Beta
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5_0_Beta\Broken
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5_Image_Tests
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images\C~50GB
Directory of R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images\Test

After copying the above from the CMD.EXE screen and editing the start of each line I produced this code

Code: Select all

CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\C_xp"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\H"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\S"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\S - Copy"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\OCZ"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\OCZ\Zap"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\OCZ\_H"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Seven"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\D"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\T"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\T0"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Test\T1"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5\Boot_CD_images"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5\Initial_Trial_Backups"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5_0_Beta"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5_0_Beta\Broken"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\v5_Image_Tests"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images\C~50GB"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images\Test"


To automatically process new folders after they are added - without needing any consequent editing,
I wish to replace those 21 specific lines with general purpose magic that is the equivalent of

Code: Select all

CALL :PATH "R:\LS-3.3.1\*\*\*\*


The recursive code that I need should ONLY be concerned with passing each of child sub-folders to the :PATH code.
The ":PATH" code has the responsibility for processing any files that match *-00-*.MRIMG,

Regards
Alan

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: HELP - How to recurse a directory structure ?

#4 Post by aGerman » 27 Apr 2013 10:51

Alan

ain't a FOR /R loop sufficient?

Code: Select all

FOR /R "%~1" %%a IN (*-00-*.mrimg) DO ECHO %%a


Regards
aGerman

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: HELP - How to recurse a directory structure ?

#5 Post by alan_b » 27 Apr 2013 10:58

Thanks, I will give that a try and report back

I am afraid that I have been forgetting some of the techniques I was taught here a couple of years ago.

Regards
Alan

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: HELP - How to recurse a directory structure ?

#6 Post by alan_b » 27 Apr 2013 14:35

Sorry, but no. I was hoping that your single line would recurse and process all of the 21 paths I have manually determined

This is what I copy/pasted from my DOS screen when I ran my batch file T.BAT :
R:\>T
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS
The full path of is too long.
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS
Press any key to continue . . .

This is the start of that script

Code: Select all

@ECHO OFF
CD /D "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\"
CD

FOR /R "%~1" %%a IN (*-00-*.mrimg) DO ECHO %%a

CD
pause
goto :eof
exit

CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\C_xp"
CALL :PATH "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Laptop\149\H"

REM etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc. etc.


Regards
Alan

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

Re: HELP - How to recurse a directory structure ?

#7 Post by foxidrive » 27 Apr 2013 19:09

alan_b wrote:R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS
The full path of is too long.


This is an issue with your archive subdirectory system, Alan.
The problem is that your backups contain very long folder paths and there is a limit to the number of characters in a folder path, and the number of folders, that CMD tools can handle.

One solution might be to create a folder in the root called A and MOVE the entire branch from \Images\Laptop\ and below into the A folder.
This may allow aGerman's command line to work.

Another way to reduce the path is to try this command and if it works then see which paths are causing the problem, and if they can be truncated.

Code: Select all

dir /ad /s /b >dirlist.txt

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: HELP - How to recurse a directory structure ?

#8 Post by alan_b » 28 Apr 2013 02:34

Dirlist.txt has a maximum line length of less than 200 characters
It is 50985 lines in length and has a size of 4.32 MB (4,534,489 bytes) on my 64 bit Windows 7

Not sure about the size but assume the folder length is no problem

Regards
Alan

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

Re: HELP - How to recurse a directory structure ?

#9 Post by foxidrive » 28 Apr 2013 02:58

Was there any error on the console when it ran?

if not then try this instead:

dir /a-d /s /b >filelist.txt


Advice from Microsoft:
The URL path for all files and folders must be 260 characters or less (and no more than 128 characters for any single file or folder name in the URL).

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: HELP - How to recurse a directory structure ?

#10 Post by Endoro » 28 Apr 2013 04:27

The max path length I can create with "md" is 247 characters (XP).
With 3rd party tools I stopped at 500 characters.
The prompt looks like this (every "XXXXXX~1" has 50 characters):

Code: Select all

C:\TEST\XXXXXX~1\XXXXXX~1\XXXXXX~1\XXXXXX~1\XXXXXX~1\XXXXXX~1\XXXXXX~1\XXXXXX~1\XXXXXX~1\XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX>

But the "for /r /d" loop can read only 262 characters.
The "dir /b /s " command shows an error message (path too long) above 262 characters.
But the "rd /s" command can also remove very long filenames (tested with a path length of 508 characters).

alan_b
Expert
Posts: 357
Joined: 04 Oct 2008 09:49

Re: HELP - How to recurse a directory structure ?

#11 Post by alan_b » 28 Apr 2013 10:27

I started this test from R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\
and ran these three commands :-

DIR /AD /S /B > MYLIST-1.TXT
DIR /A-D /S /B > MYLIST-2.TXT
DIR /A /S /B > MYLIST-3.TXT

Results :-

MYLIST-1.TXT
Size 4 KB
46 Lines
113 characters maximum length
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Images\Easeus_ToGo\Disk and Partition backup 8-30-2011 8-12 AM
Questionable characters are double dot extensions for the first folder name, and ~ in this name
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images\C~50GB\Reflect-html

MYLIST-2.TXT
Size 65 KB
681 Lines
166 characters maximum length
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images\Rescue_ISOs\SYSTEM{2d3788cd-8c19-11e0-bccd-001e8c888f33}.TMContainer00000000000000000001.regtrans-ms

MYLIST-3.TXT
Size 68 KB
727 Lines
166 characters maximum length
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\Master Images\Rescue_ISOs\SYSTEM{2d3788cd-8c19-11e0-bccd-001e8c888f33}.TMContainer00000000000000000001.regtrans-ms

When I run the original script the CMD.EXE window shows the same as before - but it is much faster when not running from root of R:\

Code: Select all

R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS>#

R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS>FOR /R "" %a IN (*-00-*.mrimg) DO ECHO %a
The full path of  is too long.

R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS>PAUSE
Press any key to continue . . .

There are two groups of archives so I tested which gave the problem by doing each separately

Code: Select all

FOR /R "Images" %%a IN (*-00-*.mrimg) DO ECHO %%a
FOR /R "Master Images" %%a IN (*-00-*.mrimg) DO ECHO %%a

Head banging moment, they both worked

I tried

Code: Select all

FOR /R "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS" %%a IN (*-00-*.mrimg) DO ECHO %%a

That also worked without problem
I am getting somewhere - don't know where yet :)

New code

Code: Select all

FOR /R "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS" %%a IN (*-00-*.mrimg) DO ECHO %%a
echo full name __ -"R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS"-
echo current name -"%~1"-

Results as before and followed by :-
R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS>echo current name -""-
current name -""-

R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS>PAUSE
Press any key to continue . . .

:) :)

To my script I appended

Code: Select all

echo ------------ -"%~dp0"-0
echo ------------ -"%~dp1"-1
echo -----------  -"%~dp2"-2

Result :-

Code: Select all

full name __ -"R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS"-
current name -""-
------------ -"R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS\"-0
------------ -""-1
-----------  -""-2


Conclusion
I now get the same successful results from either

Code: Select all

FOR /R "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS" %%a IN (*-00-*.mrimg) DO ECHO %%a
FOR /R "%~dp0" %%a IN (*-00-*.mrimg) DO ECHO %%a

:D :D :D
N.B.
"%~dp0" has one more character, a trailing '\', compared with
"R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS"

Supplementary
I get identical results from all of :-

Code: Select all

FOR /R "R:\LS-3.3.1\RecoveredData\_E_GPT_E__465.82 GB_NTFS" %%a IN (*-00-*.mrimg) DO ECHO %%a
FOR /R "%~dp0" %%a IN (*-00-*.mrimg) DO ECHO %%a
FOR /R %%a IN (*-00-*.mrimg) DO ECHO %%a


OBSERVATIONS
The original warning is a CMD.EXE stupidity.
I get the same warning from either of

Code: Select all

FOR /R "" %%a IN (*-00-*.mrimg) DO ECHO %%a
FOR /R "%~1" %%a IN (*-00-*.mrimg) DO ECHO %%a

and yet they is no output and no warning at all for

Code: Select all

FOR /R "Folder name that does not exist" %%a IN (*-00-*.mrimg) DO ECHO %%a


FINALLY
Thanks for getting me to consider "FOR /R ..."
That is an option I never used before or even remembered seeing.

Regards
Alan

Post Reply