Batch Command Skipping Error

September 1, 2013 at 12:03:06
Specs: Windows 7, Intel(R) Core(TM) i3 / 3.20 GHz / 8 GB Ram
So, I have been working on a little project lately and I have encountered a problem while testing.

The batch file just 'skips' commands.

When I use echo on for testing, it doesn't even write that it reads the command.

Here is the code of the file that is buggy:

if 1runtwice == 1 goto calc
set 1runtwice=1
echo.
echo 1) Default
exit /b
:calc
title Default
call 2.bat
call 1.bat
set 1runtwice=0
exit /b

Commands that get skipped:
title Default
call 2.bat
set 1runtwice=0

If you need more info, tell me.

Thanks in advance

message edited by SilasPc


See More: Batch Command Skipping Error

Report •


#1
September 1, 2013 at 18:05:05
First, recommend not to use a number as the start of a variable-name, because batch-parse can become confused between commandline parms and regular vars (commandline parms are like %1, %2, so if your varname starts with %1, then batch might think you mean cmdline parameter #1.)
Second, you did not provide for "undefined" status of variable, which f_s up the "goto" because there's nothing to compare to. There are multiple ways to fix this, but this example provides what i think is best (note that I changed var. 1runtwice to _1runtwice, to address the first issue):

@echo._1runtwice on entry: [%_1runtwice%]
pause

if not defined _1runtwice (
set _1runtwice=1
echo.
echo 1) Default
exit /b
)

:calc
title Default
call 2.bat
call 1.bat
:: changed from "0" to undefined foll. line
set _1runtwice=
exit /b
::======== end batch snip

message edited by nbrane


Report •

#2
September 1, 2013 at 22:20:55
I get the first part, which being the problem with the variable that starts with a number.

But not the second thing.

What does '@echo._1runtwice on entry: [%_1runtwice%]' do?
What happens when you undefine variables, and why would I need it?


Report •

#3
September 2, 2013 at 17:42:30
The first thing just shows the current value of %_1runtwice% going into the program. It is only for information.
The second thing is only one, of many ways, to make sure things work. Going in from a cold start, all "local" (vs global) var.s are undefined, until your batch specifically defines them. My approach aims to conserve "code-space" and also simplify things by using undefined instead of assigning the var. to zero then testing for value that may or may not have been assigned globally. (Sorry, I suck at explaning things like this). F/e, IF %a%==yes This will generate an error unless %a% has been defined, so i always test for undefined before any other tests for values to keep the error from occurring. if %a% is undefined, it looks like this to batchparser:
IF ==yes goto :zzz
You can see how that does not look right. You could also use quotes around both sides, like this:
if "%a%"=="yes" goto :zzz
Then the parser will see (with %a% undefined):
if ""=="yes" goto :zzz
which makes sense to it.
Hope this helps.

Report •

Related Solutions

#4
September 3, 2013 at 07:29:32
Oh of course.

But. Now it comes up with a text saying 'Variable not defined'(or so).
And it still skips the commands.

Is there a way to stop it saying that?(like testing if it exists or something)

If you want, I can send you the entire program

message edited by SilasPc


Report •

#5
September 3, 2013 at 18:16:53
from resp #1:
if not defined _1runtwice (...)

tests if the variable exists or (in this case) not. That example just does a two-state "flipflop": var is either not defined (then, define it as "1" and exit), or it is defined (then undefine it and exit).
note that you do NOT use % on the var.name, for testing "defined".

Yeh, it might save us both time and effort, you can pmail the program if you like.


Report •

Ask Question