简体   繁体   English

如何在具有一系列变量的批处理文件中循环执行一组命令?

[英]How can I loop a set of commands in a batch file with a series of variables?

Here is part of a Windows batch file that I already have: 这是我已经拥有的Windows批处理文件的一部分:

@echo off
setlocal ENABLEDELAYEDEXPANSION
set files=0
for /F "tokens=*" %%A in (filelist.csv) do (
    SET /A files=!files! + 1
    set var!files!=%%A
)
set var

findstr /i /c:"%var5%" trans.log
if %errorlevel% EQU 0 del %var5%

In this batch file, "filelist.csv" is just a list of files in a particular folder that is automatically generated. 在此批处理文件中,“ filelist.csv”只是自动生成的特定文件夹中的文件列表。 The quantity and names of the files in this folder change on a daily basis and when they change the list is updated. 该文件夹中文件的数量和名称每天更改,并且当它们更改时,列表也会更新。 There is one file per line in "filelist.csv". “ filelist.csv”中每行只有一个文件。 The contents of each line in the file are assigned a variable named var1 through varN, and values of these variables correspond to actual file names. 文件中每一行的内容都分配了一个名为var1到varN的变量,这些变量的值对应于实际的文件名。

The batch file then searches through another file called "trans.log". 然后,批处理文件搜索另一个名为“ trans.log”的文件。 In the specific case above it searches for the file that corresponds to "var5". 在上面的特定情况下,它将搜索与“ var5”相对应的文件。 If it finds that file mentioned in "trans.log" then it gets deleted. 如果找到“ trans.log”中提到的文件,则将其删除。

What I need to figure out is a way for this last bit of the batch: 我需要弄清楚的是批处理的最后一部分的方法:

findstr /i /c:"%var5%" trans.log
if %errorlevel% EQU 0 del %var5%

to loop for as many times as there are variables "var1" through "varN" and each time it loops, to automatically replace the variable with the next one in the series. 循环次数从变量“ var1”到“ varN”一样多,每次循环时,将变量自动替换为系列中的下一个变量。 So in the case above, if there were 10 files in the list then there would be variables "var1" through "var10". 因此,在上述情况下,如果列表中有10个文件,则将有变量“ var1”至“ var10”。 The next time the loop runs from where it is above, it would replace "var5" with "var6" and then "var7", all the way to "var10" and then it would exit the loop and move on to any other commands that come after it in the batch. 下一次循环从上面的位置运行时,它将用“ var6”替换“ var5”,然后用“ var7”替换,一直到“ var10”,然后退出循环并继续执行任何其他命令批量处理之后。

Can anyone help me with this, please? 有人可以帮我吗? TIA TIA

I think this does what you're asking. 我认为这符合您的要求。

@ECHO OFF

@echo off
setlocal ENABLEDELAYEDEXPANSION
set files=0
for /F "tokens=*" %%A in (text.txt) do (
    SET /A files=!files! + 1
    set var!files!=%%A
    for %%i in (!files!) do (CALL :MyFunction !var%%i!)
)
REM
REM Other code can go here...
REM
GOTO END

:MyFunction
ECHO findstr /i /c:"%1" trans.log
ECHO if %errorlevel% EQU 0 del %1

:END

Remove the ECHO statements in MyFunction to actually run findstr and delete the file. 删除MyFunctionECHO语句以实际运行findstr并删除该文件。

@echo off
setlocal
for /F "skip=4tokens=*" %%A in (filelist.csv) do (
 findstr /i /c:"%%A" trans.log > nul
 if not errorlevel 1 ECHO del %%A
)

This should do what you appear to want to do. 这应该可以完成您似乎想做的事情。 Without sample data, testing is impossible. 没有样本数据,测试是不可能的。

The skip=4 skips the first 4 lines of filelist.csv and feeds each succeeding line to FINDSTR which sets errorlevel to 0 if the filename is found in trans.log . skip=4跳过filelist.csv的前4行,并将每行后面的内容馈送到FINDSTR ,如果在trans.log找到文件名,则将errorlevel设置为0。 the if not errorlevel 1 tests for errorlevel being 0 - an the set/test cycle is executed for each line after the first 4 (skipped) lines. if not errorlevel 1if not errorlevel 1测试为0-在前4(跳过)行之后,对每行执行设置/测试周期。

Note that your original scheme would fail (if I understand your purpose correctly) if file5 was not in trans.log . 请注意,如果file5 不在 trans.log ,则您的原始方案将失败(如果我正确理解了您的目的)。 but file6 (or any later) was in trans.log - file5 would be erroneously deleted and the target retained. file6 (或任何更新) trans.log - file5会被错误地删除与目标保持。 You'd also get a series of errors for hits after file5 because, having deleted file5 , re-executing the delete on file5 would find it was absent... file5之后,您还会遇到一系列针对匹配的错误,因为删除了file5 ,然后重新执行file5的删除file5会发现它不存在...

Naturally, the del command is simply echo ed - change echo del to del after verification to actually delete the targets. 自然, del命令只是简单地echo ed- 验证后将 echo del更改为del即可实际删除目标。

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

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