0

I have a requirement to delete all the files older than 30 days except the files created on the first of every month in Windows 2008 R2. The files gets created inside the folder on a daily basis and contains a date tag attached to them. I can easily delete/move all the files older than 30 days using "forall" and "robocopy" respectively but I do not know how to exclude the files created on the first of every month from deletion.

  • 1
    possible duplicate of [Batch file to delete files older than N days](http://stackoverflow.com/questions/51054/batch-file-to-delete-files-older-than-n-days) – Endoro Oct 02 '13 at 10:45
  • 1
    Please supply a sample filename or two with the date tag and date format. – foxidrive Oct 02 '13 at 11:16
  • The simplest solution would probably be to first move those files out (with a for loop checking for only files with a day of 1), run the cleanup, then move them back. That is my suggestion without having any specifics to the file structure. – David Ruhmann Oct 02 '13 at 13:50
  • The file names are like - CIL2128_A_18_FULL_20130901_2335.csv, CIL2128_A_18_FULL_20130902_2332.csv, CIL2128_A_18_FULL_20130903_2336.csv, CIL2128_A_18_FULL_20130904_2335.csv. So need to exclude the file CIL2128_A_18_FULL_20130901_2335.csv from deletion – user2838203 Oct 02 '13 at 13:52
  • Hi David, can you please write the loop for moving only the files with the date tag of "01" to a separate location as I'm not a Programmer but an Admin. Thanks – user2838203 Oct 02 '13 at 17:13

1 Answers1

0

Having the almost same problem, I'm posting my solution adapted to your case.

Hope it helps someone in the future.

Many thanks to David Ruhmann above, that's pretty much his answer. Sorry I can't yet mod him up...

REM -- moving 1st of month folders to temp\
forfiles  /M *_FULL_20*01_*.csv  /C "cmd /c move @path temp\ "

REM -- deleting files older than 30 days
forfiles  /M *_FULL_20*.csv  /D -30   /C "cmd /c del /s /q @path "

REM -- bringing back temp\ to this folder
forfiles  /P temp\  /C "cmd /c move @path ..\ "

I translated my answer for files. To delete folders, use "rmdir" instead of "del"

Yow
  • 63
  • 8