FORFILES.exe – Windows CMD Command

Select a file (or set of files) and execute a command on each file. Batch processing.

To include special characters in the command line, use the hexadecimal code for the character in 0xHH format (ex. 0x09 for tab). Internal CMD.exe commands should be preceded with “cmd /c”.

The last modified dates set in the future are not common but can happen when your computer clock date/time is changed e.g. due to daylight savings time.

Early versions of FORFILES were supplied in the NT resource kit and used Unix style ‘-‘ parameters, (still supported for backward compatibility) also the /D option accepted dates only in DDMMYYYYHHMN format.


Recent versions of FORFILES contain a bug, in processing command-line arguments: running a /c command that contains an argument such as FORFILES /C “PING -a” will fail. The expected convention is that argv[0] will contain the program name, but FORFILES instead passes the first argument as argv[0]. Old versions e.g. FORFILES version 1.46 (24th March 2006) do not suffer from this bug.

One workaround for this is to include the program name as the first argument, this will be passed along by FORFILES and the CreateProcess call will then work as expected:
FORFILES /c “Ping Ping -a”

Alternatively pass the command to CMD /C, which will then process all the arguments correctly.
If you want to use an internal command, or if you want to use redirection, pipe, command concatenation, etc, then you must use CMD /C.
FORFILES /c “CMD /C Ping -a”

There are some disadvantages to using CMD.exe with FORFILES, a new process will be created and destroyed for every file that FORFILES processes, so if you loop through 1000 files, then 1000 copies of CMD.exe will be opened and closed, this will affect performance. Also, any variable created with SET will be lost as soon as FORFILES moves on to the next file.

In most cases using a simple FOR command along with Parameter extensions will provide a better/less buggy solution.

Return values

If ForFiles finds one or more matches if will return Errorlevel =0
If ForFiles finds no matches if will return Errorlevel =1 and will print “ERROR: No files found with the specified search criteria.”

You may also like...

2 Responses

  1. zortilonrel says:

    It?¦s in reality a great and helpful piece of info. I?¦m happy that you shared this useful information with us. Please stay us informed like this. Thank you for sharing.

  2. frolep rotrem says:

    I am not really great with English but I get hold this real easy to read .

Leave a Reply

Your email address will not be published.