Ekagra Srivastava
Ekagra Srivastava

Reputation: 201

Batch File- Code Not Working Properly?

I'm printing findstr results as a text file and deleting the files which are printed as a text file, but i am facing some issues !

My code is like:

@echo off
findstr /s /m "Trojan" "C:\*.*">>"C:\result.txt" 2>nul 
for %%i in (C:\result.txt) do del %%i

But, i am not able to delete the file(s) with the help of the code above, because it's not delteing file(s) which have spaces in their path

Example-

It is deleting "C:\Anything.exe" which has "Trojan" as a string

It is not deleting "C:\Anything xyz.exe" which has "Trojan" as a string

Please Help to optimize my code !!!

Upvotes: 1

Views: 44

Answers (1)

MC ND
MC ND

Reputation: 70951

for /f "delims=" %%i in (C:\result.txt) do del "%%i"

You need for /f to read the file, an empty delimiter list to avoid splitting the line on spaces while reading the file and proper quoting in the del command.

Upvotes: 2

Related Questions