Clean up Prophet 21 P21Forms directory on Windows servers using forfiles command


Many applications create files for logging the functionality and errors occurred during operation. There are times the log files are not cleaned up and start to accumulate within a folder on the operating system.  Using the “forfiles” command to reduce the number of files in a directory (folder) for faster indexing (listing) of files in Windows 7, 8 and Windows Server 2008, 2008 R2, 2012, 2012 R2, and 2014.


Running nightly backups of websites to an external USB drive reduces the space on the drive since every backup is being retained for archive purposes. The files are compressed to save space, but accumulate due to no cleanup module. Using the “forfiles” command from the command line, in Task Manager, or within the actual backup script will reduce the number of files within the directories (folders).

Note: Forfiles will only accept single arguments for search criteria, so if the directory (folder) has multiple search criteria, in the nightly backup example there are both .tar and .bak files that need to be removed, so you will need to perform a loop within a batch file.

Note: Be careful with wildcard characters like “*” and “?” since forfiles will bypass the recycle bin.

Note: This is a way to delete files from the command line or a batch file and not a way to bypass other deletion methods. Explorer.exe will consume CPU and memory while running so consider running forfiles during off-hours.

Note: Test forfiles with a non-intrusive command like dir to examine the files selected before running forfiles command section with a dir command.


  1. Open a command prompt by typing cmd in the search or run text boxes in Windows.
  2. At the command line type forfiles /? and press Enter
    1. If you receive the message “forfiles is not recognized as an internal or external command, operable batch file or program” the means the environmental path is not in the path statement on the PC or server.
      1. 64 bit system: C:\Windows\System32\forfiles
      2. Use the full path in the command (recommended for automated tasks) or add the path to the Environmental Variables – Path statement
  3. Let us analyze the following command: “forfiles /P L:\Backups\MyWebsite\ /M *.tar /D -200 /C “cmd /c del @file”
    1. /P is the path directive. In the example the folder is on the L: disk and under the Backups folder.
    2. /M is the search pattern. The search pattern should be as close to finding the files that the command will execute against.
    3. /D is the date for the files that will be acted against by the command. In this example -200 means delete any files with a .tar extension that is over 200 days old.
    4. /C is the actual command. “cmd /c del @file” is what will be executed against any .tar files that are over 200 days old in the L:\Backups\MyWebsite\ folder.
    5. /S **CAUTION** with this flag. This means recursive and will follow into subdirectories within the main directory. This could have undesirable effects and should be tested with a non-final command like “cmd /c dir @file”. Taking the previous command “forfiles /P L:\Backups\ /S /M *.tar /D -200 /C “cmd /c del @file” will delete all TAR files over 200 days old in any folder under L:\Backups.
  4. Logging in forfiles can be accomplished by creating a log file, like forfiles /P L:\Backups\ /S /M *.tar /D -200 /C “cmd /c del @file” >> C:\temp\myfiles.txt

This is a small example of what the forfiles command can do to clean up large number of files on a Windows system.

Comments are closed.