I've got a pretty simple batch script, but for some reason the conditional isn't working like I'm expecting:
SET RIG=%1
IF /I RIG==Polar (
SET SQLSERVER=SQL01
SET MONITOR=SQL02
SET TESTSETTING="test"
SET TESTCONTAINER="test2
) ELSE (
ECHO Your input was %1.
)
When I pass the following command: testrun.bat Polar I get the ECHO statement returned: Your input was Polar."
I've tried putting the %1 in quotations, messing with the way parentheses are laid out, and I've reviewed ss64.com's syntax for batch script IF statements, but I just can't figure this one out.
RIGNAME is never going to be equal to Polar
Now you could try assigning the value to rigname rather than rig.
Even then, yu'd need to compare %rigname% to Polar to access the content of rigname rather than the string RIGNAME.
Related
So I'm trying to make a search engine that, when you input a search string, replaces the spaces with a "+", and it'd be helpful if someone pointed out which commands I can use to achieve that.
So far I haven't found anyone that has the same question, which is why I'm posting it here. I've found someone with a way to detect spaces in a variable:
if not "%VAR%"=="%VAR: =%"
but no way to replace them.
Any hints?
P.S.: Please, do not recommend me PowerShell or other scripting languages/methods like I saw some people do, I have my reason for using batch and I'm going to stick to it.
So let me explain how substitution works.
set "var=This is a line with spaces"
set "var=%var: =+%"
echo %var%
in the second set we set var to %var% again, but we use substitution of spaces. Everything after : up to = is the search function and everything after = up to the % is the replace function. So %var: =+% means find all the space and replace with + Hence the outcome of the above code will be:
This+is+a+line+with+spaces
Obviously the variable can be manipulated multiple times:
set "var=This is a line with spaces"
set "var=%var: =+%"
set "var=%var:spaces=pluses%
echo %var%
you can also use the substitution to do comparisons without doing substitution using set:
set "var=This is a line with spaces"
if /i "%var: =+%"=="This+is+a+line+with+spaces" echo Matched!
I suggest you read the help by running set /? from cmdline.
I'm creating a simple program that will echo variables from SET /p strings into a nice, neat list. However, I am having trouble creating 2 working IF statements, one using NOT, and one using == to detect if one of my variables, %pwad%, is empty, or contains values. I want to use what the IF statement returns to set variable %finalpwad% to either "No pwad detected" or %pwad%.
How should I properly write this statement? Where might I need corrections, fixing the IF statements or maybe even the part where it sets %pwad% to %finalpwad%?
I have already tried fixing my call part and what they call from, but to no avail. I'm almost sure this is an IF statement issue, as I'm not too good with them, and always struggle reading the notes about the command from IF /?.
Here's a snippet of my code and the source of the problem I am having:
set /p pwad=Set a pwad (or none):
if %pwad% NOT [] call :yespwad & pause
if %pwad% == [] :nopwad & pause
:nopwad
set finalpwad=No pwad detected
goto :printout
:yespwad
set finalpwad=%pwad%
goto :printout
I expect the output to continue onto :printout, where it echoes all the variables the user enters, but it instead exits the program, and makes it so I can't find out whether it properly read my IF NOT or IF == statements. I rudimentarily added pauses to snuff out the problem and see where the source was, and I concluded it must the IF statements.
The help file clearly shows the proper syntax for comparing strings.
IF [NOT] string1==string2 command
It is recommended that you use quotes as well when comparing strings.
IF "string1"=="string2" command
IF comparisons are literal. Each side of the comparison has to match. Using brackets does not check for an empty string.
There also is an option to check if a variable is defined.
IF DEFINED VAR command
Looking at your logic you could essentially do this:
#echo off
set /p "pwad=Set a pwad (or none): "
IF DEFINED pwad (
set "finalpwad=%pwad%"
) ELSE (
set "finalpwad=No pwad detected"
)
Why does the following batch script fail?
Is it because there are 2 sets of double quotes?
How do I get this to work??
SET INCOME-CODE="test (bob, bob2)"
IF "%INCOME-CODE%"=="NULL" (
SET APCINVOICE=%APCINVOICE%,
) ELSE (
SET APCINVOICE=%APCINVOICE%%INCOME-CODE%,
)
Two things.
First, your variable INCOME-CODE actually has quotes in it. Try this short demonstration:
#echo off
set TEST1="this is a string with spaces"
set "TEST2=this is a string with spaces"
echo %TEST1%
echo %TEST2%
Second, you are comparing to the literal string "NULL". There is no NULL value in batch. Please see this post for more details: What is the proper way to test if variable is empty in a batch file... What you probably want to do is compare to an empty string or use one of the methods in the question above if you're working with something more complex.
IF "%INCOME-CODE%"=="" ( ... )
I would like to output a variable to a text file. I have done this in my code, but I have one output within an if statement that I can't get to work.
if not exist "%TuningLog%" (
set Title=Tuning Parameters Saving Log
set LogTitle=--------- %Title% ---------
echo %LogTitle%>> "%TuningLog%"
)
All its supposed to do is check first for the existense of a log file and if it doesn't exist then I want to in the first instance append a title.
But all I get in the log file is " echo is off." If I don't use a variable and just place text there then it works fine.
Can anybody spot the problem? Thanks for reading and for any help!
UPDATE:
I changed my code to. I don't know much about delayed expansion, but I tried this and it happened to work...
if not exist "%TuningLog%" (
setlocal enabledelayedexpansion
set Title=Tuning Parameters Saving Log
set LogTitle=--------- !Title! ---------
echo !LogTitle!>> "!TuningLog!"
endlocal
)
If anyone can provide a reason as to why the first attempt didn't work and this did, then please inform this thread for the purposes of learning. Thank you!
because or early expansion. your variable gets replaced with its value at the moment the block starts. at this time, it is still undefined and thus empty. if you have echo on, you can watch this happening on screen. if you enable delayed expansion, as you did in your second example, it gets only expanded at the moment it is used, so it works as you expect variables to work in "real" programming languages.
EnableDelayedExpansion
causes Variables to be expanded in simple language it causes the system to treat the value of variable and not the variable name itself
Ive seen others address this several years back but they didnt seem to give a working answer.
When I try to pass these two commands in a batch file (they work when done by hand) :
nltest /dsgetsite>c:\windows\temp\site.txt
set /p CurrentADSite<c:\windows\temp\site.txt
but when I attempt to issue the commands via a batch file I get this:
C:\working>nltest /dsgetsite 1>c:\windows\temp\site.txt
C:\working>set /p CurrentADSite 0<c:\windows\temp\site.txt
The syntax of the command is incorrect.
How on earth do I actually get this to work? Is there an easier way to pass the dsgetsite results straight into a variable?
You can use for /f to avoid the temp file (also you [usually] can't write to the Windows directory so that will blow up anyway):
for /f %%x in ('nltest /dsgetsite') do if not defined CurrentADSite set CurrentADSite=%%x
According to http://ss64.com/nt/set.html you may have missed out an =
"To place the first line of a file into a variable:
Set /P _MyVar=<MyFilename.txt "
If all else fails you could always fall back on:
:: start the temp batch with start of set command
echo set CurrentADSite=>c:\windows\temp\site.bat
:: add to the temp bat : the variable assignment
nltest /dsgetsite>>c:\windows\temp\site.bat
:: run the temp batch and return here
call c:\windows\temp\site.bat