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.
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.
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.
Switch | Argument | Meaning |
---|---|---|
/P | pathname | The directory (folder) to search for files in. The default is the current directory. UNC paths (\\machine\share) are not accepted. |
/M | searchmask | A 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 |
/S | (none) | Selects matching files in subdirectories. By default, only the single, specified directory is searched. |
/C | command | Execute 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). |
/D | date | Selects 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. |
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.
Variable | Meaning |
---|---|
@file | The name of the matching item, double quoted. |
@fname | The basename of the matching item (without file extension), double quoted. |
@ext | The 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. |
@path | Full path of the matching item, double quoted, including drive letter, and file extension (if any). |
@relpath | Path of the matching item, double quoted, and relative to the starting directory (given by /P). Each path begins with a dot and backslash (.\ ). |
@isdir | Evaluates to the literal string TRUE if the matching item is a directory, or FALSE if not. |
@fsize | Size of the matching item, in bytes. Directories report a size of zero. |
@fdate | Date the file was last modified, in the localized date format of the current user. |
@ftime | Time the file was last modified, in the localized time format of the current user. |
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).
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.
forfiles
find
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, 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.
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.
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.
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.
In Windows NT operating systems, a Windows service is a computer program that operates in the background. It is similar in concept to a Unix daemon. A Windows service must conform to the interface rules and protocols of the Service Control Manager, the component responsible for managing Windows services. It is the Services and Controller app, services.exe, that launches all the services and manages their actions, such as start, end, etc.
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.
Windows Vista contains a range of new technologies and features that are intended to help network administrators and power users better manage their systems. Notable changes include a complete replacement of both the Windows Setup and the Windows startup processes, completely rewritten deployment mechanisms, new diagnostic and health monitoring tools such as random access memory diagnostic program, support for per-application Remote Desktop sessions, a completely new Task Scheduler, and a range of new Group Policy settings covering many of the features new to Windows Vista. Subsystem for UNIX Applications, which provides a POSIX-compatible environment is also introduced.
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 a pair of commands which allow users to quickly switch between the current and previous directory when using the command line. When called, they use a directory stack to sequentially save and retrieve directories visited by the user.
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.
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 an interactive and more user-friendly alternative to the non-interactive interface available with punched cards.