Forfiles

Last updated
forfiles
Developer(s) Microsoft
Operating system Microsoft Windows
Type Command
License Proprietary commercial software
Website docs.microsoft.com/en-us/windows-server/administration/windows-commands/forfiles

forfiles is a computer software utility for Microsoft Windows, which selects files and runs a command on them. File selection criteria include name and last modified date. The command specifier supports some special syntax options. It can be used directly on the command line, or in batch files or other scripts.

Contents

The forfiles command was originally provided as an add-on, in the Windows 98, [1] Windows NT [ citation needed ] and Windows 2000 [2] Resource Kits. It became a standard utility with Windows Vista, as part of the new management features.

Usage

The forfiles command has several command-line switches. If no switches or parameters given, it outputs the name of every file in the current directory.

Switches

SwitchArgumentMeaning
/PpathnameThe directory (folder) to search for files in. The default is the current directory. UNC paths (\\machine\share) are not accepted.
/MsearchmaskA glob pattern (wildcard search). Only files whose filename matches the pattern are selected. The file extension is included in the filename; the path (folder name) is not. The pattern must match the entire name, or use wildcards. The default is to match all files.

This option treats glob patterns *.* and * differently. The former will only match files with a dot in their name, while the latter will match even those with no dot or extension. Most DOS/Windows commands will match files with no extension even when given a *.* pattern.

/S(none)Selects matching files in subdirectories. By default, only the single, specified directory is searched.
/CcommandExecute the given command for each matching file. The command string typically needs to be wrapped in double quotes. See below for the syntax within the command string. The default command is CMD /C ECHO @FILE, which causes the name of each matching file to be output (displayed).
/DdateSelects files based on their last modified date. See below for date syntax. By default, files are selected regardless of their date.
/?(none)Displays a help message (brief usage instructions). Suppresses file search/command execution. May not be used with any other switch.

Command syntax

The command string is executed as given, except as noted below.

Sequences of the form 0xFF, where "0x" is literal, and "FF" represents any two-digit hexadecimal number, are replaced with the corresponding single-byte value. This can be used to embed non-printing ASCII characters, or extended ASCII characters.

The sequence \" is replaced with a literal quotation mark ". Using the 0x sequence form described previously, 0x22 can also be used, which additionally hides the " from the command interpreter.

Several variables are provided, to be used in the command as placeholders for the values from each file. Variables are technically not required, but must be used if the command is to vary for each file.

VariableMeaning
@fileThe name of the matching item, double quoted.
@fnameThe basename of the matching item (without file extension), double quoted.
@extThe file extension, double quoted, without leading dot. If a file has multiple extensions, only the last is returned. If the file has no extension, a quoted empty string is returned.
@pathFull path of the matching item, double quoted, including drive letter, and file extension (if any).
@relpathPath of the matching item, double quoted, and relative to the starting directory (given by /P). Each path begins with a dot and backslash (.\).
@isdirEvaluates to the literal string TRUE if the matching item is a directory, or FALSE if not.
@fsizeSize of the matching item, in bytes. Directories report a size of zero.
@fdateDate the file was last modified, in the localized date format of the current user.
@ftimeTime the file was last modified, in the localized time format of the current user.

Date syntax

The date switch (/D) selects files based on their last modified date, given a date argument.

The date argument can be given as a literal date, in MM/DD/YYYY format (other date formats are not accepted). Alternatively, the date argument can be given as a number, in which case it is taken to mean an age in days (i.e., the day date days before the present date).

If the date argument begins with a minus (-), only files modified on or before the given date are selected (older file / modified earlier). Otherwise, only files modified on or after the given date are selected (younger files / modified later). An explicit plus (+) may be given, but is the default. Note that both modes select files on the given date. There is no way to select files only on a given date (without also either before or after).

Examples

The following command selects all log files (*.LOG) in the Windows directory 30 days or older, and lists them with their date.

C:\>FORFILES /P C:\Windows /M *.LOG /D -30 /C "CMD /C ECHO @FDATE @FILE"6/12/2015 "iis7.log"5/28/2015 "msxml4-KB954430-enu.LOG"5/28/2015 "msxml4-KB973688-enu.LOG"5/26/2015 "setuperr.log"

The following command would delete the same files.

C:\>FORFILES /P C:\Windows /M *.LOG /D -30 /C "CMD /C DEL @PATH"

The use of CMD /C is required in the above examples, as both ECHO and DEL are internal to the command processor, rather than external utility programs.

See also

Related Research Articles

cd (command) Computer command in various operating systems

The cd command, also known as chdir, is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.

In computing, a command is a directive to a computer program to perform a specific task. It may be issued via a command-line interface, such as a shell, or as input to a network service as part of a network protocol, or as an event in a graphical user interface triggered by the user selecting an option in a menu.

