29

i have a folder with 2K+ files in it, i need to delete around 200, i have a txt file with all the file names i need removed ordered in a list, how do i remove the specific files from the folder using the list? (OS is windows 7)

Hennes
  • 65,804
  • 7
  • 115
  • 169
Avishking
  • 293

6 Answers6

48

Type this on the command line, substituting your file for files_to_delete.txt:

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

A version of this suitable to include in .cmd files (double %%) and able to deal with spaces in file names:

for /f "delims=" %%f in (files_to_delete.txt) do del "%%f"
25

Using PowerShell:

Get-Content c:\path\to\list.txt | Remove-Item
Siim K
  • 8,062
20

Simple way is copy the txt file to a file called mydel.bat in the directory of the files to delete. Using an editor like Microsoft Word edit this file. Do a global replace on Newline normally ^p in Word. Replace it with space/f^pdelspace. This will change

File1.bin
File20.bin
File21.bin

to (with /f for "force delete read-only files"):

File1.bin /f
del File20.bin /f
del File21.bin /f
del

Edit the fist line to add the del space and delete the last line.

Run the batch command.

Arjan
  • 31,511
kingchris
  • 638
2

First method works after some changes:

  1. open Notepad
  2. copy all file names with extension which need to be deleted after adding del at the beginning like

    del File1.bin
    del File20.bin
    del File21.bin
    
  3. save the file as xyz.bat in the same folder

  4. run the file
Arjan
  • 31,511
Hassan
  • 21
1

I imagine it can be done with powershell.

Knowing Perl, I tend to use it for this sort of thing

perl -l -n -e "unlink" filenames.txt
0

"for /f", as shown in many posts above and/or using a batch (.bat) script of any kind, are the best methods for large file deletion processing, however, if you have a smaller list (or you want a really long command), you can use a space delimited list with the del command directly from a command prompt by doing something like this:

C:\myfolder> del File1.bin File20.bin File21.bin
Matt
  • 1