I’ve been browsing through the Microsoft TechNet Forums to see whether I can learn something new or maybe give someone a helping hand. Now before I am going to show the solution I gave someone, let me first tell you this.
It was just before having diner that I read the question and already thought of a possible solution, but then it was time for diner, moving away from my laptop I thought it looks like the knowledge on the good old DOS scripting language is slowly disappearing. When I came back from diner I submitted the response (after creating a little test script first). Once I had submitted my response, I noticed that the question was already marked as answered so unfortunately someone was faster in responding than me. Now comes the funny part of the story, it was the person himself who had provided the answer by writing some C# Code to do the job. Now this makes me feel really old
Ok, so much for the introduction, let’s have a look at what the was the problem to solve. The person posting the question wanted to use the REG QUERY command to retrieve a registry value that contains a path string, the problem though was that the folder path contained spaces within the folder name.
To demonstrate the problem I have created the following registry key, as you can see the PATH value has a folder path with spaces.
Now if we run the following command:
reg query HKEY_CURRENT_USER\Software\FooCorp /v Path
the result is as following.
Those that are somewhat familiar with the DOS scripting language will automatically think of using the FOR command and create a script that looks as following:
for /F "Tokens=3" %%a in (‘reg query HKEY_CURRENT_USER\Software\FooCorp /v Path’) do set xpath=%%a
The problem is, that because of the spaces within the path, not the entire path string is returned. The following code does the trick and returns the full path.
for /F "Tokens=3*" %%a in (‘reg query HKEY_CURRENT_USER\Software\FooCorp /v Path’) do set xpath=%%a %%b
7 Replies to “Extract Path with spaces from Registry value”
Thank you! Helped me a lot
Thank you very much.
It solved my spaces problem too.
Thanks a lot!!!!!..it solved like the biggest problem of mine.. 🙂
I am seeing a problem though..
After using the ‘*’ what i am getting is: “C:\ProgramFiles\Common Files\”
but what i am expecting is” “C:\Program Files\Common Files”.
There is no space between “Program” and “Files”, however i see the spaces in other folder names..
pls help me fix it..
This isn’t a general solution. Try running it on input that may, or may not, contain a space.
You will find that %%B will expand literally to %B if there is no space in the input.
Further evidence that batch files were sent to punish us.
I had the same problem and was a little bit disappointed about the drawback with the “space”. Especially if you have more than 1 spaces in the first separation, this will fail. Using the ideas above, I come up with an improved proposal:
for /F “skip=2 tokens=2*” %%a in (‘reg query HKEY_CURRENT_USER\Software\FooCorp /v Path’) do set xpath=%%b
Doesn’t work on Win 10.