Asked  7 Months ago    Answers:  5   Viewed   26 times

I need to set values to a environmental variable using a batch file. I wrote the script for this:

@echo off
set value="Hello world"
setx -M srijani "%srijani%;%value%"

It gives the error:

ERROR: Invalid syntax. Default option is not allowed more than '2' time(s).
Type "SETX /?" for usage.

I googled and found that while using white spaces we need to write it inside a double quotes.

set value="Hello world"

But, that is not working too.

Note: I am on Windows 7.



The error output by command setx is caused by wrong usage of the quotes on assigning the string to variable value.

The command is set and the parameter is variable=value. As for most commands and applications it is possible and often required to surround a parameter with double quotes if containing 1 or more spaces or any other character from this list: &()[]{}^=;!'+,`~. Those characters are displayed on last help page output by running in a command prompt window cmd /? or help cmd.

But wrong is here:

set value="Hello world"

With first double quote after the equal sign the entire parameter variable=value of command set is not enclosed in double quotes.

This results in interpreting the double quotes as part of the string to assign to variable with name value. Everything from the equal sign to end of line including the double quotes and possibly existing trailing spaces and horizontal tabs is assigned here to variable value instead of just the string Hello world as expected.

On expanding the line

setx -M srijani "%srijani%;%value%"

the result is therefore:

setx -M srijani "Value of variable srijani;"Hello world""

And command setx interprets the wrong quoted parameter as syntax error.

Correct would be using:

set "value=Hello world"

Now the entire parameter of command set is enclosed in double quotes. Therefore ignored on parsing the line are:

  • all spaces/tabs between command set and the first double quote,
  • the first double quote,
  • the last double quote,
  • and all perhaps existing spaces/tabs after last double quote.

So just Hello world is assigned to a variable with name value.

For more details about correct assignment of a string to an environment variable read answer on Why is no string output with 'echo %var%' after using 'set var = text' on command line? It contains also a simple demo batch code.

Some more information:

How an argument string containing 1 or more quotes somewhere in the middle is interpreted depends on command respectively application. The behavior on interpreting an argument with 1 or more " within an argument string can vary depending on used compiler as explained in an answer on batch file: list rar file in specific folder and write result into text file and of course the source code of the command / application.

For most commands and applications the correct syntax is:

command "parameter in quotes"
"Path to applicationapp.exe" "parameter in quotes" 

But there are applications which require quotes in the middle of an argument string. An example of such an application is Windows Explorer.

The following syntax is required to open an Explorer window from within a batch file with current directory displayed in window.

explorer.exe /e,"%CD%"

Not working are:

explorer.exe "/e,%CD%"
explorer.exe /e "%CD%"

So explorer.exe requires that the directory to open is specified after /e, with quotes in the middle of parameter string or it interprets "/e,%CD%" respectively "/e %CD%" as name of the directory with path to display in Explorer window.

See also SS64 - Windows Explorer command-line options.

Tuesday, June 1, 2021
answered 7 Months ago

Another solution:

for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /format:list') do set datetime=%%I

It will give you (independent of locale settings!):

( YYYYMMDDhhmmss.<milliseconds><always 000>+/-<minutes difference to UTC>  )

From here, it is easy:

set datetime=%datetime:~0,8%-%datetime:~8,6%

For Logan's request for the same outputformat for the "date-time modified" of a file:

for %%F in (test.txt) do set file=%%~fF
for /f "tokens=2 delims==" %%I in ('wmic datafile where name^="%file:=\%" get lastmodified /format:list') do set datetime=%%I
echo %datetime%

It is a bit more complicated, because it works only with full paths, wmic expects the backslashes to be doubled and the = has to be escaped (the first one. The second one is protected by surrounding quotes).

Tuesday, June 1, 2021
answered 7 Months ago

Most run configurations have an option to set environment variables:

enter image description here

Sunday, August 22, 2021
answered 4 Months ago

You could create a new .env on your ec2 instance and add all the env vars in there. One option would be ssh-ing into the box and creating the file via vi or cat. Or you could write a script to remotely pull the .env in from an external location.

You could also ssh into the box and export APP_ENV=production all your env vars (assuming that's the right command for your OS).

Adding env vars to your environment will depend on the OS that your ec2 instance is running, so the solution will depend on that. ec2 has a concept of 'tags' which might be useful, but the docs show they limit the number of tags to 10, so you may have to do it manually and per ec2 instance :/

See here for one method that uses tags to pull in and set env vars (non-laravel specific).

I just went through this yesterday while getting Laravel running on Elastic Beanstalk, the solution was clean. You can actually set the env vars directly via the aws console (EB app/environment -> Configuration -> Software Configuration -> Environment Properties).


The key concept to understand is that Laravel just uses phpdotenv to dump vars from the .env file into php's global $_ENV, whereas any already existing env vars are automatically included in $_ENV when php starts the server (docs). So the .env file itself is unnecessary, really just a dev convenience. (unless I've just been spoiled by elastic beanstalk so far).

Friday, September 10, 2021
answered 3 Months ago

Just call labels and have the colors referenced inside the labels:

@echo off
for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a"
call :green
call :red
git pull && call :green || call :red

goto :eof
echo %ESC%[92mSuccess%ESC%[0m
goto :eof
echo %ESC%[91mFailed%ESC%[0m
goto :eof

And the result:

enter image description here

Note the sequence of the conditional operators are:

command && errorlevel is 0 || errorlevel is larger than 0

in other words, command and if successful or if not successful

Sunday, October 3, 2021
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :