Forfiles Command and Advanced Skills
In the context of the `forfiles` command in a Windows batch script, the `@` sign is used as a placeholder for various attributes of the files being processed. When you use `forfiles`, you can refer to different attributes of the files using `@` followed by a specific attribute.
Here are some common attributes that you can reference with `@` in `forfiles`:
- `@file`: Represents the file name.
- `@fname`: Represents the file name without extension.
- `@ext`: Represents the file extension.
- `@path`: Represents the full path of the file.
- `@relpath`: Represents the relative path of the file.
- `@isdir`: Indicates whether the file is a directory (returns TRUE or FALSE).
- `@fsize`: Represents the size of the file in bytes.
- `@fdate`: Represents the date of the file.
- `@ftime`: Represents the time of the file.
For example, if you want to echo the full path of each file found by `forfiles`, you can use `@path`:
```batch
forfiles /S /M *.* /C "cmd /c echo @path"
```
This command will echo the full path of each file found by `forfiles`. Each occurrence of `@path` will be replaced by the full path of the file being processed.
To suppress errors in the forfiles
command and only display the matched filenames, you can redirect the error
output to nul
. This is
achieved by appending 2>nul
to your command. Here's an example of how you can do this:
sh
forfiles /p
"C:\Path\To\Directory" /s /m *.ext /c
"cmd /c echo @file" 2>
nul
In this command:
/p "C:\Path\To\Directory"
specifies the
path to the directory you want to search in./s
tells forfiles
to search in subdirectories as well./m *.ext
specifies the file mask (in this example,
files with a certain extension, e.g., .ext
)./c "cmd /c echo @file"
specifies the
command to execute on each file that matches the criteria. @file
will be replaced by the file
name.2>nul
redirects the error output stream (stderr)
to nul
, effectively
suppressing the error messages.If you need to perform a different operation or specify a different path,
adjust the command accordingly. The key part is the 2>nul
at the end, which ensures that
error messages are not displayed.
The 2>nul
part of the
command is used to redirect the error output (standard error, or stderr) to nul
, which effectively discards any
error messages.
Here's a breakdown of the components:
2>
: This specifies the
redirection of the standard error stream. In Windows command-line, 2
represents the standard error
stream.nul
: This is a special device
file that discards all data written to it. Redirecting to nul
means that the data is
effectively thrown away and not displayed on the screen.When you use 2>nul
at
the end of your command, it tells the command processor to send any error
messages (stderr) to nul
, so
they do not appear in the command prompt output. This is useful when you want
to suppress error messages and only see the successful output of your command.
For example:
sh
forfiles /p
"C:\Path\To\Directory" /s /m *.ext /c
"cmd /c echo @file" 2>nul
In this command:
forfiles
searches for files matching the specified
criteria./p "C:\Path\To\Directory"
specifies the
directory to search./s
tells forfiles
to search subdirectories./m *.ext
specifies the file pattern to match (e.g.,
all .ext
files)./c "cmd /c echo @file"
specifies the
command to execute for each file found, which in this case is to echo
(print) the file name.2>nul
discards any error messages that forfiles
might produce, such as
"The filename or extension is too long".By using 2>nul
, you
ensure that only the matched filenames are displayed, and any error messages
are suppressed.
In Windows command-line, the numbers used for redirection represent different standard streams. Here are the commonly used standard streams:
These streams are used to redirect input and output in command-line operations. Here's how each can be used:
Redirect input from a file to a command:
sh
command < inputfile
This redirects the contents of inputfile
to the standard input of command
.
Redirect output to a file:
sh
command > outputfile
This redirects the standard output of command
to outputfile
.
Append output to a file (instead of overwriting):
sh
command >> outputfile
This appends the standard output of command
to outputfile
.
Redirect error messages to a file:
sh
command 2> errorfile
This redirects the standard error output of command
to errorfile
.
Redirect both standard output and standard error to the same file:
sh
command > outputfile 2>&1
This redirects the standard output to outputfile
and then redirects the standard error (2) to the same location as the standard
output (1).
Alternatively, you can also do this:
sh
command > outputfile 2> errorfile
This redirects the standard output to outputfile
and the standard error to errorfile
.
forfiles
To illustrate combined redirection with forfiles
,
here is an example that redirects standard output to outputfile.txt
and discards error
messages:
sh
forfiles /p
"C:\Path\To\Directory" /s /m *.ext /c
"cmd /c echo @file" > outputfile.txt 2>nul
This command will:
outputfile.txt
.nul
.Using these redirection techniques allows for precise control over how input and output are handled in scripts and command-line operations.
To use the forfiles
command to find files that match a specific pattern and display only the
filenames, while suppressing error messages, you can refine your command to use
the @path
variable. Here's
how you can modify your command:
sh
forfiles /S /M *confid* /C
"cmd /c if @isdir==FALSE echo @path" 2>nul
/S
: Instructs forfiles
to search recursively in
subdirectories./M *confid*
: Specifies the
search pattern. In this case, it searches for files containing
"confid" in their names./C "cmd /c if @isdir==FALSE echo @path"
:cmd /c
: Runs the specified command (if @isdir==FALSE echo @path
) in
the command prompt.if @isdir==FALSE
: Checks if the current item is not
a directory.echo @path
: Displays the full path of the file.2>nul
: Redirects any error
messages to nul
,
suppressing them.When you run the command, it will search for all files that match the
pattern *confid*
, display
only the file paths, and ignore any errors such as "The filename or
extension is too long".
Here is a practical example to make it clearer:
sh
forfiles /S /M *confid* /C
"cmd /c if @isdir==FALSE echo @path" 2>nul
This command will list the full paths of all files (not directories) that contain "confid" in their names, while suppressing error messages.
Creating an alias in Windows for a complex command like forfiles
with a variable pattern can be
done using a batch file. Unfortunately, Windows Command Prompt does not support
aliasing in the same way Unix-like systems do with alias
in shells like Bash. However, you
can achieve similar functionality by creating a batch script.
Here's how you can do it:
1.
Create a Batch File: Create a new batch file (e.g., findfiles.bat
).
2. Edit the Batch File: Open the batch file in a text editor and add the following content:
batch
@echo off
if "%~1"=="" (
echo Usage: findfiles pattern
exit /b 1
)
forfiles /S /M %1 /C "cmd /c if @isdir==FALSE echo @path" 2>nul
@echo off
: Prevents the commands from being
displayed in the command prompt.if "%~1"==""
: Checks if the
first argument (the pattern) is empty. If it is, it prints a usage message
and exits.forfiles /S /M %1 /C "cmd /c if @isdir==FALSE echo
@path" 2>nul
: Runs the forfiles
command with the pattern passed as the
first argument to the batch script (%1
).
It suppresses error messages with 2>nul
.To use your new batch file, open a command prompt and navigate to the directory where the batch file is located, or add its location to your system's PATH environment variable for easy access from any directory.
sh
findfiles
"*confid*.doc*"
This command will search for all files matching the pattern *confid*.doc*
, display only the file
paths, and suppress error messages.
If you want to be able to run findfiles
from any directory without specifying the full