Windows 10

Batch File To List Folder Names

In this tutorial, you’ll learn how to list folder names in a directory using a simple Windows batch script. This is especially useful for generating folder reports, automation scripts, or organizing data by directory names.

Batch files are composed of DOS (Disk Operating System) commands that automate tasks in the Windows environment.
 

đź§Ş Example 1: Batch Script to List Folder Names with Full Path

The following script lists all folder names in the current directory with their full paths:

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

âś… Output:

C:\Users\StackHowTo\Docs
C:\Users\StackHowTo\Images
C:\Users\StackHowTo\Download
  • for /d %%D in (*) — Iterates over each directory.
  • %%~fD — Displays the full path of each folder.

 
 

đź§Ş Example 2: Batch Script to List Folder Names Without Full Path

The following script lists all folder names in the current directory without showing the full paths:

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

âś… Output:

Docs
Images
Download

%%~nxD — Displays only the name of the folder (without the full path).
 

đź’ˇ Bonus Tips
  • Want to list folders from a different directory? Replace (*) with a path like “C:\MyFolders\*”
  • Want to export the list to a text file? Add > folders.txt to the end of the echo line.
📚 Summary

Using the for /d loop in batch scripts, you can easily:

  • đź“‚ List folders with or without full paths
  • 🔄 Loop through directories for automation
  • đź“‹ Export folder names for documentation or logs

This is a quick and powerful method to handle folder listings without needing third-party tools or scripts.
 

Leave a Reply

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