Matt
Matt

Reputation: 71

How to show 1 layer of folders in a directory with -tree?

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

Upvotes: 2

Views: 4852

Answers (4)

lit
lit

Reputation: 16236

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

Upvotes: 1

aschipfl
aschipfl

Reputation: 34909

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 /.

Upvotes: 6

double-beep
double-beep

Reputation: 5504

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.

Upvotes: 1

Matt
Matt

Reputation: 71

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

Upvotes: 0

Related Questions