<http://www.netikka.net/tsneti/info/tscmd125.htm>
Copyright © 2003-2012 by Prof. Timo Salmi  
Last modified Wed 21-Mar-2012 07:41:15

 
Assorted NT/2000/XP/.. CMD.EXE Script Tricks
From the html version of the tscmd.zip 1cmdfaq.txt file
To the Description and the Index
 

This page is edited from the 1cmdfaq.txt faq-file contained in my tscmd.zip command line interface (CLI) collection. That zipped file has much additional material, including a number of detached .cmd script files. It is recommended that you also get the zipped version as a companion.

Please see "The Description and the Index page" for the conditions of usage and other such information.



125} How do I get the age of a file? Or of the files in a folder?

Although the ingredients of this task already are covered in this FAQ (in particular see item #8, item #31 and item #45), let's revisit this frequent question. First consider the case of a single file:
  @echo off & setlocal enableextensions
  ::
  :: Usage

  if "%~1"=="" (
    echo Usage: %~0 ["Filename"]
    goto :EOF)
  ::
  :: Check for existence

  if not exist "%~1" (
    echo Target "%~1" not found
    goto :EOF)
  ::
  :: Temporary files

  set vbs_=%temp%\tmp$$$.vbs
  set tmp_=%temp%\tmp$$$.cmd
  ::
  :: Build a Visual Basic Script

  > "%vbs_%" echo Set FSO=CreateObject("Scripting.FileSystemObject")
  >>"%vbs_%" echo sysDate = Date
  >>"%vbs_%" echo Set fname = FSO.GetFile("%~1")
  >>"%vbs_%" echo age = DateDiff("d", fname.DateLastModified, SysDate)
  >>"%vbs_%" echo WScript.Echo "@set age_=" ^& age
  ::
  :: Run the script with Microsoft Windows Script Host Version 5.6

  cscript //nologo "%vbs_%" > "%tmp_%"
  ::
  :: Run the auxiliary cmd script made by vbs

  call "%tmp_%"
  ::
  :: Display the result

  echo The file "%~1"
  echo is %age_% days old
  ::
  :: Clean up

  for %%f in ("%tmp_%" "%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq cmdfaq2.cmd
  The file "cmdfaq2.cmd"
  is 14 days old

An aside. The above script fails if given an existing folder as the parameter instead of a proper file name. How to test for the type (folder or file) is covered in item #75 if you find it relevant to include that test.

Next consider traversing all the files in a folder
  @echo off & setlocal enableextensions
  ::
  :: Usage

  if "%~1"=="" (
    echo Usage: %~0 ["[Folder\]FileName"]
    echo Wilcards allowed in the filename
    goto :EOF)
  ::
  :: Traverse an entire folder

  if "%~2"=="recurse" goto _next
  for %%f in ("%~1") do call "%~0" "%%~f" recurse
  goto :EOF
  ::
  :_next
  ::
  :: Temporary files

  set vbs_=%temp%\tmp$$$.vbs
  set tmp_=%temp%\tmp$$$.cmd
  ::
  :: Build a Visual Basic Script

  > "%vbs_%" echo Set FSO=CreateObject("Scripting.FileSystemObject")
  >>"%vbs_%" echo sysDate = Date
  >>"%vbs_%" echo Set fname = FSO.GetFile("%~1")
  >>"%vbs_%" echo age = DateDiff("d", fname.DateLastModified, SysDate)
  >>"%vbs_%" echo WScript.Echo "@set age_=" ^& age
  ::
  :: Run the script with Microsoft Windows Script Host Version 5.6

  cscript //nologo "%vbs_%" > "%tmp_%"
  ::
  :: Run the auxiliary cmd script made by vbs

  call "%tmp_%"
  ::
  :: Display the result
  echo The file "%~1"
  echo is %age_% days old
  ::
  :: Clean up

  for %%f in ("%vbs_%" "%tmp_%") do if exist %%f del %%f
  endlocal & goto :EOF

The output might be e.g.
  C:\_D\TEST>cmdfaq "c:\_d\test\*.*"
  The file "c:\_d\test\BATFAQ.BAS"
  is 271 days old
  The file "c:\_d\test\BATFAQ.BAT"
  is 211 days old
  The file "c:\_d\test\BATFAQ2.BAT"
  is 214 days old
  The file "c:\_d\test\BATFAQ3.BAT"
  is 1080 days old
  The file "c:\_d\test\CMDFAQ.CMD"
  is 0 days old
  The file "c:\_d\test\CMDFAQ2.CMD"
  is 14 days old

Finally, what about pinpointing the files that are for example at least 250 days old? A small modification suffices:
  ::
  :: Display the result
  if %age_% GEQ 250 (
    echo The file "%~1"
    echo is %age_% days old
    )

It is trivial to see that you can put any operation in there. One very frequently asked questions is "How do I delete files older than x days with a command line script?". Just insert within the "if"
  del /p "%~1"
Also see about forfiles.exe in Item #8.

References/Comments:
  Google Groups: Remove files older than X
  Google Groups 19 Mar 2012 21:31:20