incrementing a counter variable inside a FORLOOP

2019-08-07 04:27发布

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
)

3条回答
相关推荐>>
2楼-- · 2019-08-07 05:00

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.

查看更多
Luminary・发光体
3楼-- · 2019-08-07 05:04

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
查看更多
We Are One
4楼-- · 2019-08-07 05:24

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

查看更多
登录 后发表回答