1

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).

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.