Batch file findstr不使用包含破折号(-)的字符串?
我一直在开发一个BAT文件,它将根据创建日期删除旧文件。为此,我生成了所有文件和路径的列表,然后是要保护的文件名列表。然后使用FINDSTR从文件和路径列表中删除这些文件 这个系统工作得很好,直到我遇到一个带破折号的文件(或者看起来是这样!) 下面是一个例子: cleaner_protect.txt包含: cleaner_fullpath.txt包含: 我想从cleaner_fullpath.txt中删除cleaner_protect.txt中存储的新文件,留下旧文件,稍后我将删除这些文件(还不到该位lol)。以下是我目前的代码:Batch file findstr不使用包含破折号(-)的字符串?,batch-file,findstr,hyphen,Batch File,Findstr,Hyphen,我一直在开发一个BAT文件,它将根据创建日期删除旧文件。为此,我生成了所有文件和路径的列表,然后是要保护的文件名列表。然后使用FINDSTR从文件和路径列表中删除这些文件 这个系统工作得很好,直到我遇到一个带破折号的文件(或者看起来是这样!) 下面是一个例子: cleaner_protect.txt包含: cleaner_fullpath.txt包含: 我想从cleaner_fullpath.txt中删除cleaner_protect.txt中存储的新文件,留下旧文件,稍后我将删除这些文件(还不
:: Remove protected files from list to be deleted (fullpath)
:RemoveFile
:: load string into variable
set /p target= <cleaner_protect.txt
:: remove protected file from full path list
echo -----------------------------
echo Searching for: "%target%"
echo -----------------------------
pause
findstr /v ".\<%target%\>." cleaner_fullpath.txt > cleaner_temp.txt
echo -----------------------------
type cleaner_temp.txt
echo -----------------------------
pause
del cleaner_fullpath.txt
ren cleaner_temp.txt cleaner_fullpath.txt
:: Count remaining lines in list
Set target=cleaner_protect.txt
Set /a lines=0
For /f %%j in ('Find "" /v /c ^< %target%') Do Set /a lines=%%j
Echo %target% has %lines% lines.
pause
:: Loop until completed
IF %lines% GTR 0 (
:: Remove line from protected list
more +1 cleaner_protect.txt > cleaner_temp.txt
del cleaner_protect.txt
ren cleaner_temp.txt cleaner_protect.txt
set /a lines-=1
GOTO :RemoveFile
)
::从要删除的列表中删除受保护的文件(完整路径)
:RemoveFile
::将字符串加载到变量中
设置/p目标=cleaner_temp.txt
回音-----------------------------
键入cleaner_temp.txt
回音-----------------------------
暂停
del cleaner_fullpath.txt
ren cleaner_temp.txt cleaner_fullpath.txt
::统计列表中的剩余行
设置目标=cleaner\u protect.txt
设置/a行=0
对于('Find”“/v/c^<%target%')中的/f%%j,请设置/a行=%%j
回显%target%有%lines%行。
暂停
::循环直到完成
如果%行%GTR 0(
::从受保护列表中删除行
更多+1 cleaner_protect.txt>cleaner_temp.txt
del cleaner_protect.txt
ren cleaner_temp.txt cleaner_protect.txt
设置/a行-=1
转到:删除文件
)
暂停和回音用于调试目的。。。我想让它以几乎看不见的方式运行
有人能解释一下吗?我需要这段代码来反复检查dropbox并删除可能处于不同结构级别的旧文件。也许这行代码就可以满足您的所有需求:
findstr /E /V /G:cleaner_protect.txt cleaner_fullpath.txt > cleaner_temp.txt
样本输出:
P:\Old File.txt
我会这样做:
@echo关闭
setlocal EnableExtensions DisableDelayedExpansion
rem/*在“cleaner\u protect.txt”的每一行前面加上“\\\”,删除尾随空格,
rem(如果适用),并将结果写入“cleaner_temp.txt”:*/
>“清洁剂温度txt”(
对于/F“usebackq delims=eol=|”%%E in(“cleaner_protect.txt”)do(
设置“项=\\%%E”
setlocal EnableDelayedExpansion
回声!项目:^ |=!
端部
)
)
rem/*搜索“cleaner_fullpath.txt”,查找不以任何行结尾的行
rem`cleaner_temp.txt`并仅处理返回的项目:*/
对于/F“delims=eol=|”%%F英寸('
findstr/V/I/E/L/G:“cleaner_temp.txt”“cleaner_fullpath.txt”
""做"(
回音删除“%%F”
)
rem//清理临时文件“cleaner\u temp.txt”:
删除“清洁剂温度txt”
端部
退出/B
测试脚本后,删除大写的ECHO
命令。
假设cleaner\u protect.txt
包含以下内容:
New File.txt
新文件-Copy.txt
临时文件cleaner_temp.txt
将包含以下内容:
\\New File.txt
\\新文件-Copy.txt
因此,使用文本文件cleaner\u fullpath.txt
:
P:\New File.txt
P:\New File-Copy.txt
P:\Old File.txt
仅处理以下项目:
P:\Old File.txt
前导的\
被findstr
视为一个文本\
(因为它使用\
作为转义字符)
前缀\\
的实现也是为了涵盖以下情况:
cleaner\u protect.txt
包含以下内容:
New File.txt
而cleaner_fullpath.txt
包含以下内容:
P:\New File.txt
P:\Very New File.txt
如果没有前缀,p:\Very New File.txt
也会在结尾匹配New File.txt
,因此不会被错误删除cleaner\u protect.txt
包含以下内容:
.File.txt
而cleaner_fullpath.txt
包含以下内容:
P:\.File.txt
P:\Other.File.txt
使用\
前缀,p:\Other.File.txt
也会在末尾匹配\.File.txt
,因为
是
的元字符(即使文本搜索字符串是由/L
定义的,但像\>一样转义。
仍然适用并导致文本
),因此,它也不会被错误地删除。但是,使用\
前缀,转义应用于第二个\
,因此结果是文本\
;
不需要使用/L
选项进行转义批处理的哪个部分失败了,你得到了什么,你期望得到什么?我会尝试使用
findstr/v/r/c:“\”
@jeb它似乎在findstr/v.\”失败。当%target%
包含破折号时,cleaner\u fullpath.txt>cleaner\u temp.txt
。如果%target%
包含New File.txt
,则该过程工作正常。但是,如果%target%
包含新文件-Copy.txt
,则输出cleaner_temp.txt为空且不包含任何数据,这将覆盖cleaner_fullpath.txt文件。我希望输出cleaner_temp.txt(因此cleaner_fullpath.txt)将目标字符串从文件中删除。@MCND当我添加/r/c
开关时,命令行在到达目标字符串(即文件名)时回显FINDSTR:/c ignored
它包含破折号,但仍然生成相同的空白输出文本文件。@Domenic,/v/r/c:“regularexpression”
是否包含冒号?哇。。。因此,这个解决方案在我的示例文件上运行得非常好,但是在我的实际文件中根本无法正常工作。多么令人沮丧!FINDSTR似乎生成了一个与原始文件匹配的输出文件(没有任何内容被删除)。我已经上传了在我的一些真实文件上运行代码时生成的文本文件,你能看到这不起作用的原因吗?这些文件是
findstr /E /V /G:cleaner_protect.txt cleaner_fullpath.txt > cleaner_temp.txt
P:\Old File.txt