Combining lines of code - batch-file

How can i combine in my bat file
set FILESPATH1=C:\Temp\Test
set FILESPATH2=C:\Temp\Test\Bas
set FILESPATH3=C:\Temp\Test\Dennis
To one single line of code

set "FILESPATH1=C:\Temp\Test"&set "FILESPATH2=C:\Temp\Test\Bas"&set "FILESPATH3=C:\Temp\Test\Dennis"
& separates commands; the quotes are not compulsory, but ensure that the value assigned to the variable doesn't contain invisible trailing spaces.

Depending on what you want: you can use && or & between the three commands.
&& executes the command(s) on its right if the command(s) on its left succeeded but & will execute the command(s) on its right even if the command(s) on its left failed (see this link).
In your case it doesn't really matter which one you use:
set "FILESPATH1=C:\Temp\Test" & set "FILESPATH2=C:\Temp\Test\Bas" & set "FILESPATH3=C:\Temp\Test\Dennis"

Related

How to escape escape "=" or Equal to sign in batch script in a double quote string

SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm&ogbl#inbox"
echo %gmail5%
The output is
H:\local\CODE\Batch scripting\powershell\Config>.\test.bat
H:\local\CODE\Batch scripting\powershell\Config>SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm&ogbl#inbox"
H:\local\CODE\Batch scripting\powershell\Config>echo https://mail.google.com/mail/u/5/?tab=wm & ogbl#inbox
https://mail.google.com/mail/u/5/?tab=wm
'ogbl#inbox' is not recognized as an internal or external command,
operable program or batch file.
I checked the StackOverflow most of the post said that anything in "" is escaped along with =. I cannot figure out,why it gets recognized in echo. My use case is to use these strings in another batch script for vdesk.
vdesk create:4
vdesk on:1 run:%gmail5%
Batch uses & to separate different commands on one line. It does not assume that "..." is a string - it might be, however unlikely, that the " is a legitimate character as a command parameter.
Hence, you need to escape the & with a caret (^) which should work with all "awkward" characters except % for which the escape is % itself.
the thing you are trying to accomplish can be done in following way,your code was all right except every time when you print special characters use ^ or Cmd will confuse it for &(And) operater.
#echo off
SET "gmail5=https://mail.google.com/mail/u/5/?tab=wm^&ogbl#inbox"
echo %gmail5%
pause >nul

Batch scripting - add special character to output for Codeception command

I am writing a bat file to automate the process of the below Codeception command.
php vendor/bin/codecept run tests/acceptance/SigninCest.php:^anonymousLogin$
The problem is that I cannot output the ^ character for example:
set functionNamePrefix=^^
set output=php vendor/bin/codecept run tests/acceptance/SigninCest.php:
set functionName=anonymousLogin
set functionNamePostFix=$
set command=%output%%functionNamePrefix%%functionName%%functionNamePostFix%
the $ symbol is correctly displayed but the ^ is not.
Any advice?
Thanks
Enclose the variable in quotes:
set "functionNamePrefix=^^"
Now the variable %functionNamePrefix% will contain ^.
Special characters such as the %|^ are seen as operators to cmd.
When you set functionNamePrefix=^^ and try to echo it, you effectively allow cmd to utilize the special character. Therefore, echo %functionNamePrefix% will give the more prompt, as cmd is expecting the next input line because of the ^.
When however you double quote a string, you are excluding the character in the current cmd run. It is however also recommended to double quote strings when using set to ensure you eliminate unwanted whitespace. For instance:
set var=value
Note the accidental space after value, this space will form part of the value as long as it exists, so enclose everything in double quotes to play safe and to ensure the special characters are not becoming functions in the current batch run.
set "functionNamePrefix=^^"
set "output=php vendor/bin/codecept run tests/acceptance/SigninCest.php:"
set "functionName=anonymousLogin"
set "functionNamePostFix=$"
set "command=%output%%functionNamePrefix%%functionName%%functionNamePostFix%"

Check if variable contains a comma, without displaying the variable value

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

Executing command using variable in command prompt

