1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Deepest First

Discussion in 'T&T - Aliases' started by forbin, Aug 30, 2017.

  1. forbin

    Joined:
    Dec 29, 2009
    Messages:
    24
    Likes Received:
    0
    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:
    Code:
    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.

    Thanks!
    - forbin
     
    #1 forbin, Aug 30, 2017
    Last edited: Sep 16, 2017 at 11:47 AM
  2. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,287
    Likes Received:
    39
    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!
     
    forbin likes this.
  3. forbin

    Joined:
    Dec 29, 2009
    Messages:
    24
    Likes Received:
    0
    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!
     
  4. Charles G

    Joined:
    Apr 2, 2011
    Messages:
    1,015
    Likes Received:
    0
    To get over the 32 subdirs limit why not just do something like this:
    .
    Code:
    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....
     
  5. forbin

    Joined:
    Dec 29, 2009
    Messages:
    24
    Likes Received:
    0
    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.
     
  6. forbin

    Joined:
    Dec 29, 2009
    Messages:
    24
    Likes Received:
    0
    Hmm.
    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:
    Code:
    D:\JTB\Docs\Personal\Planning\..
    D:\JTB\Docs\Personal\Published\.
    D:\JTB\Docs\Personal\Published\..
    D:\JTB\Docs\Personal\Published\CodeInspect
    D:\JTB\Docs\Personal\Published\RealtimePerformance1995
    D:\JTB\Docs\Personal\Published\CodeInspect\.
    D:\JTB\Docs\Personal\Published\CodeInspect\..
    . . .
    So, I'll have to remove duplicates as well as dots.

    Sigh, things are NEVER simple as they seem! :eek: :banghead:
     
  7. forbin

    Joined:
    Dec 29, 2009
    Messages:
    24
    Likes Received:
    0
    Hahaha! But sometimes, they are!

    C:\Dir\Whatever> TREE /b
     
  8. Charles G

    Joined:
    Apr 2, 2011
    Messages:
    1,015
    Likes Received:
    0
    Code:
    [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\CCleaner
    C:\Users\Galloway\Documents\CMD_DOCS
    C:\Users\Galloway\Documents\DatingSites
    C:\Users\Galloway\Documents\Family Tree Maker
    C:\Users\Galloway\Documents\Fax
    C:\Users\Galloway\Documents\FixeHChat
    C:\Users\Galloway\Documents\FL_Mindells
    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\NewPC
    C:\Users\Galloway\Documents\Rich Test Format Files
    C:\Users\Galloway\Documents\Robb
    C:\Users\Galloway\Documents\Scanned Documents
    C:\Users\Galloway\Documents\Text Files
    C:\Users\Galloway\Documents\AIM Logs\kipperjill
    C:\Users\Galloway\Documents\DatingSites\eHarmony
    C:\Users\Galloway\Documents\DatingSites\FetLife
    C:\Users\Galloway\Documents\DatingSites\OkCupid
    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\Fax\Drafts
    C:\Users\Galloway\Documents\Fax\Inbox
    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)
    C:\Users\Galloway\Documents\NewPC\Findfiles
    
    [C:\Users\Galloway\Documents]
     

Share This Page