Batch If statement not working - batch-file

Having a problem with an If statement in a batch file. I have several scripts, one will set a value to variable and store it to a text file to be read by another script. The value is either yes or no. That part works. When the next script executes, it first reads the line from the txt file and stores the value to a variable. That part works, I can echo the variable and see that it reads the line and stores it to the variable. Now, the next section is suppose to evaluate the variable and based on the result the script will either continue or exit. for some reason it always exits. Below is sample of the statement:
::****Check Value******
set /p _xexit=<xclean.txt
Echo %_xexit%
if %_xexit%=yes (
goto skip3
) else (
::CODE TO EXECUTE IF %_xexit%=no starts here
code
::AFTER THIS CODE EXECUTES, SCRIPT WILL EXIT
::My skip3
:skip3
echo "Exit Called"
wait
Exit

You have to double the =
if "%_xexit%"=="yes"

Related

What is the difference between "myvar=me" and "myvar"="me"?

I've been wondering what is the difference between "myvar=me" and "myvar"="me" in a batch file?
It might make a difference to my program which is a rock, paper, and scissors game.
The difference can be easily seen on running a batch file with following lines:
#set "myvar=me"
#set "myvar"="me"
set myvar
#pause
The first line defines an environment variable with name myvar with value me.
The second line defines an environment variable with strange name myvar" with value "me.
The third line is output by Windows command interpreter after preprocessing the command line before execution and outputs all environment variables of which name start with myvar with environment variable name, equal sign and environment variable value.
And fourth line halts batch execution until a key is pressed to see output of third line in case of batch file was executed with a double click.
So the first three lines of output are:
set myvar
myvar=me
myvar"="me
For details on how to define an environment variable right with correct assigning a value read answer on:
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
It explains with text and examples why the syntax set "variable=value" is usually the best.
That depends on context.
In case of usage inside the set command it can treat quotes as a part of a variable name and a value:
>set "a"="111"
'
>set
a"="111
...
But not in this case:
>set "a=111"
'
>set
a=111
...
Internal logic of the cmd.exe is simple is that as it eats character by character and removes quotes from the most left and right parts of a string.
Here is another context (file test.bat):
#echo off
call :TEST "1111"="2222"
call :TEST "1111","2222"
call :TEST "1111";"2222"
call :TEST "1111"-"2222"
exit /b 0
:TEST
echo -%1- -%2-
'
-"1111"- -"2222"-
-"1111"- -"2222"-
-"1111"- -"2222"-
-"1111"-"2222"- --
As you see some characters treated here as a parameter separator in a command line.
Personally I prefer to write the set "var=value" without ^-escape characters before the & and ) characters which can be part of a file path.

Batch Script excueting unchosen block of code

I have a batch script that checks the input of the user before running a specific block of code. Both blocks manipulate the same variables but it appears the variables of the second block are the ones that are being set even if I choose the first block to execute.
Code:
if %x == 1 goto :Block1
if %x == 2 goto :Block2
:Block1
set variable== "Works"
:Block2
set variable == "Works"
Block 2 is executing even if I input 1. Am I missing something?
Unlike many languages, batch has no concept of the end of a "procedure" - it simply continues execution line-by-line until it reaches the end-of-file. Consequently, you need to goto :eof after completing the mainline, otherwise execution will continue through the subroutine code. :EOF is a predefined label understood by CMD to mean end of file. The colon is required.

Executing batch file breaks when checking if variable has value

To chcek if file is loaded or not, I load its contents using:
set /p filevar=file.txt
and check if var is empty:
if "%filevar%"="" exit
When script chceks file with multiple lines, execution of script stops, so i suppose that chcecking fails. Why script fails? How to perform such check properely?
Firstly, you've got the syntax of your set /p command messed up. As it is, it will prompt the user with the text "file.txt". I think what you mean is
set /p "filevar=" <file.txt
You should also use the /b switch with exit to prevent the console window from closing if your script is run from the command line.
But yeah, as jeb states, to check whether left equals right, either use == or equ. Or, as dbenham reminds me, if you're checking for an empty value, you can also use if not defined.
if "%filevar%"=="" exit /b
if "%filevar%" equ "" exit /b
if not defined filevar exit /b
All three statements will have the same result1.
In a console window, enter help if for more information.
1 It's worth mentioning that, while those three if statements have the same result outside of a parenthetical code block, only the third one will work reliably within parentheses (such as in a for loop). The first two would need delayed expansion to work properly if used within the same code block as %filevar% is set.

