<http://www.netikka.net/tsneti/info/tscmd045.htm>
Copyright © 2003-2010 by Prof. Timo Salmi  
Last modified Sun 25-Apr-2010 08:06:04

 
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.



45} How can I find out how many days old a file is?

The FILEAGE.CMD script accompanying tscmd.zip has been written for that purpose. An example of the output
C:\_F\CMD>echo %date%
11.04.2008

C:\_F\CMD>fileage fileage.cmd
+----------------------------------------------------+
| FILEAGE.CMD script to find out how old a file is   |
| By Prof. Timo Salmi, Last modified Sat 22-Dec-2007 |
+----------------------------------------------------+

22.12.2007 13:53 2471 C:\_F\CMD\FILEAGE.CMD
The file is 111 days old

or

  @echo off & setlocal enableextensions
  ::
  :: The target file

  set filename_=%~1
  if not exist "%filename_%" (
    echo File "%filename_%" not found
    goto :EOF)
  ::
  :: Get filedate. Assume a file date format of DD.MM.YYYY HH:MN

  set fdate_=%~t1
  set fdd_=%fdate_:~0,2%
  set fmm_=%fdate_:~3,2%
  set fyyyy_=%fdate_:~6,4%
  ::
  echo %fdd_%.%fmm_%.%fyyyy_% "%filename_%"
  ::
  >"%temp%\tmp$$$.vbs" echo WScript.Echo DateDiff("d", DateValue("%fdd_%, %fmm_%, %fyyyy_%"), Date)
  for /f %%a in ('cscript //nologo "%temp%\tmp$$$.vbs"') do set ago_=%%a
  for %%f in ("%temp%\tmp$$$.vbs") do if exist %%f del %%f
  ::
  echo The file is %ago_% days old
  ::
  endlocal & goto :EOF

C:\_D\TEST>date /t
11.04.2008

C:\_D\TEST>cmdfaq "CMDFAQ2.CMD"
07.04.2008 "CMDFAQ2.CMD"
The file is 4 days old

Or, writing it out here in a locale date-format independent format
  @echo off & setlocal enableextensions disabledelayedexpansion
  ::
  :: The target file

  set filename_=%~1
  if not exist "%filename_%" (
    echo File "%filename_%" not found
    goto :EOF)
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  :: Run the VBS script with Microsoft Windows Script Host Version 5.6
  for /f "tokens=*" %%a in ('
    cscript //nologo "%vbs_%" "%filename_%"') do (
      set ago_=%%a)
  ::
  :: Show the results

  echo "%filename_%" is %ago_% days old
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
  '
  ' Get the age of a file in days

  Function FileAgeFn(filename) 'VBS
    Dim fso, f 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFile(filename) 'VBS
    FileAgeFn = DateDiff("d", f.DateLastModified, currentdate) 'VBS
  End Function 'VBS
  '
  ' Display the age of a file

  Sub DisplayFileAge(filename) 'VBS
    WScript.Echo FileAgeFn(filename) 'VBS
  End Sub 'VBS
  '
  ' The VBS main program

  currentdate = Date 'VBS
  set arg = WScript.Arguments 'VBS
  DisplayFileAge(arg(0)) 'VBS

C:\_D\TEST>date /t
09.10.2008

C:\_D\TEST>cmdfaq "My test file.txt"
05.09.2008  20:03                51 My test file.txt
"My test file.txt" is 34 days old

To get the ages of all the files in a folder
  @echo off & setlocal enableextensions disabledelayedexpansion
  ::
  :: Set the target folder

  set foldername_=C:\_D\BAS
  if not exist "%foldername_%\" (
    echo Folder "%filename_%" not found
    goto :EOF)
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  :: Run the VBS script with Microsoft Windows Script Host Version 5.6
  cscript //nologo "%vbs_%" "%foldername_%"
  ::
  :: Clean up

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

  Function PadLeftFn(Str,Places) 'VBS
    Dim s 'VBS
    For i = 1 To Places-1 'VBS
      s = s & " " 'VBS
    Next 'VBS
    PadLeftFn = Mid(s & Str, Len(Str), Places) 'VBS
  End Function 'VBS

  Function FileAgeFn(filename) 'VBS
    Dim fso, f 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFile(filename) 'VBS
    FileAgeFn = DateDiff("d", f.DateLastModified, currentdate) 'VBS
  End Function 'VBS

  Sub TraverseOneFolder(foldername) 'VBS
  Dim fso, f, f1, fc 'VBS
  Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
  Set f = fso.GetFolder(foldername) 'VBS
  Set fc = f.Files 'VBS
  For Each f1 in fc 'VBS
    Wscript.Echo PadLeftFn(FileAgeFn(f1),6) & " " & f1 'VBS
  Next 'VBS
  End Sub 'VBS
 
  ' The VBS main program
  currentdate = Date 'VBS
  set arg = WScript.Arguments 'VBS
  TraverseOneFolder(arg(0)) 'VBS

The output displaying the files' ages could be e.g.
C:\_D\BAS>cmdfaq
  1328 C:\_D\BAS\BATFAQ.BAS
  2137 C:\_D\BAS\BATFAQ3.BAT
     0 C:\_D\BAS\CMDFAQ.CMD
  1400 C:\_D\BAS\FAQPAS.PAS
    34 C:\_D\BAS\My test file.txt
   165 C:\_D\BAS\My test file2.txt
   184 C:\_D\BAS\Test & (5!).txt
  1081 C:\_D\BAS\TEST.VBS

