Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for listfilesbetween.bat

(view source code of listfilesbetween.bat as plain text)

  1. @ECHO OFF
  2. :: Check Windows version
  3. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  4.  
  5. :: Exactly 3 mandatory command line arguments plus optional /S switch
  6. IF "%~3"=="" GOTO Syntax
  7. IF NOT "%~4"=="" IF /I NOT "%~4"=="/S" GOTO Syntax
  8.  
  9. :: Check if file or directory exists
  10. IF NOT EXIST "%~3" GOTO Syntax
  11.  
  12. :: Check if dates are valid for use with XCOPY's /D switch
  13. ECHO.%~1| FINDSTR /R /X /C:"[01]*[0-9][/\-][0-3]*[0-9][/\-][12][0-9][0-9][0-9]" >NUL || GOTO Syntax
  14. ECHO.%~2| FINDSTR /R /X /C:"[01]*[0-9][/\-][0-3]*[0-9][/\-][12][0-9][0-9][0-9]" >NUL || GOTO Syntax
  15.  
  16. :: Use a local environment
  17. SETLOCAL ENABLEDELAYEDEXPANSION
  18.  
  19. :: Check if file or folder
  20. SET Arg3=%~3
  21. SET Type=File
  22. ECHO.%~3 | FINDSTR /R /C:"[\?\*]" >NUL
  23. IF ERRORLEVEL 1 (
  24. 	IF "%~nx3"=="" (
  25. 		SET Type=Folder
  26. 	) ELSE (
  27. 		DIR /AD /B "%~dp3" | FINDSTR /R /I /X /C:"%~nx3" >NUL && SET Type=Folder
  28. 	)
  29. 	IF "%Arg3:~-1,1%"=="\" (
  30. 		SET Type=Folder
  31. 		SET Arg3=%Arg3:~0,-1%
  32. 	)
  33. 	IF EXIST "!Arg3!\" SET Type=Folder
  34. )
  35. IF /I "%Type%"=="Folder" SET Arg3=%Arg3%\*.*
  36.  
  37. :: List all files after and including TO date in a temporary file
  38. XCOPY "%Arg3%" C:\*.* /D:%~2 %4 /H /Y /L | FINDSTR /R /B /V /C:"[1-9][0-9]* " > "%Temp%.\~%~n0.tmp"
  39.  
  40. :: Now list all files older than and including FROM date, and only
  41. :: display its path if the file is NOT listed in the temporary file
  42. FOR /F "tokens=*" %%A IN ('XCOPY "%Arg3%" C:\*.* /D:%~1 %4 /H /Y /L ^| FINDSTR /R /B /V /C:"[1-9][0-9]* "') DO (
  43. 	SET Found=0
  44. 	FOR /F "tokens=*" %%B IN ('TYPE "%Temp%.\~%~n0.tmp"') DO (
  45. 		IF "%%~A"=="%%~B" SET Found=1
  46. 	)
  47. 	IF "!Found!"=="0" ECHO %%A
  48. )
  49.  
  50. :: Delete the temporary file
  51. IF EXIST "%Temp%.\~%~n0.tmp" DEL "%Temp%.\~%~n0.tmp"
  52.  
  53. ENDLOCAL
  54. GOTO:EOF
  55.  
  56.  
  57. :Syntax
  58. ECHO.
  59. ECHO ListFilesBetween.bat,  Version 1.02 for Windows NT4+
  60. ECHO List matching files last modified between two dates
  61. ECHO.
  62. ECHO Usage:  LISTFILESBETWEEN  from_date  to_date  file_spec  [ /S ]
  63. ECHO.
  64. ECHO Where:  from_date  is the earliest timestamp to be included
  65. ECHO.        to_date    is the day after the last timestamp to be included
  66. ECHO         file_spec  is the folder or file specification
  67. ECHO         /S         includes subfolders
  68. ECHO.
  69. ECHO Notes:  Both from_date and to_date must be in MM/DD/YYYY or MM-DD-YYYY format;
  70. ECHO         the order MM DD YYYY is fixed, the delimiter depends on the locale.
  71. ECHO         The file_spec must be a fully qualified path, i.e. drive:\path[\files]
  72. ECHO         or \\server\share\path[\files]; wildcards are allowed.
  73. ECHO         Files with timestamps equal to from_date are included, files with
  74. ECHO         timestamps equal to to_date are excluded; to include today's files
  75. ECHO         without having to calculate tomorrow's date, use a date in the far
  76. ECHO         future for to_date, e.g. 01/01/2100.
  77. ECHO         Matching hidden files are included.
  78. ECHO         Do not use /S switch on the root of the C: drive.
  79. ECHO.
  80. ECHO Written by Rob van der Woude
  81. ECHO http://www.robvanderwoude.com
  82.  
  83. :: Abort with errorlevel 1 without using EXIT
  84. IF "%OS%"=="Windows_NT" COLOR 00
  85.  

page last modified: 2024-04-16; loaded in 0.0254 seconds