user1799803
user1799803

Reputation: 187

Assign variables in batch script

I'm new in Windows batch programming and I have found problems in the variable assignment. This is my code:

@echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
    set /a Video=%Video%+1
    @echo Video
    set file=%%a
    @echo file
)

If FILEMEDIA has two lines I would like to obtain Video=2 and the line in file variable. However, at the end I obtain Video=1 and an error when I tried to print file (echo is off).

Upvotes: 0

Views: 2058

Answers (1)

Rubik
Rubik

Reputation: 1471

Some kind of duplicate with How do I increment a DOS variable in a FOR /F loop?

Variables that should be delay expanded are referenced with !VARIABLE! instead of %VARIABLE%.

@echo off
setlocal enabledelayedexpansion
set Video=1
set FILEMEDIA=outputMedia.txt
for /f %%a in (%FILEMEDIA%) do (
    set /a Video+=1
    @echo !Video!
    set file=%%a
    @echo file
)
endlocal

Upvotes: 1

Related Questions