Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have to write a batch file to delete the oldest file in a folder, but i don't know how to get the name of the oldest file.

I can sort files with DIR, DIR /B /O:D /T:C but this is a list and i need the first file (since it sorts the oldest first) in that list.

How can I do it?


share|improve this question
up vote 1 down vote accepted

That dredged up some painful memories! I found this one:

I won't cut and paste the script as there is a really nice write up there.

share|improve this answer

I think this should be possible with the forfiles command. Technet link. But it is not included in WinXP. There are however "Resource Kits" from Microsoft which include several Tools, forfiles is one of them. Try to google the "Windows 2000 Resource Kit", it includes forfiles IIRC.

share|improve this answer

would be sooo easy with gnu userspace apps for windows. :)

You might want to check it out if you have a possibility to install tools on the machine in question.

Sourceforge currently has some guru meditation issues, but hope the link works.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.