I'm trying to do create a batch script to if else statement depending upon if a variable in question contains a "," comma without displaying the variable values.
I have a variable %PATHTMP% which could potentially be one of two values =
" RS_Path = c:\data\RV\R\"
" RS_Path = c:\data\RV\RS\,C:\data\RV2\RS2"
I want to identify if there is a , comma as this will allow me to determine if there are two paths in the variable.
This following work to achieve this, however if a "," is not present it displays the value of the %PATHTMP% in the command line window. I wish NOT to display the value.
#echo off
echo.%PATHTMP%|findstr /C:"," >nul 2>&1
if not errorlevel 1 (
echo Comma , found
) else (
echo Comma , not found
)
The issue is the following line is display the value if no comma is present.
echo.%PATHTMP%|findstr /C:"," >nul 2>&1
Can anyone please advise on how to suppress the variable value in this line when a comma is not present. I've tried using >nul 2>nul without success. Alternatively how to re-write to achieve the same result with a if else as described without display the variable values in the command line. Not its important to display the command line as I am showing user reference information in the command line pre and post this command, so minimising or hiding thee command line windows is not a option.
Thank you
One other ways to check this is to try and remove a comma from the string to see if the variable is still equal to itself.
IF NOT "%PATHTMP:,=%"=="%PATHTMP%" echo comma found
Related
IF "~1" == "" (
pause
set /p training_folder="Enter training folder: "
) else (
pause
set training_folder="%~1" & ::business rules file is assumed to be in here
)
pause
) was unexpected at this time. Batch error
Magoo gave you the traditional answer - Use REM instead of :: when in parentheses.
But there is another option. You can use undefined variable "expansion" anywhere within your code, even in the middle of a line. The "comment" simply expands to an empty string.
set training_folder="%~1" %= business rules file is assumed to be in here =%
An = cannot appear in variable names, except for the undocumented dynamic pseudo variables like %=ExitCode% and %=ExitCodeAscii%. An = can never appear anywhere other than the first character in the name. I use a pair at the beginning and end for the pleasing visual symmetry, and it is guaranteed to never match the name of a variable, no matter the content.
The only restriction is your "comment" cannot contain : or %.
Replace the :: with rem. As it is, there appears to be a label within a code-block, which is not usually allowed.
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"
)
I keep seeing "set y=%y:~0,-2%" or some visual equivalent to it in code, but I have no idea what it's called, what it does, or how it's syntax works; I haven't been able to find an explanation, just more uses of it. Can some one explain?
"Set" sets the value of a Windows variable.
EX: set myvar=abc <= assigns the string "abc" to variable "%myvar"
"%" deferences the current value of a Windows Variable.
EX: echo %myvar% <= prints "abc"
The %y:~0,-2 syntax extracts a substring from the current value of the variable
<= extracts last 2 characters from Windows variable "%y"
Here are some useful links that might help :
Tricks with Command Line Arguments
Command line Parameters
Variable substitution
Set is used to set variables eg.: set /p A= will ask the user for input.
in cmd type set /?
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.
I've recently been trying to make a program to simply store text to a file for later viewing, storing it as a .rar file for security against those who don't understand how to extract the text from the .rar (i.e. the less "techy" people)...
I have, however, encountered an error in the program that results in the <word> not expected at this time followed by the .exe closing when I input add/<word> <word>... (i.e. any multi-word string with spaces in between the words [add/<word>, however, does function properly]).
Is there a special rule that must be followed for storing multi-word strings to a .rar or a file in general (I do, however, know that there is a rule for creating/renaming folders/directories)?
The Program Segment:
:command
cls
set /p journal=<journal.rar
echo %journal%
echo.
set /p command=What would you like to do?
cls
if %command%==exit exit
if %command%==help goto help
if %command%==delete echo START; > journal.rar
if %command:~0,4%==add/ echo "%journal%""%command:~4%;" > journal.rar
if %command:~0,5%==edit/ echo %journal:%command:~5%=%;" > journal.rar
goto command
Excuse me. Your question is not clear. There are several confusing points in it, like "followed by the .exe closing" (which .exe is closing?), and the fact that your question is NOT related to .rar files in any way, nor to "storing multi-word strings". However, I can see the following points in it:
When a variable value is expanded with percent signs this way: %command% you must be aware that the variable is first expanded and then the resulting line is parsed. This mean that the value of the variable may introduce errors in the line. For example, in this line: if %command%==exit exit, if the value of command variable is add/one two three, then the line that is parsed is this: if add/one two three==exit exit that, of course, issue an error! (type if /? for further details).
The way to avoid this problem is enclosing both the variable and the comparison value in quotes; this way, if the value have several words with spaces, the whole value is grouped in the IF command for comparison purposes: if "%command%" == "exit" exit. This must be done in every IF command that use the value of the variable.
In the following line:
if %command:~0,5%==edit/ echo %journal:%command:~5%=%;" > journal.rar
you must be aware that the line is parsed from left to right; this means that you can not nest a %variable% expansion inside another one. The way to solve this problem is first complete a %normal% variable expansion, and then a !delayed! variable expansion that will take the previous expanded value. To do that, insert this line at beginning of your program:
setlocal EnableDelayedExpansion
and change previous line by this one:
if "%command:~0,5%" == "edit/" echo !journal:%command:~5%=!;" > journal.rar
For further details, type set /? and carefully read the sections about "delayed expansion".
Here is a sample that can accept multiple words:
set "command="
set /p "command=What would you like to do? "
cls
if /i "%command%"=="have lunch" goto :food