0

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 ?!

7
  • 5
    Ahm... what about removing the spaces?? Commented Jul 11, 2017 at 12:27
  • When using the set command, you really want to avoid spaces around the = entirely; always format your set statements as set varname=value and add comments (rem statements) if you want readable documentation. Commented Jul 11, 2017 at 12:33
  • There is no problem with spaces in front of the var name if you need to align the equal sign that heftily ;-) To avoid inadvertant leading/trailing spaces in varname or content I'd double quote them Set "SVN_URL=http://server.test.com" Commented Jul 11, 2017 at 12:46
  • 1
    Yes, in my command there are no spaces around the equal sign. The number of spaces following set don't matter Set "SVN_URL=http://server.test.com" but are removed by this site ;-) Commented Jul 11, 2017 at 12:54
  • 1
    Possible duplicate of Batch File Set Variable not working Commented Apr 4, 2018 at 10:01

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.