【问题标题】:Variable not incrementing in Windows Batch file recursive loopWindows Batch 文件递归循环中的变量不递增
【发布时间】:2020-03-10 18:46:18
【问题描述】:

我是一个没有经验的批处理脚本,我的任务是在日志名称上附加一个递增的数字。该脚本使用 7zip 的 CLI 工具扫描 zip 文件,并生成日志。这是我遇到问题的相关位(我添加了“SETLOCAL EnableDelayedExpansion”、“SET count=1”、“_%count%”和“set /a count+=1 bits):

SETLOCAL EnableDelayedExpansion
SET count=1
for /R G: %%g in (*.zip) DO %rundir%\\7za.exe t -r -p"<zip password goes here>" "%%~fg" 1> "%logfolder%\\%%~nxg.log_%count%.txt" set /a count+=1 2>&1

计数保持在 1,我不太确定如何让它移动。我主要用 Python 编写代码,在这一点上,我很想把它全部烧掉,然后用 Python 重新编写所有东西。我不知道代码的“2>&1”位是做什么的。非常感谢任何有关解决此问题的帮助!

【问题讨论】:

    标签: windows for-loop batch-file command-line


    【解决方案1】:

    在同一行中运行 set /a count+=1 不会使其成为新命令。此外,您没有使用您设置的delayedexpansion。看看我是如何使用!count! 而不是%count%

    @echo off
    setlocal enabledelayedexpansion
    set count=0
    for /R G: %%g in (*.zip) DO (
        set /a count+=1
        %rundir%\7za.exe t -r -p"<zip password goes here>" "%%~fg" >>"%logfolder%\%%~nxg.log_!count!.txt" 2>&1
     )
    

    至于2&gt;&amp;1 它只是将stderr 重定向到stdout 但是我不确定你为什么需要set /a count+=1 所以不要添加它,我们可以为7za 命令添加它为了将stdoutstderr 记录到日志文件中(我已经这样做了)

    【讨论】:

    • 非常感谢,它有效!感谢对代码本身的帮助,以及对“2>&1”位的解释。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-06
    • 2011-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    相关资源
    最近更新 更多