Windows 10

Batch File To List Folders and Subfolders

In this tutorial, we are going to see how to list folders and subfolders by using For Loop. The batch file contains a series of DOS (Disk Operating System) instructions. It allows triggering the execution of commands found in this file.
 

Example 1: Batch File To List Folders and Subfolders

The following example list folders and subfolders names with full path. /R means list folders recursively:

@echo off

for /d /R %%D in (*) do echo %%~fD

Output:

C:\Users\StackHowTo\Docs
C:\Users\StackHowTo\Images
C:\Users\StackHowTo\Images\Class
C:\Users\StackHowTo\Download

If you want to list just the folders and subfolders names without full path use %%~nxD instead of %%~fD.
 

 

Example 2: Batch File To List Folders and Subfolders

The following example list folder names without full path:

@echo off

for /d /R %%D in (*) do echo %%~nxD

Output:

Docs
Images
Class
Download

 

Leave a Reply

Your email address will not be published. Required fields are marked *