Windows, remove files that contain ‘zxy’

I recently had a problem trying to delete a few thousand files off a windows server, which all contained a common string. Windows search and delete just couldn’t do it without crashing, so after some searching I managed to get the script bellow up and running.
To use, copy the text and save in a .bat file, double click to run, and once complete the window will close.
It will look through the current directory and all of the child directories, deleting any matches that it finds, so place the file wisely!

@echo off
setLocal EnableDelayedExpansion
for /f “tokens=* delims= ” %%A in (‘dir/b/s *.txt’) do (
find /i “delivery failure” < %%A > nul
if not errorlevel 1 del %%A
)
This particular version will look in all files that end in “.txt” and contain the words “delivery falure”.
I am sure there are many better ways to do the same task, however this worked ok for me…
With that said, please only use if you know what your doing, I am not a windows programmer, so anything could happen!

Technology enthusiastic with many ongoing online projects one of which is this personal blog PingBin. While also working full time within a data center designing and maintaining the network infrastructure.

Leave a reply:

Your email address will not be published.

Site Footer