at (command) Task scheduling command on various operating systems

In computing, at is a command in Unix-like operating systems, Microsoft Windows, and ReactOS used to schedule commands to be executed once, at a particular time in the future.

Cabinet is an archive-file format for Microsoft Windows that supports lossless data compression and embedded digital certificates used for maintaining archive integrity. Cabinet files have .cab filename extensions and are recognized by their first four bytes MSCF. Cabinet files were known originally as Diamond files.

<span class="mw-page-title-main">4DOS</span> Command line interpreter

4DOS is a command-line interpreter by JP Software, designed to replace the default command interpreter COMMAND.COM in Microsoft DOS and Windows. It was written by Rex C. Conn and Tom Rawson and first released in 1989. Compared to the default, it has a large number of enhancements.

An environment variable is a user-definable value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME or USERPROFILE variable to find the directory structure owned by the user running the process.

A path is a string of characters used to uniquely identify a location in a directory structure. It is composed by following the directory tree hierarchy in which components, separated by a delimiting character, represent each directory. The delimiting character is most commonly the slash ("/"), the backslash character ("\"), or colon (":"), though some operating systems may use a different delimiter. Paths are used extensively in computer science to represent the directory/file relationships common in modern operating systems and are essential in the construction of Uniform Resource Locators (URLs). Resources can be represented by either absolute or relative paths.

In computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.

In computing, kill is a command that is used in several popular operating systems to send signals to running processes.

cmd.exe Command prompt program

Command Prompt, also known as cmd.exe or cmd, is the default command-line interpreter for the OS/2, eComStation, ArcaOS, Microsoft Windows, and ReactOS operating systems. On Windows CE .NET 4.2, Windows CE 5.0 and Windows Embedded CE 6.0 it is referred to as the Command Processor Shell. Its implementations differ between operating systems, but the behavior and basic set of commands are consistent. cmd.exe is the counterpart of COMMAND.COM in DOS and Windows 9x systems, and analogous to the Unix shells used on Unix-like systems. The initial version of cmd.exe for Windows NT was developed by Therese Stowell. Windows CE 2.11 was the first embedded Windows release to support a console and a Windows CE version of cmd.exe. The ReactOS implementation of cmd.exe is derived from FreeCOM, the FreeDOS command line interpreter.color a

<span class="mw-page-title-main">Comparison of command shells</span>

A command shell is a command-line interface to interact with and manipulate a computer's operating system.

In computing, sleep is a command in Unix, Unix-like and other operating systems that suspends program execution for a specified time.

whoami

In computing, whoami is a command found on most Unix-like operating systems, Intel iRMX 86, every Microsoft Windows operating system since Windows Server 2003, and on ReactOS. It is a concatenation of the words "Who am I?" and prints the effective username of the current user when invoked.

Robocopy is a command-line file transfer utility for Microsoft Windows. Robocopy is functionally more comprehensive than the COPY command and XCOPY, but replaces neither. Created by Kevin Allen and first released as part of the Windows NT 4.0 Resource Kit, it has been a standard feature of Windows since Windows Vista and Windows Server 2008.

In Microsoft Windows, cacls and its replacement, icacls, native command-line utilities capable of displaying and modifying the security descriptors on folders and files. An access-control list is a list of permissions for securable object, such as a file or folder, that controls who can access it. The cacls command is also available on ReactOS.

A batch file is a script file in DOS, OS/2 and Microsoft Windows. It consists of a series of commands to be executed by the command-line interpreter, stored in a plain text file. A batch file may contain any command the interpreter accepts interactively and use constructs that enable conditional branching and looping within the batch file, such as IF, FOR, and GOTO labels. The term "batch" is from batch processing, meaning "non-interactive execution", though a batch file might not process a batch of multiple data.

In computing, pushd and popd are commands used to work with the command line directory stack. They are available on command-line interpreters such as 4DOS, Bash, C shell, tcsh, Hamilton C shell, KornShell, cmd.exe, and PowerShell for operating systems such as Windows and Unix-like systems.

find (Windows)

In computing, find is a command in the command-line interpreters (shells) of a number of operating systems. It is used to search for a specific text string in a file or files. The command sends the specified lines to the standard output device.

In computing, label is a command included with some operating systems. It is used to create, change, or delete a volume label on a logical drive, such as a hard disk partition or a floppy disk. Used without parameters, label changes the current volume label or deletes the existing label.

<span class="mw-page-title-main">Command-line interface</span> Computer interface that uses text

A command-line interface (CLI) is a means of interacting with a computer program by inputting lines of text called command-lines. Command-line interfaces emerged in the mid-1960s, on computer terminals, as a user-friendly alternative to punched cards. Operating system command-line interfaces are often implemented with command-line interpreters or command-line processors.

References