FORFILES - Process Files in Bulk

Maintained on

Sometimes you want to delete only log files older than 30 days, or apply the same operation to every file with a certain extension.

The FORFILES command selects the files that match your conditions and runs a command on each one. Because it can filter by last-modified date, it is well suited to cleaning up old files.

Basic Usage

FORFILES [/P directory] [/M search_mask] [/S] [/C "command"] [/D [+|-]date_or_days]

Every option is optional. If you omit them all, the names of all files and folders in the current directory are displayed.

forfiles
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>forfiles
”backup.cmd"
"report.txt"
"logs”
C:\users\user>

Note that the names are displayed enclosed in double quotation marks.

Main Options

OptionDescription
/P directoryThe directory to start searching from (default: the current directory)
/M search_maskThe file names to target. Wildcards are allowed (default: all files)
/SSearch subdirectories recursively
/C "command"The command to run on each file (default: cmd /c echo @file)
/D date_or_daysFilter by last-modified date. Explained in detail below

Processing Files by Extension

Use /M to narrow down the targets and /C to specify the command to run.

forfiles /M *.log /C "cmd /c echo @file"

Inside the /C command, you can refer to information about the file being processed with @ variables.

VariableContent
@fileFile name (including the extension)
@fnameFile name without the extension
@extExtension (without the dot)
@pathFull path of the file
@relpathPath relative to the directory given with /P
@isdirTRUE for a directory, FALSE for a file
@fsizeFile size in bytes
@fdateLast-modified date
@ftimeLast-modified time
forfiles /P C:\Logs /M *.log /C "cmd /c echo @path @fsize bytes"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>forfiles /P C:\Logs /M *.log /C "cmd /c echo @path @fsize bytes"
”C:\Logs\app.log” 20480 bytes
”C:\Logs\error.log” 1024 bytes
C:\users\user>
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

@path and @file include double quotation marks

@file and @path are expanded with double quotation marks around the value. This means you can pass them as they are, as in del @path, and paths that contain spaces still work correctly. You do not need to add your own quotation marks, as in "@path".

Run Internal Commands Through cmd /c

FORFILES starts the command given in /C as a separate process. Internal commands built into Command Prompt, such as echo and del, are not standalone executable files, so you need to call them with cmd /c.

forfiles /M *.txt /C "echo @file"
forfiles /M *.txt /C "cmd /c echo @file"

This is also why the default value of /C is cmd /c echo @file.

チェック

External commands such as .exe files can run without cmd /c. For example: forfiles /M *.txt /C "notepad @file".

Filter by Last-Modified Date (/D)

The /D option filters the targets by last-modified date. You can specify either a number of days or a date.

SpecificationMeaning
/D -30Files last modified 30 days ago or earlier
/D -2025/01/01Files last modified on January 1, 2025 or earlier
/D 2025/01/01Files last modified on January 1, 2025 or later

If you omit the sign, + is assumed. The date format depends on the regional settings of your OS (yyyy/MM/dd on Japanese systems; Microsoft’s documentation lists MM/DD/YYYY). The output format of @fdate also follows the regional settings. If you specify a number of days, you do not need to worry about the format.

forfiles /P C:\Logs /M *.log /D -7 /C "cmd /c echo @file @fdate"
A number of days includes the boundary day

/D -30 targets files whose last-modified date is the same as the date 30 days ago, or older. Files modified exactly 30 days ago are included.

Delete Old Files Safely

Periodically deleting logs is a typical use of FORFILES. However, deletion cannot be undone, so always proceed in the following two steps.

Step 1: Display the targets without deleting, and check them

forfiles /P C:\Logs /M *.log /S /D -30 /C "cmd /c if @isdir==FALSE echo @path"

Step 2: If the targets look right, replace echo with del

/M *.log also matches folders, so if @isdir==FALSE limits the targets to files. Use the same condition in the preview and in the deletion.

forfiles /P C:\Logs /M *.log /S /D -30 /C "cmd /c if @isdir==FALSE del @path"
del cannot be undone

Files deleted with del do not go to the Recycle Bin. If you get the /P path wrong, you may delete files you did not intend to. Always check the targets in Step 1 before running the deletion.

Process Subfolders Too (/S)

With /S, all subfolders under the directory given with /P are also targeted.

forfiles /P C:\Docs /S /M *.txt /C "cmd /c echo @relpath"

@relpath returns a path relative to the directory given with /P, such as ".\sub\note.txt".

Target Only Folders

With @isdir, you can tell files and folders apart.

forfiles /P C:\Docs /C "cmd /c if @isdir==TRUE echo @file"

Specify Special Characters

If you want to use characters such as double quotation marks inside /C, you can specify a hexadecimal code in the form 0xHH.

forfiles /M *.txt /C "cmd /c echo 0x22@fname0x22"
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>forfiles /M *.txt /C "cmd /c echo 0x22@fname0x22"
”readme"
"memo”
C:\users\user>

0x22 represents a double quotation mark (").

Tips for Batch Files

When No Target Is Found

If no file matches the conditions, FORFILES displays an error message and sets ERRORLEVEL to a non-zero value. You can use this when you want a batch file to detect “no targets”. However, other errors, such as a wrong path, also return 1, so it is not suited to strict checks.

@echo off
forfiles /P C:\Logs /M *.log /D -30 >nul 2>&1
if errorlevel 1 (
    echo There are no logs to delete.
) else (
    echo Logs older than 30 days were found.
)

About % in Batch Files

FORFILES @ variables start with @, so you do not need to double any % in a batch file. On the other hand, when you use a variable such as %LOG_DIR% in a path, enclose it in double quotation marks in case it contains spaces.

@echo off
set LOG_DIR=C:\My Logs
set DAYS=30

forfiles /P "%LOG_DIR%" /M *.log /S /D -%DAYS% /C "cmd /c echo @path"
When to use FOR instead

If you only need to repeat an operation over a list of file names, the FOR command can do it too. However, FOR cannot easily compare last-modified dates, so FORFILES is handy when you need a date condition.

Summary

TaskCommand
List file namesforfiles /M *.log
Run an internal commandforfiles /C "cmd /c echo @file"
Include subfoldersforfiles /S
Extract files N or more days oldforfiles /D -N
Specify the starting directoryforfiles /P directory
Process by full path@path

Practice Questions

Practice Problem

Which command lists files that were last modified 30 or more days ago?

回答がサーバーに送信されることはありません
Practice Problem

Why do you add cmd /c when running echo or del with /C in FORFILES?

回答がサーバーに送信されることはありません
Practice Problem

What should you do first, for safety, when deleting old files with FORFILES?

回答がサーバーに送信されることはありません
#Command Prompt #Batch File #FORFILES #Command #File Operations