Windows 10

Batch File To List Filenames in a Specified Folder

In this tutorial, you will learn how to use a batch file to list all filenames in a specified folder using the FOR loop. Batch files are scripts that execute DOS (Disk Operating System) commands to automate tasks.
 
 

🧪 Batch Script to List Filenames in a Specific Folder

The following example lists all files located in the folder C:\Users\StackHowTo\:

@echo off
for %%f in (C:\Users\StackHowTo\*) do @echo %%f

✅ Output:

C:\Users\StackHowTo\file1.txt
C:\Users\StackHowTo\file2.txt
C:\Users\StackHowTo\file3.txt
C:\Users\StackHowTo\image.png
  • %%f — Represents each file in the specified folder.
  • @echo %%f — Prints the full path of each file to the console.

 

🔁 Other Useful Variations

You can adapt the FOR loop depending on what exactly you want to list:

🔹 1. Iterate Through Files in the Current Directory:

for %f in (.\*) do @echo %f

🔹 2. Iterate Through Subdirectories Only in the Current Directory:

for /D %s in (.\*) do @echo %s

🔹 3. Iterate Through Files in Current and All Subdirectories:

for /R %f in (.\*) do @echo %f

🔹 4. Iterate Through Subdirectories Recursively:

for /R /D %s in (.\*) do @echo %s

 

📚 Summary
Task Command
List files in a folder for %%f in (C:\Path\*) do @echo %%f
List files in current dir for %f in (.\*) do @echo %f
List subfolders for /D %s in (.\*) do @echo %s
Recursive files for /R %f in (.\*) do @echo %f
Recursive subfolders for /R /D %s in (.\*) do @echo %s

This method is lightweight, fast, and requires no additional software—just native Windows batch scripting. Perfect for automating folder content audits, backups, or log file indexing!
 

Leave a Reply

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