2

I want to simply add 10 to the variable "%%i" in this batch file code and print it out to the screen. numbers.txt is a file that contains a single column of numbers.

FOR /F %%i IN (numbers.txt) DO (
    set /a "T=%%i+10"
    @echo %T%
)

For example, if %%i was 1 I would want T to be 11.

Thanks.

0

4 Answers 4

4

Without delayed expansion, you can use:

@echo off
set constant=10
FOR /F %%i IN (numbers.txt) DO (
    set /a "T=%%i+%constant%"
    call echo %%T%%
)

constant does not need delayed expansion, because it's constant throughout the loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Not a good idea to use TEMP as a batchfile variable - it points to a directory used for temporary files...(TMP too)
1

Try the following code, it should work according to your description in the question :

@echo off
setlocal enabledelayedexpansion
set T=0  
set K=10
for /f %%i in (numbers.txt) do (    
  set /A T=!K! + %%i    
  echo !T! )
endlocal

Save it as a .bat file extension and run from command prompt.
If file numbers.txt contain :

1
2
3

Output will be:

11
12
13

Comments

0

You can do this with delayed environment variable expansion. In the shell, you must first type

cmd /v

Then you can execute this batch script:

FOR /F %%i IN (numbers.txt) DO (
    set /a T=%%i + 10
    @echo !T!
)

If you don't run cmd /v first, then it will simply output !T! literally. See set /? for details on delayed environment variable expansion.

Comments

0

Start your batch file by turning on delayed expansion. With delayed expansion enabled, reference a variable inside a loop using ! instead of %.

SETLOCAL ENABLEDELAYEDEXPANSION
FOR /F %%i IN (numbers.txt) DO (
    set /a "T=%%i+10"
    @echo !T!
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.