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

SignUp Now!

Deepest First

I've got a small alias, "deepestFirst", that I probably should bulletproof before it goes off the rails and kills something!

It's defined as:
deep*estFirst=`for /L %ii in (32,-1,0) do (set ret=%@EXEC[ global /h /i /S+%[ii] /S%[ii] %$ ] )`

Its purpose is to allow executing a command (or group) in every subdirectory from the CWD on downwards, but to do it in the deepest directories first, then proceeding upwards to each parent. There are (usually rare) occasions when you'd like to do that.

I'm hoping one of our superusers, Vince, Steve, Charles, or one of our other fine members can help out here!

Some notes:
  • This alias is an entry in my aliases.tcc file, read in during TcStart.btm.
  • It obviously cheats a little and assumes it will never be run from a directory where the subdirectories are more than 32 deep. This is generally a very safe assumption for any filesystem that I've been maintaining. It doesn't seem to waste much time skipping the "too deep" values.
  • I've got to say, I'm a tiny bit proud of the kludge of using /Sx /S+x to cause GLOBAL to function ONLY at depth 'x'. And a tiny bit embarrassed! :rolleyes:
Now some questions:
  • The @EXEC[] function seems to be necessary to cause %[ii] to be expanded before GLOBAL can interpret its command switches. Otherwise the /S switches aren't interpreted correctly. Is there a better way of doing this, or should I stick with @exec?
  • Would this be better defined as a small BTM file, perhaps using DO instead of FOR? My programmer's "sixth sense" tells me it might be easier to bulletproof that way.
    • Would that be easier to halt via Ctrl+Break in case something goes wrong?
  • Here's the biggest question: what dangers might be lurking here? It's been a while since I've written something as general-purpose as this (in JpSoft-land), and I know there are all sorts of ways that parameter expansion can go wrong. My spider senses are tingling but I can't immediately identify the danger.

- forbin
Last edited:
Well, my mind is boggled.

If needed a quick-and-dirty way to do this, I'd probably redirect the output of DIR /A:D /S /B to a file, then pull lines from the file in reverse order. If I needed it often, or elegant, I'd bite the bullet and write a plugin. But since you've come up with something that works for you, then my compliments!
If needed a quick-and-dirty way to do this, I'd probably redirect the output of DIR /A:D /S /B to a file, then pull lines from the file in reverse order. If I needed it often, or elegant, I'd bite the bullet and write a plugin. But since you've come up with something that works for you, then my compliments!

I'm definitely going to at least rewrite it as a BTM, using the approach you suggested.
I just now mistyped a command using the alias implementation, and it wouldn't break in the middle for me.
Fortunately, it was only a minor thing and no harm done, but it was a warning call: get rid of the alias.

However, I'm lazy and will write the plugin only if I have to. LOL.

Thanks for your feedback!
To get over the 32 subdirs limit why not just do something like this:
dir /a:d /b /s /f c:\ > %temp%\dirlist.txt
:next statement assumes up to 99 levels of subdirs, change 02 as necessary
(for %dirname in (@%temp%\dirlist.txt) echo %@format[02,%@count[\,%dirname]] %dirname%)>%temp%\DirLvls.txt)
:DirLvls.txt has:
:03 C:\Users\Name\My videos
:then just loop the DirLvls.txt to see the max subdir level
:then just loop the same file and counting down for max-level down to 1

Something similar could be done with PDIR - not sure though....
That's pretty good, but it has a slight flaw.
The DIR command will output only the leaf directories.
For example, it would have a line for "C:\Users\Name\My videos" and "C:\Users\Name\Documents" (if those had no nested subdirs), but would not show a line containing only C:\Users\Name (assuming I ran it from C:\Users, for example).

I can see two ways of going.
  • I can either fix the part that generates the lines containing the paths.
  • Or I can add another step that parses the DirLvls.txt file to get the intermediates.
Since the intermediate directories are duplicated many times in the raw output, I think it's probably a better bet to try to fix the DIR command.
DIR /a:d /s /f gets the intermediate directories but picks up "." and "..".
Adding /h makes it lose the intermediate directories.

Funny, I've been using JpSoft products since the days of 4DOS and I never noticed that!

Thinking aloud here, I can easily strip out the dots. But that leaves every directory duplicated, and you don't want to execute the same command twice in a single directory. Crud... I was hoping I could just skip every other line, but looking at the output, I get more than just two:
. . .

So, I'll have to remove duplicates as well as dots.

Sigh, things are NEVER simple as they seem! :eek: :banghead:
[C:\Users\Galloway\Documents]*ver & dir /a:d /b /s /f

TCC  21.01.51 x64   Windows 7 [Version 6.1.7601]
C:\Users\Galloway\Documents\AIM Logs
C:\Users\Galloway\Documents\BHS Reunion Lisa Ginger Vanderberry
C:\Users\Galloway\Documents\Family Tree Maker
C:\Users\Galloway\Documents\My Filehippo Downloads
C:\Users\Galloway\Documents\My Kindle Content
C:\Users\Galloway\Documents\My Received Files
C:\Users\Galloway\Documents\My Scans
C:\Users\Galloway\Documents\My Stationery
C:\Users\Galloway\Documents\Rich Test Format Files
C:\Users\Galloway\Documents\Scanned Documents
C:\Users\Galloway\Documents\Text Files
C:\Users\Galloway\Documents\AIM Logs\kipperjill
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds
C:\Users\Galloway\Documents\Family Tree Maker\Embellishments
C:\Users\Galloway\Documents\Family Tree Maker\Templates
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds\Life Style
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds\Outdoor
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds\Pattern
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds\Scene
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds\Tapestry
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds\Texture
C:\Users\Galloway\Documents\Family Tree Maker\Backgrounds\Tree
C:\Users\Galloway\Documents\Family Tree Maker\Embellishments\Borders
C:\Users\Galloway\Documents\Family Tree Maker\Embellishments\Flourishes
C:\Users\Galloway\Documents\Family Tree Maker\Embellishments\Seasons
C:\Users\Galloway\Documents\Family Tree Maker\Embellishments\Symbols
C:\Users\Galloway\Documents\Family Tree Maker\Embellishments\Words
C:\Users\Galloway\Documents\My Received Files\charlessgalloway3915472136
C:\Users\Galloway\Documents\My Received Files\charlessgalloway3915472136\History
C:\Users\Galloway\Documents\My Scans\2017-02 (Feb)

Oops! Forgot all about the follow-up once I got it working.
Here's the finished product; hope it helps someone when they need it!


  • DeepestFirst.btm
    4.7 KB · Views: 306
  • Gnu_Tac.zip
    18.6 KB · Views: 317