Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

Forfiles Command in Windows: A Practical Guide

Understand Forfiles Command in Windows with clear explanations, practical examples, and useful tips. This updated guide covers the essential concepts and...

Table of Contents

Forfiles Command in Windows is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.

The forfiles command selects and executes a command on a file or set of files. This command is very useful for batch processing.

For an example of how to use this command, please see the example below.

Syntax forfiles command

 forfiles [/p ] [/m ] [/s] [/c " "] [/d [{+|-}][{|}]]   forfiles [/p ] [/m ] [/s] [/c " "] [/d [{+|-}][{|}]] 

Parameters

Parameter Description / p Specifies the path from which the command starts searching. By default, the search starts in the current directory. / m Search for files according to the specified search mash. The default search mash is *. *. / s Indicates the search forfiles command recursively in the subdirectory. / c " "Run the specified command on each file. The command string must be enclosed in quotation marks. The default command is " cmd / c echo @file " . / D [{+ | -}] ? [{| ?}] Select the file whose date was last modified in the specified time frame. - Select the file whose date is modified later or equal to (+) or earlier or equal to (-) the specified date, in which Date has the format MM / DD / YYYY. - Select the file with the date to be modified later or equal to the current (+) date plus the number of days specified or earlier or equal to (-) the current date minus the specified number of days. - Valid values for Days include any number in the range from 0 to 32,768. If no indication is specified, + is used by default. /? Show help at the command prompt.

Note

  • Forfiles are often used in batch files.
  • Forfiles / s is similar to dir / s .
  • You can use the following variables in the command string, when specified by the command line option / c:
Description variable @ FILE File name. @FNAME File name without extension. @EXT File name extension. @PATH Full path of the file. @RELPATH The relative path of the file. @ISDIR Evaluate TRUE if the file type is a directory. If not, this variable will evaluate to FALSE. @FSIZE File size, in bytes. @FDATE The latest change date is recorded on the file. @FTIME The latest change time is recorded on the file.
  • With forfiles, you can run the above command or pass arguments to multiple files. For instance,, you can run the type command on all files in a directory tree with the .txt file name extension . Or you can execute all batch files (* .bat) on drive C, with the file name "Myinput.txt" as the first argument.
  • With forfiles, you can do any of the following:
    • Select file by absolute date or relative date using parameter / d.
    • Create a tree that stores files using variables like @FSIZE and @FDATE.
    • Differentiate files from directories using the @ISDIR variable .
    • Include special characters in the command line using hexadecimal code for characters, in 0xHH format (eg 0x09 for a tab).
  • Forfiles work by deploying recurse subdirectories on tools designed to handle only a single file.

For instance,

To list all batch files on drive C, enter:

 forfiles /pc: /s /m *.bat /c "cmd /c echo @file is a batch file" 

To list all folders on drive C, enter:

 forfiles /pc: /s /m *.* /c "cmd /c if @isdir==TRUE echo @file is a directory" 

To list all files in the current directory that have existed for at least a year, enter:

 forfiles /s /m *.* /d -365 /c "cmd /c echo @file is at least one year old." 

To display the " File is outdated" text for each file in the current directory, existing before January 1, 2007, enter:

 forfiles /s /m *.* /d -01/01/2007 /c "cmd /c echo @file is outdated." 
  • Fondue command in Windows
  • Fc command in Windows
  • The rpcping command in Windows

FAQ

What should you know about syntax forfiles command forfiles [/p ] [/m ] [/s] [/c " "] [/d [{+|-}][{|}]] forfiles [/p ] [/m ] [/s] [/c " "] [/d [{+|-}][{|}]] Parameters Parameter Description / p Specifies the path from which the command starts searching. By default, the search starts in the current directory. / m Search for files according to the specified search mash. The default search mash is *. *. / s Indicates the search forfiles command recursively in the subdirectory. / c " "Run the specified command on each file. The command string must be enclosed in quotation marks. The default command is " cmd / c echo @file " . / D [{+ | -}] ? [{| ?}] Select the file whose date was last modified in the specified time frame. - Select the file whose date is modified later or equal to (+) or earlier or equal to (-) the specified date, in which Date has the format MM / DD / YYYY. - Select the file with the date to be modified later or equal to the current (+) date plus the number of days specified or earlier or equal to (-) the current date minus the specified number of days. - Valid values for Days include any number in the range from 0 to 32,768. If no indication is specified, + is used by default. /? Show help at the command prompt. Note Forfiles are often used in batch files. Forfiles / s is similar to dir / s . You can use the following variables in the command string, when specified by the command line option / c: Description variable @ FILE File name. @FNAME File name without extension. @EXT File name extension. @PATH Full path of the file. @RELPATH The relative path of the file. @ISDIR Evaluate TRUE if the file type is a directory. If not, this variable will evaluate to FALSE. @FSIZE File size, in bytes. @FDATE The latest change date is recorded on the file. @FTIME The latest change time is recorded on the file. With forfiles, you can run the above command or pass arguments to multiple files. For instance,, you can run the type command on all files in a directory tree with the .txt file name extension . Or you can execute all batch files (* .bat) on drive C, with the file name "Myinput.txt" as the first argument. With forfiles, you can do any of the following: Select file by absolute date or relative date using parameter / d. Create a tree that stores files using variables like @FSIZE and @FDATE. Differentiate files from directories using the @ISDIR variable . Include special characters in the command line using hexadecimal code for characters, in 0xHH format (eg 0x09 for a tab). Forfiles work by deploying recurse subdirectories on tools designed to handle only a single file. For instance,?

To list all batch files on drive C, enter:

What is Forfiles Command in Windows?

The forfiles command selects and executes a command on a file or set of files.

Why is Forfiles Command in Windows important?

A clear understanding of Forfiles Command in Windows helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.