Jellicle
Jellicle

Reputation: 30206

Windows batch: create timestamp from string

I want to delete any files that are older than a certain, arbitrary date (which date I wish to designate manually in my code).

How can I get the timestamps of the files concerned and compare them with my arbitrary date? I know I can get the timestamps with something like:

for %%f in (./*) do echo %%~tf

...and I can compare strings or numbers with gtr and lss, but I can't very well compare the timestamps as strings (because they'd be compared alphabetically instead of chronologically).

Upvotes: 1

Views: 277

Answers (1)

dbenham
dbenham

Reputation: 130819

Use FORFILES with the /D option. (type FORFILES /? from a command prompt for help).

For example, the following will delete all files from the current directory that have not been modified since March 1, 2011:

forfiles /d -3/1/2011 /c "cmd /c if @isdir==FALSE del @file"

It is possible to work with dates returned by %%~tf, but it is a royal pain; especially if you want your solution to work on multiple machines, possibly with different date format settings.

Upvotes: 1

Related Questions