Trailing backslash

Jan 19, 2011
581
10
Norman, OK
#1
How do I get a trailing backslash from "TREE" or "DIR"? The only setting I could find in OPTIONs was for trailing backslash on command line completion.

Here's my structure.
Code:
[C:\test] $ tree /f

C:\test
├──dir1
│  ├  file1a
│  ├  file1b
│  └  file1c
├──dir2
│  ├  file2a
│  ├  file2b
│  └  file2c
└──dir3
   ├  file3a
   ├  file3b
   └  file3c
And here are my results.
Code:
[C:\test] $ tree /b /f

C:\test
C:\test\dir1
C:\test\dir1\file1a
C:\test\dir1\file1b
C:\test\dir1\file1c
C:\test\dir2
C:\test\dir2\file2a
C:\test\dir2\file2b
C:\test\dir2\file2c
C:\test\dir3
C:\test\dir3\file3a
C:\test\dir3\file3b
C:\test\dir3\file3c

[C:\test] $ dir /b /s
C:\test\dir1
C:\test\dir2
C:\test\dir3
C:\test\dir1\file1a
C:\test\dir1\file1b
C:\test\dir1\file1c
C:\test\dir2\file2a
C:\test\dir2\file2b
C:\test\dir2\file2c
C:\test\dir3\file3a
C:\test\dir3\file3b
C:\test\dir3\file3c
Here's what I'm looking for
Code:
[C:\test] $ tree /b /f /showmebackslashes

C:\test\
C:\test\dir1\
C:\test\dir1\file1a
C:\test\dir1\file1b
C:\test\dir1\file1c
C:\test\dir2\
C:\test\dir2\file2a
C:\test\dir2\file2b
C:\test\dir2\file2c
C:\test\dir3\
C:\test\dir3\file3a
C:\test\dir3\file3b
C:\test\dir3\file3c

[C:\test] $ dir /b /s /showmebackslashes
C:\test\dir1\
C:\test\dir2\
C:\test\dir3\
C:\test\dir1\file1a
C:\test\dir1\file1b
C:\test\dir1\file1c
C:\test\dir2\file2a
C:\test\dir2\file2b
C:\test\dir2\file2c
C:\test\dir3\file3a
C:\test\dir3\file3b
C:\test\dir3\file3c
I don't see an existing command line switch for it on either command. Am I missing a setting somewhere or should I reference this in "Suggestions"?
Code:
[C:\test] $ tree /?
Display a graphical directory tree.

