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

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Alireza Fattahi
  • 42,517
  • 14
  • 123
  • 173
  • 5
    Ahm... what about removing the spaces?? – aschipfl Jul 11 '17 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. – Jeff Zeitlin Jul 11 '17 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"` –  Jul 11 '17 at 12:46
  • @LotPings I tried `SET "SVN_URL = http://server.test.com"` but still the `echo %SVN_URL%` is not defined. Did I get you wrong ?! – Alireza Fattahi Jul 11 '17 at 12:51
  • 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 ;-) –  Jul 11 '17 at 12:54
  • Dear @LotPings can you please send it as answer I could not get it work ! – Alireza Fattahi Jul 11 '17 at 20:03
  • 1
    Possible duplicate of [Batch File Set Variable not working](https://stackoverflow.com/questions/19448522/batch-file-set-variable-not-working) – phuclv Apr 04 '18 at 10:01

2 Answers2

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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
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.