incrementing a counter variable inside a FORLOOP

2019-08-07 05:08发布

问题:

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