Set one variable in another batch file

I would like to set a variable in another batch file, if it exsists. But it works only localy in the sub batch file. How can I fix this problem?
Main.bat:
SET TEMP=""
IF EXIST SUB.bat (
CALL SUB.bat
REM Returns: TEMP="" IN MAIN
ECHO %TEMP% IN MAIN
) ELSE (
SET TEMP="DEFAULT VALUE"
)
Sub.bat:
SET TEMP="OTHER VALUE"
REM Returns: TEMP="OTHER VALUE" IN SUB
ECHO %TEMP% IN SUB
Output by calling Main.bat:
TEMP="OTHER VALUE" IN SUB
TEMP="" IN MAIN
Two issues:
Your test is incorrect. Within a block statement (a parenthesised series of statements), the entire block is parsed and then executed. Any %var% within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block).
Hence, IF (something) else (somethingelse) will be executed using the values of %variables% at the time the IF is encountered.
Try using CALL ECHO %%TEMP%% to display the altered value and look up "delayedexpansion" for endless SO items on this frequently-encountered subject.
Second issue - which impinges on the first.
TEMP and TMP are special variablenames which specify the location of a temporary-files directory. Best not to change them as unexpected results may ensue. Use another variablename.

Batch File not setting variable

I'm trying to write a batch file that will simplify the generation of WebService stubs. The problem is that one of the SET commands for a variable is not setting the value. I've tried various things to get it to honour this SET but to no avail. Clearly missing something obvious. The rest of the script is working fine.
IF %1==-b (
ECHO %2
SET BINDINGS_FILE=%2
SHIFT & SHIFT
ECHO File: %BINDINGS_FILE%
IF EXIST "%BINDINGS_FILE%" (
SET BINDINGS=-b %BINDINGS_FILE%
) ELSE (
ECHO Please enter a valid Bindings file name: %BINDINGS_FILE%.
GOTO DONE
)
ECHO BINDINGS = %BINDINGS%
)
When I execute it with the following command, it prints the bindings file as %2 but the variable into which it gets SET remains empty.
generate-stubs.bat -b wsdl/Binding.xml -p com.acme.service wsdl/WebService.wsdl
wsdl/Binding.xml
File:
Please enter a valid Bindings file name: .
Done!
Any suggestions appreciated.
Batch file is setting the variable to the indicated value. BUT you are not seeing it.
In batch files lines are parsed, and then executed. Line by line or block by block (lines enclosed in parenthesis). When the parser reaches a line or a block of lines, at all points where a variable is read, the reference to the variable is removed and replaced with the value in the variable before starting to execute the block. So, if a variable changes its value inside a block, this new value will not be accesible from inside this same block. What is being executed does not include a reference to the variable but the value in the variable when the code was parsed.
To change this behaviour, and be able to read the changed value of the variable from inside the same block that changed the value, delayed expansion is needed.
When delayed expansion is enabled, the syntax to access/read a variable can be changed (where needed) from %var% to !var!. This instructs the parser not to do the initial replacement and delay the access to the value until execution of the command.
So, your code can be something like
setlocal enabledelayedexpansion
IF "%1"=="-b" (
ECHO %2
SET "BINDINGS_FILE=%~2"
SHIFT & SHIFT
ECHO File: !BINDINGS_FILE!
IF EXIST "!BINDINGS_FILE!" (
SET "BINDINGS=-b !BINDINGS_FILE!"
) ELSE (
ECHO Please enter a valid Bindings file name: !BINDINGS_FILE!.
GOTO DONE
)
ECHO BINDINGS = !BINDINGS!
)
Looks like a very old post but might save someone hours of effort...
All commands within IF statement brackets "(" ")" get executed as a single block. The variable value does get changed during execution of the IF statement block (LINE 2 till LINE 5) but the changed value can ONLY be used after the current block execution completes.
e.g. your code snippet below,
1 IF %1==-b (
2 ECHO %2
3 SET BINDINGS_FILE=%2
4 SHIFT & SHIFT
5 ECHO File: %BINDINGS_FILE%
)
6 ECHO %BINDINGS_FILE%
Although the value of "BINDING_FLAGS" is set in Line #3, but it will take effect ONLY in the next block or next command, which means, that Line #6 will display the actual value and not LINE #5

Resources