简体   繁体   English

批处理 - 在文本文件中查找字符串并删除整行

[英]Batch - Find string in text file and delete full line

I use the script from this Thread accepted answer from Mofi .我使用了这个 Thread 中的脚本接受了 Mofi 的答案 The script copy folders and store them in text file to exclude once copied folders on next run.脚本复制文件夹并将它们存储在文本文件中,以在下次运行时排除一次复制的文件夹。

Sometimes I have folders called [incomplete]-different.names and I do not want to copy this folders.有时我有名为[incomplete]-different.names文件夹,我不想复制这些文件夹。 I want that all folders with the string [incomplete]- and the name behind are skipped or are not even written in the text file %CurrentList% for further processing.我希望所有带有字符串[incomplete]-和后面的名称的文件夹都被跳过或什至不写入文本文件%CurrentList%以供进一步处理。

These are my previous attempts but so far I could not get up and running with the script from the top.这些是我之前的尝试,但到目前为止我无法从顶部启动并运行脚本。 Help would be nice, and thanks in advance.帮助会很好,并提前致谢。

Try 1:尝试 1:

for /f "delims=" [incomplete]-%%D in ("%CurrentList%") do ( set str=%%D set str=!str: =! set str=!str: %%D =! echo !str!

Try 2:尝试2:

findstr /v /b /c:"[incomplete]-"%%D" "%CurrentList%" del "%%D"

You were really close with your second attempt.您的第二次尝试非常接近。

If all you want is to delete lines in %CurrentList% that contain the string [incomplete]- , you can just direct the output of a findstr /v to a temp file and then overwrite CurrentList with that file.如果您只想删除 %CurrentList% 中包含字符串[incomplete]- ,您可以将findstr /v的输出定向到临时文件,然后用该文件覆盖 CurrentList。

findstr /v /c:"[incomplete]-" "%CurrentList%" >tmpList.txt
move /y tmpList.txt "%CurrentList%" >nul

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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