How to use the command 'forfiles' (with examples)
- Windows
- December 25, 2023
The ‘forfiles’ command is a powerful tool in Windows that allows users to search for files and execute a specified command on them. It provides a flexible way to perform batch operations on files, including searching for files in a specific directory, running a command for each file, and more.
Use case 1: Search for files in the current directory
Code:
forfiles
Motivation: This use case is useful when you want to search for files within the current directory without any additional parameters or filters.
Explanation: By running the ‘forfiles’ command without any arguments, it will search for files inside the current directory.
Example Output:
C:\Users\Username\Documents\file1.txt
C:\Users\Username\Documents\file2.txt
C:\Users\Username\Documents\file3.txt
Use case 2: Search for files in a specific directory
Code:
forfiles /p path\to\directory
Motivation: This use case is helpful when you want to search for files in a specific directory other than the current one.
Explanation: The ‘/p’ flag is used to specify the path to the directory where you want to search for files. Replace ‘path\to\directory’ with the actual path to the desired directory.
Example Output:
C:\Users\Username\Documents\file1.txt
C:\Users\Username\Documents\file2.txt
C:\Users\Username\Documents\file3.txt
Use case 3: Run the specified command for each file
Code:
forfiles /c "command"
Motivation: This use case is useful when you want to execute a command for each file found by ‘forfiles’. It allows you to perform batch operations on the files.
Explanation: The ‘/c’ flag is used to specify the command you want to run for each file. Replace “command” with the desired command or script. You can use variables like ‘@file’ to represent the current file being processed.
Example Output:
Executing command for file: file1.txt
Executing command for file: file2.txt
Executing command for file: file3.txt
Use case 4: Search for files using a specific glob mask
Code:
forfiles /m glob_pattern
Motivation: This use case is handy when you want to search for files using a specific file glob pattern.
Explanation: The ‘/m’ flag is used to provide a file glob pattern to filter the search results. Replace ‘glob_pattern’ with the desired pattern. For example, ‘*.txt’ will search for all files with a ‘.txt’ extension.
Example Output:
C:\Users\Username\Documents\file1.txt
C:\Users\Username\Documents\file2.txt
C:\Users\Username\Documents\file3.txt
Use case 5: Search for files recursively
Code:
forfiles /s
Motivation: This use case is useful when you want to search for files not only in the current directory but also in all subdirectories.
Explanation: The ‘/s’ flag is used to enable the recursive search mode, which includes all subdirectories when searching for files.
Example Output:
C:\Users\Username\Documents\file1.txt
C:\Users\Username\Documents\Subdirectory\file2.txt
C:\Users\Username\Documents\Subdirectory\file3.txt
Use case 6: Search for files older than 5 days
Code:
forfiles /d +5
Motivation: This use case is helpful when you want to find files that were last modified more than a specified number of days ago.
Explanation: The ‘/d’ flag is used to specify the number of days. By using the ‘+5’ argument, it will search for files that were modified more than 5 days ago.
Example Output:
C:\Users\Username\Documents\file1.txt
C:\Users\Username\Documents\file2.txt
C:\Users\Username\Documents\file3.txt
Conclusion:
The ‘forfiles’ command provides a versatile way to search for files and perform batch operations on them in Windows. By using the various flags and parameters, you can customize the search criteria and the command to be executed for each file. This command is particularly useful for automating file management tasks and performing bulk operations efficiently.