0

I am using below script to append today's date and move files to an archive directory in following format:

XX-XXXX__2019-04-01.xlsx
XX-XXXX__2019-04-01.txt

Where XX-XXXX is a variable filename and 2019-04-01 in this example is today's date. I would now like to expand the archiving process to check the current date and also delete files older than 2 days - i.e. from 30 March and older. Any suggestions on how this can be achieved will be greatly appreciated, thanks!

    @echo off

    for /f "delims=" %%a in ('wmic OS Get localdatetime ^| find "."') do set DateTime=%%a

    set Yr=%DateTime:~0,4%
    set Mon=%DateTime:~4,2%
    set Day=%DateTime:~6,2%
    set Hr=%DateTime:~8,2%
    set Min=%DateTime:~10,2%

    for %%I in ("C:\Temp\Test_File\*.xlsx") do move "%%~I" "C:\Temp\Test_File\Archive\Processed\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
    for %%I in ("C:\Temp\Test_File\*.txt") do move "%%~I" "C:\Temp\Test_File\Archive\Logs\%%~nI__%Yr%-%Mon%-%Day%%%~xI"
aschipfl
  • 33,626
  • 12
  • 54
  • 99
Aligator3000
  • 335
  • 3
  • 17
  • 1
    datetime calculations/comparisons are much easier done in script languages with [datetime] variable type like j-/vbsript/PowerShell. Is that in option for you? Also for your file names, can there be underscores in the part before the date? –  Apr 01 '19 at 21:15
  • 1
    Can you use the Last Modified date of the file to determine how old it is, or do you have to check against the filename? If you can check to see how old the file actually is, try the answer from https://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days?rq=1 – SomethingDark Apr 02 '19 at 00:34
  • 1
    @SomethingDark: I would rather check against the date in the filename as it's a true reflection of when the file was actually archived. Last modified date will not necessarily be the same as datestamp in the filename. – Aligator3000 Apr 02 '19 at 09:25

0 Answers0