JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Batch file to loop through files in subdirectories

Author:JIYIK Last Updated:2025/03/22 Views:

This article explains how we can write a batch script to loop through files in subdirectories. We will take an example to explain the concept.


Batch file to loop through files in subdirectories

Assume we have the directory structure shown below.

- Main directory (Contains our .bat file and some top-level directories)
  - Sub-directory
    - Search Directory
      - A bunch of files (Files we want to loop through)

We can get the list of files and folders using the following command in our home directory.

for /f %%f in ('dir /b /r *') do echo %%f

However, we need to search for files in a directory. Here is an example of searching the file structure in a directory.

C:\Users\pc\Search16\0045\search\FP585.txt

/RInstead of using to loop through all the files in all subdirectories /fas shown in the previous example command, we can use as shown below.

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

The above command will display a list of all files having .txt file extension in all subdirectories.

In summary, as mentioned above, when writing a batch command to loop through files in all subdirectories, we can use /Rinstead /f.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Batch file to remove X characters from a file name

Publish Date:2025/03/22 Views:153 Category:OPERATING SYSTEM

This article explains how we can remove specific characters from the filename of a file using a batch script. We will cover several methods below to rename files on Windows. File Explorer Renaming on Windows File Explorer offers the least f

Stop a running process from a batch file

Publish Date:2025/03/21 Views:201 Category:OPERATING SYSTEM

This article explains how to stop a running process from a batch file in Windows. We use Batch's taskkill command to terminate the running process. 请注意 , the command will be executed only if the specified process is open. Batch file t

Get the current batch file directory

Publish Date:2025/03/21 Views:125 Category:OPERATING SYSTEM

This article demonstrates how to determine the location of a batch file. Batch scripts are great for automation. Sometimes you may need to get the location of a batch file. This article will help you determine the working directory and batc

Running batch files remotely

Publish Date:2025/03/21 Views:176 Category:OPERATING SYSTEM

Sometimes we need to use a computer remotely from a different location. We can use some third-party software such as TeamViewer to do this. But we can execute batch files from a remote directory without the need for third-party software. In

Run the batch file as administrator

Publish Date:2025/03/20 Views:142 Category:OPERATING SYSTEM

Batch files consist of commands that are executed by the Command Prompt. Some of these commands cannot be run without administrator privileges; therefore, it is important to run batch files as an administrator. You can manually run a batch

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial