Forfiles Command and Advanced Skills

1.    Special sign ‘@’ in forfiles command

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.

 

2.    Forfiles shows the errors like ERROR: The filename or extension is too long. I don't want to see the errors. I want to see only matched filename

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:

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.

3.    What is the meaning of 2>nul

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:

  1. 2>: This specifies the redirection of the standard error stream. In Windows command-line, 2 represents the standard error stream.
  2. 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:

By using 2>nul, you ensure that only the matched filenames are displayed, and any error messages are suppressed.

4.    What other numbers except 2 in redirection

In Windows command-line, the numbers used for redirection represent different standard streams. Here are the commonly used standard streams:

  1. 0: Standard input (stdin)
  2. 1: Standard output (stdout)
  3. 2: Standard error (stderr)

These streams are used to redirect input and output in command-line operations. Here's how each can be used:

4.1.1.      Standard Input (0)

Redirect input from a file to a command:

sh
command < inputfile

This redirects the contents of inputfile to the standard input of command.

4.1.2.      Standard Output (1)

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.

4.1.3.      Standard Error (2)

Redirect error messages to a file:

sh
command 2> errorfile

This redirects the standard error output of command to errorfile.

4.1.4.      Combined Redirection

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.

4.1.5.      Example with 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:

Using these redirection techniques allows for precise control over how input and output are handled in scripts and command-line operations.

5.    I used the following command forfiles /S /m *confid* 2>nul . In addition, I want to see only files.

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

5.1.1.      Explanation:

  1. /S: Instructs forfiles to search recursively in subdirectories.
  2. /M *confid*: Specifies the search pattern. In this case, it searches for files containing "confid" in their names.
  3. /C "cmd /c if @isdir==FALSE echo @path":
  4. 2>nul: Redirects any error messages to nul, suppressing them.

5.1.2.      Running the Command:

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.

 

6.    I want to make alias for "forfiles /S /m *confid*.doc* /c "cmd /c if @isdir==FALSE echo @path" 2>nul". How to use the alias with pattern which can be used for /M option.

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
  1. Save the Batch File: Save and close the batch file.

6.1.1.      Explanation:

6.1.2.      Usage:

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.

6.1.3.      Adding to PATH (Optional):

If you want to be able to run findfiles from any directory without specifying the full