我破坏了一些代码,它们应该删除没有名称以keep值开头的文件.我通过将文件的名称放在tmpL1和tmpL2中同时替换keep with nothing的值来实现这一点.如果tmpL1和tmpL2不同,我会保留文件,否则会被删除.
setlocal enabledelayedexpansion set keep=[File I want to keep] for /F %%L IN ('dir /b *') do ( set tmpL1=%%L set tmpL2=!tmpL1:%keep%=! if !tmpL1!==!tmpL2! ( echo.[REMOVE] ) else ( echo.[KEEP] ) )
这工作正常.但是,当我将此代码放在一个更大的脚本中时,设置tmpL2突然停止工作.而不是(部分)文件名tmpL2现在字面上包含tmpL1:=.
这是我想要使用它的脚本.附加的for循环仅用于浏览目录树.脚本的主要功能仍然相同.
setlocal enabledelayedexpansion for /F %%G in ('dir /b *-snapshots') do ( set tmpG1=%%G for /F %%H in ('dir /b !tmpG1!\*') do ( set tmpH1=%%H for /F %%I in ('dir /b !tmpG1!\!tmpH1!\*') do ( set tmpI1=%%I for /F %%J in ('dir /b !tmpG1!\!tmpH1!\!tmpI1!\*-SNAPSHOT') do ( set tmpJ1=%%J set tmpJ2=!tmpJ1:~0,8! for /F %%K in ('dir /b !tmpG1!\!tmpH1!\!tmpI1!\!tmpJ1!\*!tmpJ2!*.pom /O:N') do ( set tmp1=%%K ) set keep=!tmp1:.pom=! for /F %%L in ('dir /b !tmpG1!\!tmpH1!\!tmpI1!\!tmpJ1!\*!tmpJ2!*') do ( set tmpL1=%%L set tmpL2=!tmpL1:%keep%=! pause if !tmpL1!==!tmpL2! ( echo.[REMOVE] ) else ( echo.[KEEP] ) ) ) ) ) )
我还尝试通过替换set tmpL2 =!tmpL1来尝试“懒惰”延迟扩展:%keep%=!使用调用集tmpL2 = %% tmpL1:%keep%= %%.这也适用于小脚本,但当我将它应用于大脚本时,我得到一个错误,如“=%”无法在此位置进行语法处理(这是一个免费的翻译,因为我的控制台是德语).
任何人都知道造成这种情况的原因是什么?
您可以尝试更改此行
set tmpL2 =!tmpL1:%keep%=!
set tmpL2 =!tmpL1:%keep%=!
同
FOR /F "delims=" %%R in (""!keep!"") do set "tmpL2=!tmpL1:%%~R=!"