kenp
kenp

Reputation: 500

Windows Cmd : Delete files from a folder

I have a text file that contains files names (pictures) in each line:

Pic1.png
Pic2.jpg
Pic3.png
Pic4.gif

Is there a batch command to fetch all those file names from the .txt file and delete them?

I tried this : for /f "delims=" %%f in (files.txt) do del "%%f" but i have error : %%f was unexpected

Thanks

Upvotes: 0

Views: 407

Answers (3)

Jean-François Fabre
Jean-François Fabre

Reputation: 140276

The /F option of for means that it will read the file.txt argument and affect the a variable to each token of the file. No need to specify delimiter when there's one item per line or separated by blanks.

in a .bat file you have to double the '%' chars

for /F %%a in (file.txt) do del /f /q %%a

From command line (as stated in the above answer), just remove the extra '%' or you'll have %%a was unexpected error that you reported.

for /F %a in (file.txt) do del /f /q %a

Upvotes: 1

Hackoo
Hackoo

Reputation: 18847

You can do something like this with a batch file :

@echo off
for /f "delims=" %%a in ('Type files.txt') Do (Del /F /Q "%%a")
pause

On command line you should do like this :

for /f "delims=" %a in ('Type files.txt') Do (Del /F /Q "%a")

Upvotes: 1

sanjeevjha
sanjeevjha

Reputation: 1549

while read -r file; do rm -- "$file"; done < a.txt

where a.txt is file name where all the file to be deleted is listed

suppose a.txt contains is

A.java

B.java

C.java

then if you execute below command then all the files listed in a.txt file will be deleted

while read -r file; do rm -- "$file"; done < a.txt

Upvotes: 0

Related Questions