Official introduction of forfiles

Time:2020-3-23

Forfiles
Select the file to batch from the folder or tree.

grammar
forfiles [/p Path] [/m SearchMask] [/s] [/c Command] [/d[{+ | -}] [{MM/DD/YYYY | DD}]]

parameter
/p Path
Specify the path to indicate where to start the search. The default folder is the current working directory, which is specified by typing a period (.).
/m SearchMask
Follow searchmask to search for files. The default searchmask is *. *. *.
/s
Instructs forfiles to search in subdirectories.
/c Command
Runs the specified command on each file. Command strings with spaces must be enclosed in quotation marks. The default command is “CMD / C echo @ file”.
/d[{+ | -}] [{MM/DD/YYYY | DD}]
Select a file with a date greater than or equal to (+ (or less than or equal to (-)), where mm / DD / yyyy is the specified date and DD is the current date minus DD days. If + or – is not specified, then +. The valid range for DD is 0 – 32768.
/?
Display help at the command prompt.
Notes
Forfiles is most commonly used in batch files.

Forfiles / S is similar to dir / s.

The following table lists the variables that can be used in the / ccommand command string.

variable describe

@file

file name

@fname

File name without extension

@ext

File extension

@path

Full path to file

@relpath

Relative path of file

@isdir

True if the file type is directory, false otherwise

@fsize

File size in bytes

@fdate

Date stamp of the last modification in the file

@ftime

Last modified timestamp in file

With forfiles, you can run commands on multiple files or pass parameters to multiple files. For example, you can run the type command on all files in the tree with the *. TXT extension. Alternatively, you can use the filename “myinput. TXT” as the first parameter to execute each batch file (*. Bat) on the C: \ drive.

With forfiles, you can do any of the following:

Use / D to select files by absolute or relative dates.

Use variables such as @ fsize (file size) and @ fdate (file date) to build the archive tree for the file.

Use the @ isdir variable to distinguish between files and directories.

Format the output by including special characters on the command line and using the hex code 0xhh surround character.

Forfiles works by executing a “loop subdirectory” flag on a tool designed to process only a single file.

Example
To list all batch files on drive C:, type:
forfiles /p c:\ /s /m*.bat /c”cmd /c echo @file is a batch file”
To list all directories on drive C:, type:
forfiles /p c:\ /s /m*.* /c”cmd /c if @isdir==true echo @file is a directory”
To list all files older than 100 days on drive C:, type:
forfiles /p c:\ /s /m*.* /dt-100 /c”cmd /c echo @file :date >= 100 days”
To list all files created before January 1, 1993 on drive C, and to display “file is quiet old!” for files older than January 1, 1993, type:
forfiles /p c:\ /s /m*.* /dt-01011993 /c”cmd /c echo @file is quite old!”
To list the extensions for all files on drive C: in column format, type:
forfiles /p c:\ /s /m*.* /c “cmd /c echo extension of @file is [email protected]” With:
To list all batch files on drive C:, type:
forfiles /p c:\ /s /m *.bat /c “cmd /c echo @file is a batch file”
To list all directories on drive C:, type:
forfiles /p c:\ /s /m *.* /c “cmd /c if @isdir==true echo @file is a directory”
To list all files older than 100 days on drive C:, type:
forfiles /p c:\ /s /m *.* /d t-100 /c “cmd /c echo @file :date >= 100 days”
To list all files created before January 1, 1993 on drive C, and to display “file is quiet old!” for files older than January 1, 1993, type:
forfiles /p c:\ /s /m *.* /d t-01011993 /c “cmd /c echo @file is quite old!”
To list the extensions for all files on drive C: in column format, type:
forfiles /p c:\ /s /m*.* /c “cmd /c echo extension of @file is [email protected]

Format legend

format Significance

Italics

Information users must provide

bold

The user must type the elements shown exactly

Ellipsis (…)

Parameters that can be repeated multiple times on the command line

Between brackets ([])

optional field

Between braces ({}); separates options with a vertical bar (|). Example: {even| odd}

Option group from which the user must select only one option

Courier font

Code or program output