for converts semicolon into space character

February 5, 2010 at 11:23:51
Specs: Windows 7 Ultimate x64, 4 GB
echo %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
would return

Now, I want to save that output to %path% variable.

for /f "tokens=* delims=" %%a in ('echo %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;') do @set path=%%a
would set %path% to
C:\Windows\system32 C:\Windows C:\Windows\System32\Wbem 

As you can notice, semicolons have been replaced with space characters.
How can I prevent that?

See More: for converts semicolon into space character

Report •

February 5, 2010 at 12:34:21
I don't know why it does it. It surprised me. However, to
answer your question, this is how to prevent it:

for /f "tokens=* delims=" %%a in ("%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem") do @set path=%%a

This also has the property that it is more efficient than your
version. Using ('echo x') spawns a subprocess (which takes
up CPU time and memory), echoes x and pipes it back to the
current process. Using ("x") instead just uses the quoted
string directly.

Report •

February 5, 2010 at 13:11:42
It came to my mind to use your method, but I didn't tested it as I thought it won't use the values of variables but their names.

Now I see I should have tried that. :)

As for semicolons being replaced by space characters I just realized it does that since semicolons are used to separate multiple commands here. Putting escape character (^) in front of them would solve this problem.

But I'll use your method since, as you said, it's more efficient.

Thank you very much for your help. :)

Report •

February 5, 2010 at 14:38:09
Hi klint.

Thought you got lost.

Helping others achieve escape felicity


Report •

Related Solutions

February 6, 2010 at 05:20:47
Nah, just thought I ought to do some work instead of hanging out

Report •

February 7, 2010 at 06:45:56
And what if you just do this :

set PATH=%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;

instead of :

echo %SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;

Don't see why you need the FOR loop to break it down and bring back togheter ... The SET command does not have this semi-column problem:

C:\Temp>set ttt=1;2;3

C:\Temp>echo %ttt%


Report •

February 7, 2010 at 17:08:29
Damn... why do I always find the hardest solution first.
I didn't thought using just set will resolve variables.

Now, when I think about that, I see how stupid sometimes I can be...


Report •

Ask Question