1.

Solve : how to delete files with certain string in them??

Answer»

Hi, I am a new user with very limited DOS experience.

I figured out how to find and display filenames with a certain string in them ie: findstr /i /m test .*.

If I would like to delete all those files in one go from that directory, what are the options, do I need to write a .BAT FILE (don't really know how to do that and how to address that particular directory)
There is many holes in my Computer/Windows/DOS knowledge, so I apologize and hope you are patient with me.
thanks, EdgarAny experience in QBasic? Interrupts can do it in DOS.

In Dos you can pipe the filenames to a file, but what are you really trying to do?

Sounds a little strange that you want to delete files with a certain string in them.

Ted

PS: Just because you delete a file, could still hold the data on the disk until overwritten.
Thanks Ted,
no, no experience in QBasic, and don't know what interrupts are.

I don't really want to pipe the filenames to a file, but I want to delete the files I FOUND with "findstr" out of that directory in one go, without having to do delete every file by itself.

thanks, Edgar

Code: [Select]for /f "tokens=*" %%a in ('findstr /I /M "search string" *txt') do (
echo %%a
rem your removal command here...
)
thanks ghostdog,

I gather this is the code for a BATCH file? I am such a noob...
Is this all it takes, then you just run this batch file in that directory?
is the "rem your removal command here" needed or is it just a remark?

edgar1. Yes, that is the code
2. You probably just need to leave it like that (except for the search strings)
3. The rem specifies a remark; but your code that deletes files there (such as del %%a)So does the code go:

or /f "tokens=*" %%a in ('findstr /I /M "search string" *txt') do (
echo %%a
rem delete all files with search string in them
del %%a

)You don't need (but it still works with) either echo %%a or the rem line: echo just displays %%a (which is deleted) and rem is just a remark and doesn't get printed (only viewable while editing your batch and therefore only needed if you want to keep notes).

But otherwise it's all good.Hi guys,
thanks it worked! I used:
or /f "tokens=*" %%a in ('findstr /I /M "search string" *txt') do (
echo %%a
rem delete all files with search string in them
del %%a

)

and it did the trick. Now, two observations. On filenames with spaces in them it did not work. If the file has a different suffix than .txt it did not work (I suppose thats why *txt is in the code)

thanks for all your help, great forum.
edgarQuote from: edgar on October 10, 2007, 06:05:33 PM

On filenames with spaces in them it did not work.

using quotes thus: del "%%a" is the standard way around that.

Quote from: edgar on October 10, 2007, 06:05:33 PM
If the file has a different suffix than .txt it did not work (I suppose thats why *txt is in the code)

In fact, in the code as shown in your post, the wildcard search (*txt) will take place on all files whose name or extension ENDS in the three letters "txt",

so it would work on all these files

prices.txt
process.etxt
protetxt

NOT just files whose extension is .txt - for that the wildcard filespec would be *.txt - see the dot?

If you wanted to search all files, the wildcard filespec would be *.*


Discussion

No Comment Found