Alireza Fattahi
Alireza Fattahi

Reputation: 45465

Windows CMD Set variable with space in variable name

To make my batch file readable, I tried to align the SET statements as below

SET SVN_URL           = http://server.test.com
SET SVN_USER_NAME     = foo
SET SVN_USER_PASSWORD = pass

How ever when I try to echo %SVN_URL% I got nothing. I found that the variable names could have spaces https://ss64.com/nt/set.html

So my variable will be %SVN_URL % (with spaces)

Is it any way to fix it ?!

Upvotes: 0

Views: 2641

Answers (2)

user6811411
user6811411

Reputation:

Similar to Magoo's first version you can align the equal signs to fit your aestetics or whatever.

SET           "SVN_URL=http://server.test.com"
SET     "SVN_USER_NAME=foo"
SET "SVN_USER_PASSWORD=pass"

In a comment excessive white space is removed, so I couldn't demonstrate.

Upvotes: 1

Magoo
Magoo

Reputation: 79982

The issue is that spaces are significant on both sides of the set

This would set your values (noting that each value will contain a leading space)

SET           SVN_URL= http://server.test.com
SET     SVN_USER_NAME= foo
SET SVN_USER_PASSWORD= pass

or

for /f "tokens=1*delims== " %%a in (SVN_URL           = http://server.test.com) do set "%%a=%%b"
for /f "tokens=1*delims== " %%a in (SET SVN_USER_NAME     = foo) do set "%%a=%%b"
for /f "tokens=1*delims== " %%a in (SET SVN_USER_PASSWORD = pass) do set "%%a=%%b"

or

call :setv SVN_URL           = http://server.test.com
call :setv SVN_USER_NAME     = foo
call :setv SVN_USER_PASSWORD = pass

...
:setv
set "%~1=%~2"
goto :eof

noting that with this last, you may need to enclose the value in quotes.

Upvotes: 2

Related Questions