How to delete backup files in a batch based on file name

0

I have created a batch file that is run on several different PCs in our organization. The batch backs up the user files and saves it on a server with the folder name being made up of the computer name and the date of the backup.

set foldername=%COMPUTERNAME%
set folderdate=%date:~10,4%_%date:~4,2%_%date:~7,2%
set folder=%foldername%.%folderdate%

Now I need to write another script to be run on the storage server to delete files older than 7 days old. It can't be done based on the date stamp of the folder because several of the computers are passing the actual date the users folder was created instead of the date the backup was run. For this reason the batch file will have to extract the date from the folder name and then calculate to see if the folder is >=7 days.

Any help would be appreciated.

TX_Balloon_Grl

Posted 2019-05-02T20:49:43.213

Reputation: 11

1Batch isn't good in doing date calculations, I guess your folderdate is formatted yyyyMMdd ? I suggest to use a script language that has a datetime variable type like vbscript/PowerShell.# – LotPings – 2019-05-02T21:21:14.110

@LotPings the folders are dated yyyy_mm_dd. I have seen other batch programs that did great math. I just need to figure out how to strip the date. – TX_Balloon_Grl – 2019-05-02T21:27:54.897

As the date is attached with a dot it essentially is an extension, so iterate the folder and get the extension wiith the ~x tilde modifier (see for /?) may further split the extension at the underscores into year,month,day components with a for /f but that still leaves the subtraction of 7 days from the given day what returns a negative number for today and then you need to know how many days the previous month has to corrected the result. While this is possibly and has be done, it IS clumsy compared to calculations with an integrated datetime variable type. – LotPings – 2019-05-02T22:29:07.250

I know how to extract the date from the folder name. I also know how to take the current date and subtract the 7 days. What I can't figure out (after much research and studying for /? until I'm blue in the face) is how to do the for or forfiles syntax to compare the 2. If this were in Basic instead of in DOS, I'd have been done weeks ago. – TX_Balloon_Grl – 2019-05-06T19:03:35.643

Thanks for your assistance @LotPings. I finally got the job handled by using a VBScript. – TX_Balloon_Grl – 2019-05-15T20:53:43.730

No answers