Multiple If statements not working properly skipping second

April 2, 2013 at 07:14:56
Specs: Windows 7
Snippet not working as intended:
@echo off & setlocal enabledelayedexpansion
if "%lines%"=="%before%" (
set /A cnt+=1
if "!choice:~%cnt%,1!"=="8" (
set /A lines+=1
echo.%choice:~0,7%>> %tmp%\decoded.txt
set choice=%choice:~7%
)
)

In my case yes line and before are equal, therefore cnt is increased by 1.
Therefore !choice:~%cnt%,1! is equal to 8.
However, i want to check whether it is 8 since it obviously varies, if it is indeed 8 do this:
set /A lines+=1
echo.%choice:~0,7%>> %tmp%\decoded.txt
set choice=%choice:~7%

.
As my testing goes it doesn't go into the second operation after the second if statement it just skips it.
How can i fix this?
Simply put if %lines% is equal to %before% increment %cnt% by 1 and check if
!choice:~%cnt%,1! is equal to 8, if so, do:
set /A lines+=1
echo.%choice:~0,7%>> %tmp%\decoded.txt
set choice=%choice:~7%

I require this all in 2 if statements together, not any independent due to the structure of the program.


See More: Multiple If statements not working properly skipping second

Report •

#1
April 2, 2013 at 07:43:55
Normal (aka %) expansion runs before loop processing, so it'll always have the same value while the loop runs. If you cannot use delayed expansion, break it out into some other function and call it.

You're better off picking a sane language. Batch is really the worst almost-but-not-quite-a-real scripting language you will ever see. Both VBScript and Powershell would have no problems doing what you want.

How To Ask Questions The Smart Way


Report •

#2
April 2, 2013 at 07:59:10
Yeah of course, i'm learning python and c++ at the moment. Just doing this since it's very fast in batch.
So mind posting a solution? Much appreciated if you do.
Honestly clueless about what to do, but yes that idea of calling came to me in mind, but it wouldn't be a very good implementation.

Report •

#3
April 3, 2013 at 09:14:14
Ok nvm, found a work-around.

Report •

Related Solutions

#4
April 3, 2013 at 11:35:57
set /a cnt+=1
if %line%==%before% (
::...
) else (
set /a cnt-=1
)
might have worked...

Report •

Ask Question