5

我的脚本类似于:

cd <Directory>
set counter = 1
for /r %%f in (*) do (
<Do task>
echo Task completed for file<counter> >> C:\log
counter++
)

我不知道如何使用实际counter值。如果我使用counter或者%counter%它只是回显相同的字符串。在这种情况下我应该如何修改counter行?

4

2 回答 2

7

SET/A如果要计算数学表达式,必须使用 with 。您还需要首先通过键入SETLOCAL ENABLEDELAYEDEXPANSION作为第一行来启用延迟扩展。**FOR**在最后一次迭代发生之前,不会在循环内进行评估。但是我修改了批处理文件,以便 Counter 的值在每次迭代中递增。

SETLOCAL ENABLEDELAYEDEXPANSION

@echo off

set /a counter=1

for /r %%f in (*) do (
  echo Task Completed for file !counter! >> c:\log
  set /a counter=!counter!+1
)
于 2012-11-06T06:08:46.800 回答
0

SET /A Variable_Number Math_symbol = Step(s)

set /a Var_Num+=1-> Num_Var = Num_Var + 1(步长)

set /a Var_Num-=1-> Num_Var = Num_Var - 1(步骤)

set /a Var_Num*=2-> Num_Var = Num_Var x 2(步数)

set /a Var_Num/=2-> Num_Var = Num_Var:2(步数)

在一个循环中:

@echo off
set counter=0
:start-loop

set /a counter+=1

echo %counter% times... 
:: or/and
echo %counter% times... >> %USERPROFILE%\Desktop\log.txt

timeout /t 1 >nul
goto :start-loop

你的例子:(在'FOR'循环中)

SETLOCAL ENABLEDELAYEDEXPANSION
@echo off
set /a counter=1

for /r %%f in (*) do (
  echo Task Completed for file !counter! >> C:\log.txt
  echo Path to File: [ %%f ] >> C:\log.txt
  set /a counter+=1
)
于 2021-04-21T14:33:59.210 回答