TREE [[/A:[[-][+]rhsdaecjot] /A /B /D /F /H /N[j] /O:[-]adegnrstu /P /S[n] /T[:acw] /Z] dir...
        dir:  Directory to use as the start of the tree
        /A:(ttribute select)    /H(idden directories)
        /A(SCII)                /P(ause)
        /B(are)                 /Sn (subdirectory level)
        /D(escriptions)         /T(ime and date)
        /F(iles)                /Z (size)

[C:\test] $ dir /?
Display information about files and subdirectories.

DIR [/: /124 /A[[:][-][+]rhsdaecjot] /BCDEFGH /HL /I"text" /JKLM /N[defhjlsv] /O[[:][-]adeginorsux] /PQR /Sn /
T[:acw[u]] /U[12] /VWXZ] [file...]
        /: (show streams)       /L(ower case)
        /1 (one column)         /M (suppress footer)
        /2 (two columns)        /N (LFN format)
        /4 (four columns)       /O(rder)
        /A(ttribute select)     /P(ause)
        /B(are)                 /Q (display owner)
        /C(ompression)          /R (disable wrap)
        /D(isable color coding) /S(ubdirectories)
        /E (upper case)         /T (aTtribute) or (Time)
        /F(ull path)            /U (sUmmary information)
        /G (allocated size)     /V(ertical sort)
        /H(ide dots)            /W(ide)
        /I (match descriptions) /X (display short names)
        /J(ustify names)        /Z (use FAT format)
        /K (suppress header)
As a side note. I like seeing the difference of using the BFS vs DFS representation between TREE and DIR, respectively.
 

samintz

Scott Mintz
May 20, 2008
1,295
11
Solon, OH, USA
#3
I know a simple DIR /sb will colorize the output. So directories show up in a different color.
Otherwise, I was going to suggest what Charles just did.
Code:
function addslash=`%@if[isdir %1,%1\,%1]`
pdir /s /(@addslash[*])
 
Jan 19, 2011
581
10
Norman, OK
#6
BTW - what do BFS and DFS stand for?
Computer science theory. Breadth First Search and Depth First Search.
Briefly... given a tree (as in my example), BFS enumerates all of the first level and then traverses downward and repeats until it reaches the bottom before coming back up one level at a time and starting down the next path. DFS goes down as far as it can before repeating it's way back up to the top and going down the next path.

... and will you look at that. I listed them backwards...
given my two level example, TREE would be DFS and DIR would be BFS. Now I'm going to have to create a larger example to test if they really are; having only two levels doesn't really show for sure.

(there's more to it too, whether you use prefix, infix or postfix notation for listing them (yet, the traversal stays the same, only the output order changes), but that's beyond the scope of this tutorial :facepalm: )

Several edits later: I should have said what they stood for and said "go look it up on wikipedia" and provided links to DFS and BFS
 
Last edited:

samintz

Scott Mintz
May 20, 2008
1,295
11
Solon, OH, USA
#7
I pretty much figured it out myself. If you use PDIR and the various sorting options, you can achieve a lot of the same.

The /Ox switch sorts the entire tree at once and puts all the directories first, then all the files. If you pipe the output to SORT, you'll get the DFS pattern.
Code:
pdir /s /(@if[isdir *,*\,*]) | sort
 
#8
"C:\Program Files\JPSoft\TCMD20\SpanishD.dll"
"C:\Program Files\JPSoft\TCMD20\stdvcl40.dll"
"C:\Program Files\JPSoft\TCMD20\styles"\
"C:\Program Files\JPSoft\TCMD20\styles\Office2007.dll"
"C:\Program Files\JPSoft\TCMD20\styles\Office2010.dll"
"C:\Program Files\JPSoft\TCMD20\styles\Office2013.dll"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2012.dll"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2012Dark.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2012Light.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015.dll"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015Blue.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015Dark.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015Light.ini"
"C:\Program Files\JPSoft\TCMD20\styles\Windows7.dll"
"C:\Program Files\JPSoft\TCMD20\takecmd.dll"
"C:\Program Files\JPSoft\TCMD20\tcc.exception.log"

is a snippet of the TCMD20 folder. For the isdir * entries - I'd like those to be

"C:\Program Files\JPSoft\TCMD20\styles\"

as opposed to what is above....
 
Jan 19, 2011
581
10
Norman, OK
#9
So... this is the code to make a directory and file structure 4 deep, 4 wide, with 4 files per folder (340 directories = 4 + 4^2 +4^3 + 4^4 and 1364 total files = 4 + 4^2 + 4^3 + 4^4 + 4^5).
Code:
do a = 1 to 4 ( touch /c file%@char[%@eval[%a%+96]] & do b = 1 to 4 ( touch /c dir%a%\file%a%%@char[%@eval[%b%+96]] & do c = 1 to 4 ( touch  /c dir%a%\dir%a%%b%\file%a%%b%%@char[%@eval[%c%+96]] & do d = 1 to 4 ( touch /c dir%a%\dir%a%%b%\dir%a%%b%%c%\file%a%%b%%c%%@char[%@eval[%d%+96]] & do e = 1 to 4 ( touch /c dir%a%\dir%a%%b%\dir%a%%b%%c%\dir%a%%b%%c%%d%\file%a%%b%%c%%d%%@char[%@eval[%e%+96]]) ) ) ) )
Turns out both DIR and TREE use DFS for traversal. However, it looks like DIR prints all directory contents (dirs and files) upon entering a directory (pre-order) whereas TREE prints dir names upon entering (pre-order) and filenames on the return trip (post-order).

Isn't that interesting? Now to actually try everyone's suggestions. Hopefully nothing else shiny draws my attention in the mean time.
 

samintz

Scott Mintz
May 20, 2008
1,295
11
Solon, OH, USA
#10
"C:\Program Files\JPSoft\TCMD20\SpanishD.dll"
"C:\Program Files\JPSoft\TCMD20\stdvcl40.dll"
"C:\Program Files\JPSoft\TCMD20\styles"\
"C:\Program Files\JPSoft\TCMD20\styles\Office2007.dll"
"C:\Program Files\JPSoft\TCMD20\styles\Office2010.dll"
"C:\Program Files\JPSoft\TCMD20\styles\Office2013.dll"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2012.dll"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2012Dark.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2012Light.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015.dll"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015Blue.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015Dark.ini"
"C:\Program Files\JPSoft\TCMD20\styles\VisualStudio2015Light.ini"
"C:\Program Files\JPSoft\TCMD20\styles\Windows7.dll"
"C:\Program Files\JPSoft\TCMD20\takecmd.dll"
"C:\Program Files\JPSoft\TCMD20\tcc.exception.log"

is a snippet of the TCMD20 folder. For the isdir * entries - I'd like those to be

"C:\Program Files\JPSoft\TCMD20\styles\"

as opposed to what is above....
You can do that by removing the quotes. I was not able to make the @IF version work. But it works correctly when used with a user defined function.
Code:
addslash=%@if[isdir %1,%@unquote[%1]\,%@unquote[%1]]
pdir /s /(@addslash[*])