#ECHO OFF
set "str1=cd"
set "str2=%~dp0"
set "str5=CPCE Client\EfileServiceClient""
set "str4=%str1% %str2%%str5%"
Echo.%str4%
i want to execute the command which is existed in variable "str4" how to execute it....
Simply type %str4% in the command prompt or state it in a batch file to execute its value as a command line.
However, there are some issues in your code:
there is a closing " too much at the str5 assignment, which must be removed; otherwise, the value of str5 equaled CPCE Client\EfileServiceClient";
there is a space in the value of str5; spaces must be handled correctly, as you would type the command in the command prompt; arguments with spaces should be enclosed within "";
(actually, for the cd command (in str1), spaces do not cause any problems, so it would also work without ""; however, for most commands, spaces most probably will cause troubles)
The following adapted code will work:
#echo off
set "str1=cd"
set "str2=%~dp0"
set "str5=CPCE Client\EfileServiceClient"
set "str4=%str1% "%str2%%str5%""
%str4%
You could do this:
echo %str4% > temp.cmd
call temp.cmd
del temp.cmd
Although I think it would make more sense not to add cd to the string and do this instead:
cd %str4%

How to use a directory that contains round brackets in a batch file

I have a batch file in which the 2 first lines read:
set DIRwhereRUN=C:\UNIVERSITY\testSTABLEunstable(WITHrandomBED)
PUSHD %DIRwhereRUN%
but the batch does not work.
If I create a directory named testSTABLEunstable_WITHrandomBED and copy my stuff there everything works smoothly. Is there a way to make it work with the brackets? I don't want to rename for at least 2 reasons.
It's very difficult - and misleading - to isolate two lines as you have. There's nothing wrong with those two lines.
The difficulty that you are having is with "block" statements like
IF ... (something
something
somethinginvolving DIRWHERERUN
)
This is because batch substitutes the value of any %var% with its the-current value before executing the command(s) and hence misinterprets the ) in %Dirwhererun% as the closing-parenthesis of the IF (or ELSE or FOR.)
The way to overcome this is to "escape" %dirwhererun%'s ) (ie. temporarily suspend its special meaning) - this is done with the caret ^ - which itself is a special character (with the special meaning "the following character is just a character, not a special character".)
So - how to do this?
Here's a demonstration:
#ECHO OFF
setlocal
set "DIRwhereRUN=U:\UNIVERSITY\testSTABLEunstable(WITHrandomBED^)"
SET dir
set DIRwhereRUN=U:\THISWILLBEWRONG(WITHrandomBED^)
SET dir
set DIRwhereRUN=U:\UNIVERSITY\testSTABLEunstable(WITHrandomBED^^)
SET dir
set DIRwhereRUN=U:\UNIVERSITY\testSTABLEunstable(WITHrandomBED)
SET dir
MD %DIRwhereRUN%
PUSHD "%DIRwhereRUN%"
DIR
POPD
SET dirwhererun=%dirwhererun:)=^^)%
SET dir
(Note that I use U: as a temporary drive. I'm creating the directory using your original SET deliberately to show that it's not the SET or normal operations that are causing the problem)
Note that where the set uses quotes around the parameters, the value is applied literally. This form is often used to ensure that stray trailing spaces in lines are not included in the value set into the variable.
Note that the ^ seems ineffectual in the next set - because all it is doing is escaping the ) - and ) is NOT a special character in an ordinary SET
With the third version, the caret is included - but only one, because ^ escapes ^ and ) is an ordinary character.
Then we do all of the operations using the ) unadorned. Obviously attempting to re-create a directory is going to cause an error - but it's because the directory already exists, not because there's anything wrong with the command itself.
As demonstrated, the directory will be listed, so the PUSHD works correctly.
Finally, there's a method of setting a variable dynamically - possibly better set into another variable-name. This is useful where the variable may be read from a file or input from a user - that is, not specified literally.
Well - not quite finally. Two further quirks: First, % is not escaped by ^ but by %, and second, ECHO( appears to be the most flexible form of ECHO (where the character immediately following ECHO, normally space, but may be a number of others) - and doesn't participate in the statement-blocking mechanism.
( and ) are special characters to the shell, so you need to escape them. Cmd.exe's escape character is ^. So you can do the following:
set DIRWHERERUN=C:\UNIVERSITY\testSTABLEunstable^(WITHrandomBED^)
Bill
Use double quotes.
set "DIRwhereRUN=C:\UNIVERSITY\testSTABLEunstable(WITHrandomBED)"
PUSHD "%DIRwhereRUN%"

Resources