I'm just unable to make it work.
I used another solution to determine if a received argument contains a certain substring. But it fails whenever I don't enter a third argument. I tried 4/5 methods to check if it exists and to condition the search within the variable but it doesn't seem to work!
set str1=%3
if not "%~3"=="" (
if not x%str1:TEST=%==x%str1% SET additional_config=UNIT_TEST SKIP_WAIT %3
)
How can I avoid evaluating %3 if it doesn't exist to avoid the error?
The code simply should set additional_config if 'TEST' is part of the third argument received. (if there is a third argument)
Your problem is the parsing of if not x%str1:TEST=% or even if not "%str1:TEST=%".
This part is parsed, even if %3 is empty and there comes the problem.
str1 is empty in that case. Percent expansion with search/replace of an empty/undefined variable has unexpected results.
In your case the parser detects at the stage %str1:, that str1 is undefined and stops the percent expansion.
Therefore, the parser sees TEST=%==x%str1% SET additional_config=UNIT_TEST SKIP_WAIT %3 and it splits it at pairing percent signs:
TEST= %==x% str1 % SET additional_config=UNIT_TEST SKIP_WAIT % 3
The parts in percent signs are invalid or undefined variables
TEST= ... str1 ... 3
At this point the program stops with a syntax error, because the block (inside parenthesis) contains an invalid IF statement
How to solve it?
One way is to store the value before
set "str1=%~3"
set "contains=%str1:TEST=%"
if not "%~3"=="" (
if not "%contains%" == "%str1%" SET additional_config=UNIT_TEST SKIP_WAIT %3
)
Or even better, use delayed expansion, that doesn't fail this way
#echo off
setlocal EnableDelayedExpansion
set "str1=%~3"
if not "%~3"=="" (
if not "!str1:TEST=!" == "!str1!" SET additional_config=UNIT_TEST SKIP_WAIT %3
)
Related
How does following code works in Batch Scripting(.bat) file?
:modifyString what with in toReturn
SET "__in=%~3"
SET "__in=!__in:%~1=%~2!"
IF NOT "%~4" == "" (
SET %~4=%__in%
) ELSE (
ECHO %__in%
)
Assign the contents of the third parameter, stripped of quotes to the variable.
replace any occurrence of the first parameter in that string with the second parameter (both stripped of quotes)
If the fourth parameter is not missing, set the variablename which is the fourth parameter to the result of the previous operation. If it is not not missing then display the result of the previous operation.
(yes - I know about the double negative - but that's how it's coded)
I found some sample code but I am unable to get what this if condition means:
set /p sourceDB=Enter Source DB: %=%
set S=%sourceDB%
set I=0
set L=-1
:l ----- Forget about this line
if "!S:~%I%,1!"=="" goto ld
if "!S:~%I%,1!"=="/" set K=%I%
if "!S:~%I%,1!"=="#" set Z=%I%
if "!S:~%I%,1!"==":" set Y=%I%
set /a I+=1
goto l
The short answer is that this is how you get substrings in batch.
When you extract a substring, you use the format %string_name:~index_of_first_character_in_substring,length_of_substring% or, if the value of either index_of_first_character_in_substring or length_of_substring is contained in a separate variable (in your example, the index is its own variable), you can enable delayed expansion and use the format !string_name:~%variable_whose_value_is_the_index_of_first_character_in_substring%,length_of_substring!
In this case, your main string is in a variable called %S%, you are starting at character %I%, and grabbing 1 character.
The line you've told us to ignore is actually pretty important, as it's used to loop through the entire string.
The entire line "!S:~%I%,1!"=="" is used to check if the substring is empty -- that is, the script is finished iterating through the string. There are also conditions for if the substring is /, #, and :; with K, Z, and Y respectively containing the indices of those substrings.
As stated in the title, does anyone how to check whether a variable contains only spaces in batch?
Try this:
if "%variable: =%"=="" (
rem variable contains only spaces
)
%variable: =% is a form of %haystack:needle=replace%, searching for space and replacing with nothing. So if you replace all spaces with nothing in the variable and the result equals nothing, then the variable contains only spaces.
Edit: Aacini is right. The above if statement will evaluate true if the variable contains only spaces, but false if the variable is not defined. Probably ought to check explicitly whether the variable is defined first. Here's one way:
if not defined variable (
rem variable is not defiend
) else if "%variable: =%"=="" (
rem variable contains only spaces
)
Here's another:
if defined variable if not "%variable: =%"=="" goto valid
rem variable is either undefined or contains only spaces
:valid
I have two variables. var1 = 1.0 ; var2 = 0.9.
I want to do this:
set var1=1.0
set var2=0.9
if %var1%=%var2% echo equal
if %var1%(not equal to)%var2% echo not equal
pause
Does anybody know what look the "not equal" symbol should look like?
if "%var1%" neq "%var2%"
or
if not "%var1%"=="%var2%"
Type help if in a console window for more info. You should also get into the habit of enclosing both sides of the == signs in quotation marks, in case one of the variables contains a space.
if %var1% neq %var2% echo not equal
there's also geq gtr lss leq and equ (greater or equal, greater, less, less or equal and equal)
But - it's best to use "quotedvariablenames" on both sides of the comparison if you're using strings that contain separators (like spaces) and you can also use
if /i ...
to compare the strings case-insensitively
and also you can use
if not ....
to negate a condition
or
if condition (dothis) else (dothat)
There must be a separator before the opening parenthesis of the IF true-condition target
That opening parenthesis must be on the same physical line as the if
Where an 'else' clause is used, the ending parenthesis of the "true" block, a separator and the else keyword must be on the same physical line
Where an 'else' block is used, the else keyword, a separator and the opening parenthesis of the "else" block must be on the same physical line
ie
if condition (
something
) else (
someotherthing
)
but be careful!
batch does not support floating-point. comparing 1.0 with 0.9 will be performed as an alphabetic comparison and 2.0 will be reported as greater than 19.0 (easy with not-equal...)
Here is a batch file, I need to set the second parameter to a certain path if it is not supplied, else use the value of second parameter further. Note that i need to access the value of EXECUTE_DIR, further in the file
dummy.bat
IF %2 == "" (
SET EXECUTE_DIR = "c:\Program Files"
) ELSE (
SET EXECUTE_DIR = %2
)
ECHO exedir = %EXECUTE_DIR%
-
when I provide only 1 parameter, I get following output:
D:>dummy.bat "Lab"
( was unexpected at this time.
D:>IF == "" (
D:>
you need
if "%2"=="" ....
both sides of the comparison operator must exactly match. If %2 does not exist, your code is resolved to
if =="" ...
which is clearly a syntax error as reported.
My preferred version is
set "var=%~2"
if not defined var ...
which conveniently assigns the value of %2, with enclosing quotes removed, to var.
naturally, you could use if defined var... if that's more convenient.
meanwhile, your )else( will also generate an error. You must have spaces both sides of the else, otherwise cmd doesn't know whether you are invoking an else clause or )else( is some variety of variable or option or whatever.
The ) and ( must also be on the same physical line as the else keyword (which you have).
(belay that - proportional-space ib unformatted text makes the spaces hard to spot...)
But that brings up another point - Batch is sensitive to spaces in a SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN". The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var.