Forfiles command in Windows

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

  1. Forfiles are often used in batch files.
  2. Forfiles / s is similar to dir / s .
  3. 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.
  1. With forfiles, you can run the above command or pass arguments to multiple files. For example, 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.
  2. With forfiles, you can do any of the following:
    1. Select file by absolute date or relative date using parameter / d.
    2. Create a tree that stores files using variables like @FSIZE and @FDATE.
    3. Differentiate files from directories using the @ISDIR variable .
    4. Include special characters in the command line using hexadecimal code for characters, in 0xHH format (eg 0x09 for a tab).
  3. Forfiles work by deploying recurse subdirectories on tools designed to handle only a single file.

For example

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." 

To list the file name extension of all files in the current directory in column format and add a tab before the extension, enter:

 forfiles /s /m *.* /c "cmd /c echo The extension of @file is 0x09@ext" 

See more:

  1. Fondue command in Windows
  2. Fc command in Windows
  3. The rpcping command in Windows
3.9 ★ | 57 Vote

May be interested

  • Instructions for using Command PromptInstructions for using Command Prompt
    deep in windows is a command-line world that is obscure. in this article, we will show you how to solve errors and make your computer more secure.
  • Rd command in WindowsRd command in Windows
    the rd command helps delete a directory
  • Cmd command in WindowsCmd command in Windows
    the cmd command starts a new version of the command interpreter, cmd.exe. if used without parameters, cmd will display copyright information and the version of the operating system.
  • How to use the command history function in Command PromptHow to use the command history function in Command Prompt
    command prompt is an extremely familiar command for anyone using windows operating system. besides, a lot of current software also supports the command line to perform actions on the command prompt window, instead of on the screen.
  • The sfc command in WindowsThe sfc command in Windows
    (applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
  • Reg command copy in WindowsReg command copy in Windows
    to copy an entry to the location specified on the local pc or the remote computer we will use the reg copy command. to learn more about how to use the reg copy command, you can track the syntax, explain the command parameters and the example tipsmake.com has compiled below.
  • Fc command in WindowsFc command in Windows
    the fc command compares two files or a collection of files and displays the differences between them.
  • The echo command in WindowsThe echo command in Windows
    the echo command displays the message or turns on / off the command repeat feature. if used without parameters, the echo command will display the current echo setting.
  • Xcopy command in WindowsXcopy command in Windows
    the xcopy command is a command prompt command used to copy one or more files or folders from one location to another. with many options and the ability to copy entire directories, the xcopy command is similar to, but much more powerful, than the copy command.
  • The cacls command in WindowsThe cacls command in Windows
    the cacls command displays or modifies an arbitrary access control list (dacl) on the specified file. the command applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012.