Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

How to use multi-line DO to loop through first level directory names

Feb
5
0
I'm using TCMDv27 on Windows 10 20H2 and trying to create a multi-line DO loop that loops through all of the first level subdirectory names in a targeted directory that I give it. But I cannot figure out what syntax to use to accomplish this. So assuming the directories involved are

Z:\Audio
+--CD
+--HD
+--MP3
I want to give the loop "Z:\Audio" and have its loop variable contain "CD", "HD", and "MP3" as it loops. The rest of the multi-line loop will do some processing with those subdirectory names.

I've been referring to the helpfile syntax below as the closest syntax I could find that might do this:
DO varname IN [range...] /D"directory" [/I:"text" /S[[+]n] /A:[[-|+]rhsadecijopt /O:[-]acdeginorstuz fileset [(command)]

But it's unclear how to accomplish this. What I'm currently using is

DO F in Music:\Audio\*
IF NOT ISDIR "%F" ITERATE
other processing
ENDDO

Note that Music: is a directory alias for a drive\directory combination.

But I'd like not to have to use the IF NOT ISDIR line because if the targeted directory has many files, the script will spend time iterating past all the files. Is there no way to tell DO just to process directories? I tried adding the /A:d switch but that didn't work.

TIA
David
 
Use it like this:
Code:
$ do d in /a:d * (echo %d)
.RaRecovery
FamilyKeys
SystemManifests
If you specify a path instead of a generic wildcard, e.g. c:\Windows\*, then %d will contain the full path. If you only want the names without the path, then use %@filename[%d] instead of just %d.

Another oddity of DO is when you recurse into subfolders there are no paths.
Code:
[C:\TC27]
$ do d in /a:d /s * (echo %d)
event
library
sdk
styles
.cov
ipch
Release
synctest
x64
event-76870a50
Release
Win32
x64

Each of the subfolders are listed then as filenames only.
 
Thanks Charles! That worked, as I"m sure you're not surprised! But I'm thrilled to finally get it to work. I guess I just don't understand how to read the syntax help in the help file. I tried lots before coming here and nothing worked consistently. So thanks again.

Since we're on the topic, if I were to want to recurse through directories, for example in that same tree above, assuming there are subdirectories below the ones I gave, what would be the proper syntax to do that? I like the /S capability and can see some good uses for it, but haven't gotten it to work.

Thanks
David
 
Use it like this:
Code:
$ do d in /a:d * (echo %d)
.RaRecovery
FamilyKeys
SystemManifests
If you specify a path instead of a generic wildcard, e.g. c:\Windows\*, then %d will contain the full path. If you only want the names without the path, then use %@filename[%d] instead of just %d.

Another oddity of DO is when you recurse into subfolders there are no paths.
Code:
[C:\TC27]
$ do d in /a:d /s * (echo %d)
event
library
sdk
styles
.cov
ipch
Release
synctest
x64
event-76870a50
Release
Win32
x64

Each of the subfolders are listed then as filenames only.
Thanks Scott! Very helpful. You must have been posting this while I was posting a question about exactly this topic - recursing through subdirectories. Have you tried using /Sn as in /S1, /S3, /S0 switches? Are there special requirements to get them to work (quirks)?
 

Similar threads

Back
Top