Windows 10

Batch File To List Filenames in a Specified Folder

In this tutorial, we are going to see how to list filenames in a specified folder 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.
 

Batch File To List Filenames in a Specified Folder

The following example list all file in the specified folder “C:\Users\StackHowTo\” by using For Loop:
 

 

@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

To iterate through files in current directory use:

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

To iterate through subdirectories in current directory use:

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

To iterate through files in current and all subdirectories use:

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

To iterate through subdirectories in current and all subdirectories use:

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

 

Leave a Reply

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