MS-DOS script to delete files

marcalfa1 used Ask the Experts™
I have a text file which contains a list of filenames to be deleted in a specific folder on my Hard Drive.  Is there a way I can write a script to look in my text file and delete all the files on my drive specified within my text file.

I'm new to scripting and hoping this makes sense.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Hello marcalfa1,

Try :

for /f %i in (list.txt) do del "%i"
for /f %%i in (list.txt) do del "%%i"

Open in new window

Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015
Almost correct, leakim. You should say that the first line is for using on command line, while the second one is used in a batch file.
And we should add something which ignores spaces in path and file names when reading from the text file:

for /f "tokens=*" %i in (list.txt) do @del "%i"
or (in a batch file):
@echo offfor /f "tokens=*" %%i in (list.txt) do del "%%i"

Not able to improve on qlemo's comment, I can only add the following:

You could (and perhaps, should) depending on your application, add command line switches to the DEL command such as the following:

   @DEL /F /Q "%i"


   DEL /F /Q "%%i"

The /F will force deletion of read-only files. /Q can be useful when using wildcards - it suppresses the prompt for confirmation.

NOTE: If you type DEL /? in a DOS box you can see other switches in particular, attributes where you may need to delete files based on their attributes.

By the way, I personally find the variable names %i and %%i irritatingly misused. The letter 'i' is generally used where you are incrementing (or decrementing) say, as part of a FOR loop counter or as an index when referencing arrays.

As the first available variable is 'a', and it's designation is general, why not just use %a or %%a instead? It seems to make sense.

I seem to have said a lot more than I initially intended !!!
Qlemo"Batchelor", Developer and EE Topic Advisor
Top Expert 2015

I usually use the first uppercase letter of the stuff I expect from the file, in this case it are paths and filenames, so my choice would be %%F or %%P.

The del commandline switches are a good point, I've thought about them but had posted already.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial