1

I am trying to show just the folders in a directory.

Example C:/Test/Game1.

In Game1 there are folders folder1, folder2, folder3. But there are more folders in folder 1, 2 and 3 which I don't want to show.

I have used -maxdepth 1 but it shows up with the error

Too many parameters - 1
Compo
  • 36,585
  • 5
  • 27
  • 39
Matt
  • 61
  • 1
  • 9
  • Oh, sorry, I just was confused because of the answer... Anyway, what about this: [`tree`](http://ss64.com/nt/tree.html)`"C:\Test\Game1" /A |`[`findstr`](http://ss64.com/nt/findstr.html)`/V /B /R /C:"[| ] "` – aschipfl Mar 04 '19 at 17:35
  • @aschipfl just using the opposite as per my answer would be also fine (removing `/V`). BTW, why don't you merge all of these parameters into `/VBRC:"regex/string"`? – double-beep Mar 04 '19 at 18:00
  • @double-beep, there are several possibilities, I guess, also `/B` could be replaced by `^`... Anyway, the merged parameters is not documented (at least here: `findstr /?`), and it is a bit easier to handle when explaining what the code does (see my answer)... – aschipfl Mar 04 '19 at 18:05

4 Answers4

4

The Windows tree command does unfortunately not support an option for the maximum depth. However, you could filder the output by the findstr command in the following way:

tree "C:\Test\Game1" /A | findstr /V /B /R /C:"[| ]   "

Assuming that the output of this tree command is something like (using ASCII characters (/A) rather than extended ones, because they are easier to handle as they do not depend on the current code page):

Folder PATH listing for volume &&&&
Volume serial number is ####-####
C:\TEST\GAME1
+---folder1
|   +---folder11
|   |   +---folder111
|   |   \---folder112
|   \---folder12
|       +---folder111
|       \---folder112
+---folder2
|   +---folder21
|   \---folder22
\---folder3
    +---folder31
    \---folder32

The findstr command removes (/V) everything that begins (/B) with | or SPACE and is followed by three more SPACEs. These criteria are fulfilled for all lines that show a folder that is deeper then level 1. Hence the output would be something like this:

Folder PATH listing for volume &&&&
Volume serial number is ####-####
C:\TEST\GAME1
+---folder1
+---folder2
\---folder3

To display more levels, simply extend the search expression accordingly; so to go down until level 2, use /C:"[| ] [| ] ".

To hide the header (containing the volume information and the upper-case root path), just append a SPACE and /C:"[^+|\\]" to the command line.

Note that the Windows path separator is \ but not /.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
1

So, what you wanted was to print all directories in a folder in a tree format (ASCII characters of course). Inspired from aschipfl's solution, I came up with the opposite one:

tree /A "C:\Test\Game1" | findstr /BRC:"[^| ] "

which actually echoes lines that don't contain string |.

For a more difficult/complicated solution, I came up with:

@echo off

set "counter=0"

cd /D "C:\Test\Game1"

echo FOLDER PATH listing
for /F "skip=1 tokens=*" %%A IN ('vol') do echo %%A
echo C:.

for /F "delims= eol=" %%B IN ('dir /B /AD') do set /a "counter+=1"
set "_counter=0"

setlocal EnableDelayedExpansion
for /F "delims=" %%C IN ('dir /B /AD') do set /a "_counter+=1" & if not !_counter! EQU %counter% (echo +---%%C) else (echo \---%%C)

pause
exit /b 0

But that is not a good solution at all: it just copies the way tree command works. Better use my first solution.

double-beep
  • 5,031
  • 17
  • 33
  • 41
  • I didn't mention it but I wanted it to work with "Autorun" for the cmd in regedit. Aschipfl's one didn't work for that, but this one does. Awesome! – Matt Mar 04 '19 at 18:03
  • @Matt, are you talking about the `tree`/`findstr` approach that worked for AutoRun? how did you implement it? – aschipfl Mar 04 '19 at 18:12
0
C:\Test\Game1" /A |findstr/V /B /R /C:"[| ] "
double-beep
  • 5,031
  • 17
  • 33
  • 41
Matt
  • 61
  • 1
  • 9
0

Just looking for lines that start with '+-' works on my machine.

tree /A "C:\src" | findstr /BR "[+\\]-"

Of course, there are other commands that can do this without tree.

cmd.exe - DIR /A:D

powershell.exe - Get-ChildItem -Directory -Recurse -Depth 0

lit
  • 14,456
  • 10
  • 65
  • 119