Bat File to Delete Hundreds of Files

burg716

Commendable
Jul 22, 2016
5
0
1,510
I am looking for advice on how to create a .bat file to delete hundreds of files. I have a single folder with hundreds of files and I want to delete a portion off them. I have a spreadsheet with a list of the names of the files I’d like to delete from that folder. Anybody know how to create a .bat to delete files with certain unique file names?
 
Solution
Hi, if you can convert the list of filenames to a plain text file (maybe copy/paste to notepad and save as list.txt) then you can use a batch file something like this:

@echo off
For /F %%A in (list.txt) do (
echo %%A
)


This batch file will just type out each line that's in the list.txt file. It expects the list.txt file to be there and have a list of filenames in it.

You can change the:
echo %%A
to:
del %%A
to delete files instead.

Test this in a temporary folder before you actually put it to use.

Make a full backup of the folder before you actually run it, just in case something goes wrong.

batch2.jpg

gardenman

Splendid
Moderator
Hi, if you can convert the list of filenames to a plain text file (maybe copy/paste to notepad and save as list.txt) then you can use a batch file something like this:

@echo off
For /F %%A in (list.txt) do (
echo %%A
)


This batch file will just type out each line that's in the list.txt file. It expects the list.txt file to be there and have a list of filenames in it.

You can change the:
echo %%A
to:
del %%A
to delete files instead.

Test this in a temporary folder before you actually put it to use.

Make a full backup of the folder before you actually run it, just in case something goes wrong.

batch2.jpg
 
Solution
If your stuff follows fairly basic patterns (such as a specific file extension) DIR can be used to generate the list easily.
You'll want to look it up for specific ways of formatting parameters, but if it's something simple like a partial filename or extension:

DIR /S /Q /B /A-d *.extension > %userprofile%/Desktop/delFileList.txt

Instead of things like partial filenames, say you have a bunch of photos:
"IMG_*.jpg" would log any file of a .jpg type starting with the text IMG_ in its filename.

This should output a list of just filenames w/o intervening folders (still paths though, it's just DIR likes listing things as such: D:\Folder and then the next items would be D:\Folder\img_1.jpg ... etc; this removes the intervening containing folders from the list). If you need to run multiple of these sorts of searches, since I don't think DIR works with a list of stuff, you can use >> to append to an existing file instead of creating/overwriting one.