Solved Can't seem to get set command to work

May 22, 2012 at 10:35:45
Specs: Windows 7
Unable to increment in dos batch

Trying to read the second line of a file.

Figured I'd read the 1st, increment my line counter by one, then read the second, and after linecounter = 2, exit w/ the variable info I need.

I'm doing something wrong with the set command, but can't figure out what.

Here's the code:

@echo off
for %%i in (*.txt) do (
set L=
echo L = %L%
ECHO FILENAME= %%i
set L=0
echo L = %L%
for /f %%a in (%%i) do (
echo %%a
set /a L=L+1
echo LCOUNT= %L%
if L==2 exit /b
PAUSE
)
)


See More: Cant seem to get set command to work

Report •


✔ Best Answer
May 22, 2012 at 14:49:29
Tony is correct, you should enabledelayedexpansion to expand a variable within parenthesis but you must also use ! instead of % when doing so

This is incorrect set /a L=L+1
This is correct set /a L=!L!+1

Or you could use Set /a L+=1



#1
May 22, 2012 at 10:52:47
could be this: The SET command will not allow an equal sign to be part of the name of a variable.

more info on correct SET syntax here: http://www.computerhope.com/sethlp.htm


Report •

#2
May 22, 2012 at 11:05:03
Thanks for the info.

So, to reset, I need to use

set L

That would be one, now the other is why isn't it incrementing?


Report •

#3
May 22, 2012 at 12:26:12
You need to use delayed expansion when inside a FOR loop.
@echo off & setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set L=
echo L = %L%
ECHO FILENAME= %%i
set L=0
echo L = %L%
for /f %%a in (%%i) do (
echo %%a
set /a L=L+1
echo LCOUNT= %L%
if L==2 exit /b
PAUSE
)
)

Tony


Report •

Related Solutions

#4
May 22, 2012 at 13:18:32
Thanks, Toni, but there appears to be an issue in the set command.

No matter what syntax I try, I'm not getting the set command to behave in a reliable fasion.

If I'm understanding everything correctly:

set L=" "

Should place a space in the L variable. This doesn't appear to happen when running the job. L is whatever L was when it was last closed, and I need this to happen within the batch file.

I have tried to come up with an alternate solution just using the filename, which contains the pertinent informatino, but it all comes back to the set command in DOS (I've tried two different O/Ses, Win2K3 and Win7) and neither sets the variable, something which SHOULD be simple.

I even used the logic you provided and still no happiness.

Is there something wonky about setting variables within a for statement that I'm missing?


Report •

#5
May 22, 2012 at 13:32:03
:: Read the second line of a file
@echo off
for /f "tokens=2 delims=:" %%i in (
'findstr /n "." test.txt ^| findstr "2:"'
) do (
echo %%i
)

Tony


Report •

#6
May 22, 2012 at 14:06:59

Thank you, Tony.

However, while putting the first set outside of the for loop takes care of setting the variable to empty, once I'm inside the loop, I can't set a variable,


Report •

#7
May 22, 2012 at 14:49:29
✔ Best Answer
Tony is correct, you should enabledelayedexpansion to expand a variable within parenthesis but you must also use ! instead of % when doing so

This is incorrect set /a L=L+1
This is correct set /a L=!L!+1

Or you could use Set /a L+=1


Report •

#8
May 22, 2012 at 15:14:06
SET /A goes by its own special rules.
C:\>set L=1

C:\>set /a L=L+1
2

C:\>echo %L%
2

How To Ask Questions The Smart Way


Report •

#9
May 22, 2012 at 19:56:51
Thank you Razor.


Report •

#10
May 22, 2012 at 20:57:41
Thanks, all. I'll try it tomorrow.

Report •

#11
May 23, 2012 at 06:22:17
Thank you all.

I changed the mission slightly, as I realized I could get the information I needed from the filename. I picked TCs answer as "best answer" due to the hint concerning the behavior of variables within the for loop (critical to what I am doing), but Tony and Razor, I was able to use info from posts from both of you.

Here's the final code.

@echo off & setlocal enabledelayedexpansion
for %%i in (*.txt) do (
set L=
ECHO FILENAME= %%i
set L=%%~ni
echo L = !L!
set t=!L:~3,9!
echo t = !t!
)

Again, thank you.


Report •


Ask Question