简体   繁体   中英

Delete System Files containing string

I am trying to write a batch file that will examine a given directory, read each file for a given string "Example" and then delete any files that contain the string. The files are also System Files so I don't know what the exact extension is or if that matters (maybe you can just omit a file type filter and have it read all files?). Some of the files will be locked from reading as well so it needs to handle access denial errors if that occurs, not sure how batch files handle that.

Update What I ended up using was this:

for /f "eol=: delims=" %F in ('findstr /m monkey *.txt') do del "%F"

Found here: Search and then delete depending on whether files contain a string

I ended up finding it shortly after I posted this.

Try something like this:

@echo off

cd "C:\some\where"
for /f %%f in ('dir /b /a:-d *') do (
  findstr "Example" "%%~ff" && (
    attrib -r -s -h "%%~ff"
    del /q "%%~ff"
  )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM