incrementing a counter variable inside a FORLOOP

梦想的初衷 提交于 2019-12-24 10:45:41

问题


why is the variable "number" not increasing when the FOR loop goes over it again?

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N+=1
set /a number=!number!+1
echo  %%a !number!.jpg >output.txt
)

回答1:


OK, try this please:

@echo off &setLocal EnableDelayedExpansion
(for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N+=1
set /a number=!number!+1
echo  %%a !number!.jpg 
))>output.txt

In case of redirection you need >>output.txt or a code block (parentheses) and >output.txt.




回答2:


First of all I don't recommend you to enable expansion of variables when you are working with filenames, use it only if you really know what means enabling the delayedexpansion, the benefits (improved velocity) and the negatives (missing characters).

Also you are assigning a value for the variable "N" but you don't use that var.

Here is the code:

@echo off

(for /f "usebackq tokens=* delims= " %%a in ("input.txt") do (
    Set /A Number+=1
    Call Echo %%a %%number%%.jpg
))>"Output.txt"

Pause&Exit



回答3:


You should try

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N++
set /a number=!number!+1
echo  %%a !number!.jpg >output.txt
)

EDIT Or try:

setLocal EnableDelayedExpansion
for /f "tokens=* delims= " %%a in (input.txt) do (
set /a N++
set /a number=!number!++
echo  %%a !number!.jpg >output.txt
)


Or maybe that ++ isnt even supported by what you are using to program this. Let me know though!
Shadowpat



来源:https://stackoverflow.com/questions/16137004/incrementing-a-counter-variable-inside-a-forloop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!