How to list all PNG images in a sub directory?
Categories:
How to List All PNG Images in a Subdirectory Using a Windows Batch File

Learn to efficiently locate and list all PNG image files within a specified directory and its subdirectories using a simple Windows batch script.
Finding specific file types, especially images, across multiple nested folders can be a common task for Windows users. While graphical interfaces offer search functionalities, a batch file provides a powerful, automatable, and repeatable solution. This article will guide you through creating a simple yet effective batch script to list all PNG images within a target directory and its subdirectories.
Understanding the Core Command: FOR /R
The heart of our batch script lies in the FOR /R
command. This command is incredibly versatile for iterating through files in a directory tree. Let's break down its components:
FOR
: The primary command for looping./R
: This switch tellsFOR
to recurse into subdirectories. Without it,FOR
would only process files in the current directory.%variable
: A placeholder for the current file being processed in each iteration. We'll typically use%%f
in batch files.IN (set)
: Specifies the set of files to iterate over. This is where we define our search pattern, like*.png
.DO command
: The action to perform for each file found.
flowchart TD A[Start Batch Script] --> B{"Specify Target Directory?"} B -->|Yes| C[Set Target Directory] B -->|No| D[Use Current Directory] C --> E[Execute FOR /R Command] D --> E E --> F{"File Found (e.g., *.png)?"} F -->|Yes| G[Echo Full File Path] F -->|No| H[Continue Search] G --> E H --> I[End of Directory Tree] I --> J[End Script]
Workflow for listing files recursively using a batch script.
Creating the Batch Script
Now, let's put this knowledge into practice. We'll create a batch file that can be easily modified to search for different file types or in different starting directories.
@echo off
set "target_dir=%~dp0"
:: If you want to specify a different directory, uncomment the line below
:: and replace 'C:\Path\To\Your\Folder' with the desired path.
:: set "target_dir=C:\Path\To\Your\Folder"
echo Listing all PNG images in "%target_dir%" and its subdirectories:
echo -------------------------------------------------------------------
for /r "%target_dir%" %%f in (*.png) do (
echo "%%f"
)
echo -------------------------------------------------------------------
echo Search complete.
pause
Batch script to list all PNG files recursively.
%~dp0
variable in a batch file expands to the drive letter and path of the batch file itself. This makes the script portable, as it will search relative to its own location by default.How to Use the Script
Using the script is straightforward. Follow these steps:
1. Save the Code
Copy the batch script code into a plain text editor (like Notepad).
2. Save as a Batch File
Save the file with a .bat
extension (e.g., list_pngs.bat
). Make sure 'Save as type' is set to 'All Files' to prevent it from being saved as list_pngs.bat.txt
.
3. Run the Script
Double-click the list_pngs.bat
file. A command prompt window will open, displaying a list of all PNG files found. If you want to search a specific folder, edit the set "target_dir=..."
line in the script before running it.
4. Review Results
The script will pause at the end, allowing you to review the output. Press any key to close the window.
list_pngs.bat > png_list.txt
. This will create a file named png_list.txt
in the same directory as your batch file, containing the list of PNGs.