Deadmano
Deadmano

Reputation: 237

Windows Batch File - Display All Folder & Sub-Folder relative paths

I am trying to have a batch file output ONLY the short-name of folders and sub-folders to a file. However, with the example below I am only able to get the fullpath.

DIR /S /B /O:N /A:D > FolderList.txt

Which will output:

X:\Example\Folder 1
X:\Example\Folder 2
X:\Example\Folder 3
X:\Example\Folder 1\Sub-Folder 1
X:\Example\Folder 1\Sub-Folder 2
X:\Example\Folder 2\Sub-Folder 1

When the desired output is:

Folder 1
Folder 2
Folder 3
Folder 1\Sub-Folder 1
Folder 1\Sub-Folder 2
Folder 2\Sub-Folder 1

The issue comes with the /S switch that allows the DIR command to recurse into sub-folders.

Is there a simple way, using only a Windows Batch File, to output a list of folders and sub-folders in the current directory to a text file?

Upvotes: 2

Views: 3614

Answers (5)

user28596176
user28596176

Reputation: 11

I found that the following command code provided a robust list of folders and subfolders and created a text document with the list. Command Prompt - Get-ChildItem -Path "Your file Path Name" -Recurse | Sort-Object Name | Out-File -FilePath "list.txt"

Additional Information: The command you used in PowerShell (dir /s /b /o:gn >list.txt) should work fine for listing files and directories recursively. However, PowerShell provides a more robust command for this purpose using the Get-ChildItem cmdlet. I hope this response is a help. Have a better day!

Upvotes: 1

MC ND
MC ND

Reputation: 70941

Just to include a solution using a recursive function call approach

@echo off
    setlocal enableextensions disabledelayedexpansion

    call :getSubFolderList "%~1"
    goto :eof

:getSubFolderList folder [prefix]
    for /d %%a in ("%~f1.\*") do for %%b in ("%~2%%~nxa") do (
        echo %%~b
        call :getSubFolderList "%%~fa" "%%~b\"
    )
    goto :eof

When called with a starting folder, it will iterate over the list of subfolders, and for each one the subroutine will call itself to handle the descending folders.

The other answers to this question using echo to output the folder path use delayedexpansion, echoing the content of the variable used without any problem caused by the parser. But the code in this answer does not use delayed expansion, and characters as &() in the folder names can be a problem in the echo command.

To avoid this problem, the value to echo is quoted and wrapped inside a for replaceable parameter. That is the reason for the for %%b in the code.

To generate a file with the list of folders, the only change needed is, in the first call command:

call :getSubFolderList "%~1" > fileList.txt

note: the code in the answer has been written to receive the folder to be processed as the first argument to the batch file. Change "%~1" to your needs.

Upvotes: 2

Magoo
Magoo

Reputation: 80193

@ECHO OFF
FOR /f "delims=" %%a IN ('DIR /S /B /O:N /A:D') DO (
 SET "name=%%a"
 SETLOCAL ENABLEDELAYEDEXPANSION
 ECHO(!name:%cd%\=!
 endlocal
)

GOTO :EOF

made a little more confusing because you ask for the "short name" then describe the "relative name".

Obviously, name simply echoed to screen. You're aware of how to redirect.

Upvotes: 2

Compo
Compo

Reputation: 38708

To confirm my comment with regard the duplicate response:

@Echo Off
SetLocal EnableDelayedExpansion
For /L %%A In (1 1 8192) Do If "!__CD__:~%%A,1!" NEq "" Set/A "Len=%%A+1"
SetLocal DisableDelayedExpansion
(For /D /R %%A In (*) Do (
    Set "AbsPath=%%A"
    SetLocal EnableDelayedExpansion
    Echo(!AbsPath:~%Len%!
    EndLocal))>FolderList.txt

Upvotes: 1

Squashman
Squashman

Reputation: 14320

This uses a little trick with the old SUBST command to make the root folder a drive letter. Then you can use the FOR command modifiers to drop the drive letter from the variable on output.

@echo off

SET "folders=X:\Example"

subst B: "%folders%"
B:

(FOR /F "delims=" %%G in ('DIR /S /B /O:N /A:D') do echo %%~pnxG)>"%~dp0folderlist.txt

subst B: /D

Now obviously this does not check to make sure that the drive letter B: is available before using it. Easy enough to add some code to check for a drive letter that is not in use before executing the SUBST command.

Upvotes: 1

Related Questions