FORFILES - Process Files in Bulk
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
Note that the names are displayed enclosed in double quotation marks.
Main Options
| Option | Description |
|---|---|
/P directory | The directory to start searching from (default: the current directory) |
/M search_mask | The file names to target. Wildcards are allowed (default: all files) |
/S | Search subdirectories recursively |
/C "command" | The command to run on each file (default: cmd /c echo @file) |
/D date_or_days | Filter 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.
| Variable | Content |
|---|---|
@file | File name (including the extension) |
@fname | File name without the extension |
@ext | Extension (without the dot) |
@path | Full path of the file |
@relpath | Path relative to the directory given with /P |
@isdir | TRUE for a directory, FALSE for a file |
@fsize | File size in bytes |
@fdate | Last-modified date |
@ftime | Last-modified time |
forfiles /P C:\Logs /M *.log /C "cmd /c echo @path @fsize bytes"

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
@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.
| Specification | Meaning |
|---|---|
/D -30 | Files last modified 30 days ago or earlier |
/D -2025/01/01 | Files last modified on January 1, 2025 or earlier |
/D 2025/01/01 | Files 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"
/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"
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"
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"
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
| Task | Command |
|---|---|
| List file names | forfiles /M *.log |
| Run an internal command | forfiles /C "cmd /c echo @file" |
| Include subfolders | forfiles /S |
| Extract files N or more days old | forfiles /D -N |
| Specify the starting directory | forfiles /P directory |
| Process by full path | @path |