What if we wish to traverse the subfolders as well?
  @echo off & setlocal enableextensions disabledelayedexpansion
  ::
  :: Set the target parent folder

  set foldername_=C:\_D
  if not exist "%foldername_%\" (
    echo Folder "%filename_%" not found
    goto :EOF)
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  :: Run the VBS script with Microsoft Windows Script Host Version 5.6
  cscript //nologo "%vbs_%" "%foldername_%"
  ::
  :: Clean up

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

  Function PadLeftFn(Str,Places) 'VBS
    Dim s 'VBS
    For i = 1 To Places-1 'VBS
      s = s & " " 'VBS
    Next 'VBS
    PadLeftFn = Mid(s & Str, Len(Str), Places) 'VBS
  End Function 'VBS

  Function FileAgeFn(filename) 'VBS
    Dim fso, f 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFile(filename) 'VBS
    FileAgeFn = DateDiff("d", f.DateLastModified, currentdate) 'VBS
  End Function 'VBS

  Sub TraverseOneFolder(foldername) 'VBS
    Dim fso, f, f1, fc 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFolder(foldername) 'VBS
    Set fc = f.Files 'VBS
    For Each f1 in fc 'VBS
      Wscript.Echo PadLeftFn(FileAgeFn(f1),6) & " " & f1 'VBS
    Next 'VBS
  End Sub 'VBS

  Sub TraverseManyFolders(startfoldername) 'VBS
    Dim fso, f, f1, sf 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFolder(startfoldername) 'VBS
    If Instr (1, Ucase(f), "RECYCLER") = 0 Then 'VBS
      TraverseOneFolder(f) 'VBS
    End If 'VBS
    Set sf = f.SubFolders 'VBS
    For Each f1 in sf 'VBS
      If Instr (1, Ucase(f1), "RECYCLER") = 0 Then 'VBS
        TraverseManyFolders(f1) 'VBS
      End If 'VBS
    Next 'VBS
  End Sub 'VBS

  ' The VBS main program
  currentdate = Date 'VBS
  set arg = WScript.Arguments 'VBS
  TraverseManyFolders(arg(0)) 'VBS

The output could be e.g.
C:\_D\BAS>cmdfaq|sort
     0 C:\_D\BAS\CMDFAQ.CMD
     0 C:\_D\BAS\CMDFAQ2.CMD
     1 C:\_D\ACCOUNTS\2008\TOP0484A.PGP
     1 C:\_D\ACCOUNTS\APUTILI.PGP
     1 C:\_D\TEACHING\JOTT\MAILNEWS\JOTT.MAI
     1 C:\_D\TEACHING\JOTT\TUL\JOTTOS.XLS
     :
  8418 C:\_D\QL\BASPROG\GRAPDEMO.BAS
  8418 C:\_D\QL\BASPROG\TURTDEMO.BAS

Let's see which of my files in and below C:\_D are more than 8000 days old:
  @echo off & setlocal enableextensions disabledelayedexpansion
  ::
  :: Set the target parent folder

  set foldername_=C:\_D
  if not exist "%foldername_%\" (
    echo Folder "%filename_%" not found
    goto :EOF)
  ::
  :: Build a Visual Basic Script

  set skip=
  set vbs_=%temp%\tmp$$$.vbs
  >"%vbs_%" findstr "'%skip%VBS" "%~f0"
  :: Run the VBS script with Microsoft Windows Script Host Version 5.6
  cscript //nologo "%vbs_%" "%foldername_%" 8000
  ::
  :: Clean up

  for %%f in ("%vbs_%") do if exist %%f del %%f
  endlocal & goto :EOF
 
  Function PadLeftFn(Str,Places) 'VBS
    Dim s 'VBS
    For i = 1 To Places-1 'VBS
      s = s & " " 'VBS
    Next 'VBS
    PadLeftFn = Mid(s & Str, Len(Str), Places) 'VBS
  End Function 'VBS

  Function FileAgeFn(filename) 'VBS
    Dim fso, f 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFile(filename) 'VBS
    FileAgeFn = DateDiff("d", f.DateLastModified, currentdate) 'VBS
  End Function 'VBS

  Function TraverseOneFolder(foldername) 'VBS
    Dim fso, f, f1, fc 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFolder(foldername) 'VBS
    Set fc = f.Files 'VBS
    For Each f1 in fc 'VBS
      age = FileAgeFn(f1) 'VBS
      If age > atleastdays Then 'VBS
        Wscript.Echo PadLeftFn(age,6) & " " & f1 'VBS
      End If 'VBS
    Next 'VBS
  End Function 'VBS

  Function TraverseManyFolders(startfoldername) 'VBS
    Dim fso, f, f1, sf 'VBS
    Set fso = CreateObject("Scripting.FileSystemObject") 'VBS
    Set f = fso.GetFolder(startfoldername) 'VBS
    If Instr (1, Ucase(f), "RECYCLER") = 0 Then 'VBS
      TraverseOneFolder(f) 'VBS
    End If 'VBS
    Set sf = f.SubFolders 'VBS
    For Each f1 in sf 'VBS
      If Instr (1, Ucase(f1), "RECYCLER") = 0 Then 'VBS
        TraverseManyFolders(f1) 'VBS
      End If 'VBS
    Next 'VBS
  End Function 'VBS

  ' The VBS main program
  currentdate = Date 'VBS
  set arg = WScript.Arguments 'VBS
  atleastdays = CLng(arg(1)) 'VBS
  TraverseManyFolders(arg(0)) 'VBS

C:\_D\BAS>cmdfaq
  8269 C:\_D\QL\BASPROG\COLOUR.BAS
  8418 C:\_D\QL\BASPROG\GRAPDEMO.BAS
  8389 C:\_D\QL\BASPROG\PIANO.BAS
  8418 C:\_D\QL\BASPROG\TURTDEMO.BAS
  8292 C:\_D\QL\PAS\BEEPTEST.PAS