V
V
Verbiage2020-10-03 09:32:03
Command line
Verbiage, 2020-10-03 09:32:03

How to write a batch file that deletes files that have a line with the given content in the given directory?

Hello, I need your help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Eugene, 2020-10-03
@yellowmew

help: https://ss64.com/nt/
hint from KO:
you will need a command that searches for lines in files (one of)
you will need a command that executes another command for all the results of the third command
you will need a command that deletes files ( haha, sorry).
Or take powershell (stop digging out the stewardess torturing cmd )
Both scripts will turn out to be quite simple, both on Posh and on cmd

R
res2001, 2020-10-03
@res2001

More or less like this:

@echo off
chcp 1251 1>nul
set "find_str=search string"
set "search_dir=c:\temp"
for %%a in (%search_dir%) do (
   findstr /C:"%find_str%" "%%~a" 1>nul 2>&1 && del /f /q "%%~a"
)

The batch file bypasses all files in the directory specified by search_dir, in each file it looks for the line contained in find_str, if the line is found, it deletes the file.
Batnik did not test. First, practice on test files.
The text in the files must be encoded in cp1251. If the search text is English, then the encoding is not so important in principle.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question