How does following code works in Batch Scripting(.bat) file? - batch-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)

Related

=str13 was unexpected at this time batch file

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
)

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.

How to check a variable only contains spaces?

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

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.

using dos commands how can I match a substring from a variable?

using dos commands how can I match substring from a variable.
e.g: var="Lost = 0 (0% loss)"
I want to check whether var contains "Lost" in it.
I have already tried Contains, like but throwing error.
echo %varname%|findstr "string" >nul
if errorlevel 1 (echo %varname% does not contain "string"
) else (echo %varname% contains "string")
Note that this can all be on one line if you prefer. If broken across lines, the sequence ) else ( must be on same line, as must the if and very first (

Resources