Avatar of aptnrickard
aptnrickard
Flag 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.

Cheers
Windows BatchVB ScriptWindows Server 2008

Avatar of undefined
Last Comment
motnahp00

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
motnahp00

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
aptnrickard

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

Cheers
motnahp00

Robocopy is my favorite command line tool!

You're welcome.
Steve Knight

Try:

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.

Steve
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Steve Knight

Blimey that was a quick one, yes robocopy is useful for all sorts :-)
motnahp00

@dragon-it: Very impressive stats on MS-DOS!
Steve Knight

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!

Steve
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
motnahp00

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.

Jesse