Why does Windows FORFILES with option /D -10 not find files last modified within last 10 days?

1.2k Views Asked by At

The following command line lists every .JS file in the current directory and all subfolders, not just the files last modified in the previous 10 days as expected by me.

FORFILES /S /D -10 /M *.js /C "cmd /c echo @path"

Whereas this command lists only the .JS files modified on or after 2/20/2022.

FORFILES /S /D 2/20/2022 /M *.js /C "cmd /c echo @path"

Any ideas why using the option /D -10 doesn't result in the expected output?

1

There are 1 best solutions below

0
Mofi On

The execution of forfiles /? in a command prompt window to show the usage help results on Windows with language English US, but with region/country/format German (Austria) configured for the user account, for option /D in the output:

Selects files with a last modified date greater than or equal to (+), or less than or equal to (-), the specified date using the "dd.MM.yyyy" format; or selects files with a last modified date greater than or equal to (+) the current date plus "dd" days, or less than or equal to (-) the current date minus "dd" days. A valid "dd" number of days can be any number in the range of 0 - 32768. "+" is taken as default sign if not specified.

The date format is region dependent.

dd.MM.yyyy means:

  1. Day in month with two digits, i.e. 01 to 31.
  2. A dot is the separator between day in month and month.
  3. Month with two digits, i.e. 01 to 12.
  4. A dot is the separator between month and year.
  5. Year with century with four digits like 2022.

The specification of a date in international date format yyyy-MM-dd results on my PC in the error message:

ERROR: Invalid date specified.
Type "FORFILES /?" for usage.

Some examples for a better understanding:

forfiles /D 13.08.2021
forfiles /D +13.08.2021

This outputs all file system entries (file and directory names) with last modification date newer (greater than) or equal August 13th, 2021.

forfiles /D -13.08.2021

This outputs all file system entries (file and directory names) with last modification date older (less than) or equal August 13th, 2021.

forfiles /D -10

This outputs all file system entries (file and directory names) with last modification date older (less than) or equal February 16th, 2022 on current date being February 26th, 2022 (26.02.2022).

forfiles /D 10
forfiles /D +10

This outputs all file system entries (file and directory names) with last modification date newer (greater than) or equal March 08th, 2022 on current date being February 26th, 2022 (26.02.2022). Such a date specification does not make much sense in most cases if the current date is correct and so files/folders with a future date do most likely not exist at all. So such a date specification results usually in the error message:

ERROR: No files found with the specified search criteria.

Some more hints:

  1. The file/folder names are always output enclosed in " which is important to know on using for example in a batch file:

    @echo off
    echo The following files are older than nine days:
    echo/
    for /F "delims=" %%I in ('%SystemRoot%\System32\forfiles.exe /D -10 2^>nul') do if not exist "%%~I\" echo     %%I
    
  2. The file/folder names are never output with path even on using option /S.
    So capturing the output of forfiles using for /F for further processing the files/folders more efficient with cmd.exe instead of using the FORFILES option /C makes no sense on using option /S to search also in subdirectories of the current directory respectively the directory specified with FORFILES option /P.

  3. The usage of FORFILES without option /D makes never sense as in this case it is always more efficient and definitely better to use the internal command for of the Windows Command Processor cmd.

  4. forfiles.exe does by default not exist on Windows XP and older Windows client versions. forfiles.exe in Windows system32 directory of Windows Server 2003 can be copied to the Windows system32 directory of a PC with Windows XP. Windows Server 2003 uses the same kernel as Windows XP.
    By the way: choice.exe and robocopy.exe in Windows system32 directory of Windows Server 2003 can be also copied to Windows system32 directory of Windows XP as not existing by default on Windows XP too.

PS: A directory is for file systems like NTFS, FAT12, FAT16, FAT32, exFAT a file with the attribute d.