SET /p con1=----Do you want to display some text?(y/n):
IF con1==y (echo hi)
But this is not displaying "hi" on the command prompt screen.
Use this code
SET "con1=n"
SET /p "con1=----Do you want to display some text (y/n)? "
SET "con1=%con1:"=%"
IF /I "%con1%"=="y" (echo hi)
You forgot the percent signs to reference the value of variable con1. Therefore your IF compared fixed string con1 with fixed string y which are never equal.
The compare is now not case-sensitive because of parameter /I.
A default value is set in case of user just hits enter on prompt.
And last double quotes are removed from entered value in case of user enters one or more " instead of just y or n as otherwise the batch execution would break on line with IF condition because of a syntax error.
Related
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
)
Here is my sample batch file code and I really don't know what it does.
set TEMPRPSW=%RPSW_VERSION%
set RELVER=%TEMPRPSW:~0,4%
set RELVER=%RELVER:.=%
if %RELVER% GEQ 30 goto :eof
Please give me a working sample.
That takes a 4 character long substring of TEMPRPSW, starting from character 0.
Meaning, it takes the first 4 characters of TEMPRPSW and puts them in RELVER.
set TEMPRPSW=abcdef
set RELVER=%TEMPRPSW:~0,4%
echo %RELVER% -> will print abcd
%VAR:str=% removes str
set RELVER=123.456
set RELVER=%RELVER:.=%
echo %RELVER% -> will print 123456 with no .
here is a nice article: https://www.dostips.com/DtTipsStringManipulation.php
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.
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.