Quote equality test in batch - batch-file

How can I test my string as if it is a quote (") or not.
The default escape character is '^', isn't it?
So I tried the following but with no success yet:
if "!first_char!" == "^"" (...)
I tried double quote, too:
if "!first_char!" == """" (...)
Thanks!

The problem is not with the right part of the comparison, but with the left part. As first_char contains a quote, then the left operand of the comparison does not make sense. So you have to escape with ^ the variable that holds the ".
try something like this...
if .^!first_char!.==.^". (#echo ^!first_char! YES) else (#echo ^!first_char! NO)

The problem is only on the right part of the comparision.
The delayed expansion on the left side is always safe.
So you could simply use also a delayed expansion on the right side, like
set singleQuote="
if "!first_char!" == "!singleQuote!" (...)
Or alternativly you could escape all quotes.
if "!first_char!" == ^"^"^" (...)

Related

Hi! How i can write symbol & to variable in CMD ? )

I need to write symbol & to variable in CMD for word is like = adc&def.
There are two ways to do that:
Use a caret ^ to escape the character. It will work for these characters: & < > ^ | (You escape a caret by using a caret).
Use double-quotes. By using double quotes it's not necessary to escape characters (except exclamation marks when using EnableDelayedExpansion.
So, the final solution could be:
set "var=abc&def"
or set var=abc^&def.

How does following code works in Batch Scripting(.bat) file?

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)

What does "!S:~%I%,1!"=="" mean?

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.

Check if there's a difference between two variables

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...)

Error in Batch file when 2nd parameter is not supplied

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.

Resources