Link to home
Start Free TrialLog in
Avatar of aptnrickard
aptnrickardFlag for United States of America

asked on

Script to delete all files in a folder except locked files

I need to write a batch file (or script) that I can schedule to run on one of our servers each day at 6 AM.

The batch command I was looking at is:  del c:\windows\temp\*.* /f /s /q

This works except it errors out when it comes across a locked file.

I need it to skip the file and continue deleting the others.

I'm pretty sure it's possible but I can't figure out how.

Avatar of motnahp00
Flag of United States of America image

Link to home
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of aptnrickard


Thank you, I never would have even thought of using robocopy.

I did a couple of quick tries here and this looks to do exactly what I was looking for.  I need to test a touch more as this will go on a live server but I think it will work.

Robocopy is my favorite command line tool!

You're welcome.
Avatar of Steve Knight

cd /d c:\windows\temp
rd .  /s /q

That will try and delete all files and all dirs under c:\windows\temp - by being in the dir already it can't remove the temp directory itself, which is what you want.

Blimey that was a quick one, yes robocopy is useful for all sorts :-)
@dragon-it: Very impressive stats on MS-DOS!
Thanks, was where most windows scripting used to be posted rather than real MS-DOS mind, haven't touched that for a few years thankfully!

430,000 points this MONTH yourself too!?  That's some serious EE'ing!

I have high respect for command-line gurus. I try to do everything CL versus the GUI.

Thanks for kudos. Trying to break 500K before the end of this month. Very slim pickings today though.