Solved echo-ing a FOR command to a new batch file

December 16, 2017 at 21:47:20
Specs: Windows 8
Having trouble echoing a FOR command to a new batch file.

So....

In a command window you would use:

FOR /F "tokens=2* delims= " %y IN ('REG QUERY "HKEY_CURRENT_USER\Software\App\Sub" /v Location') DO SET rkd=%z

Within a bat file you must use:

FOR /F "tokens=2* delims= " %%y IN ('REG QUERY "HKEY_CURRENT_USER\Software\App\Sub" /v Location') DO SET rkd=%%z

But how do I do this:
>> "%file%" echo FOR /F "tokens=2* delims= " %%y IN ('REG QUERY "HKEY_CURRENT_USER\Software\App\Sub" /v Location') DO SET rkd=%%z

- without it stripping one of the '%' on the variables in the new file? Thereby making it fail on execution of new bat file.

I tried adding a third '%', but it doesn't work at all. (deletes most of the center section of the command... odd)

Playing with setlocal enabledelayedexpansion doesn't seem to help in any way.

Or.... is there a better way to extract the registry info in a batch file other than the above tactic?


See More: echo-ing a FOR command to a new batch file

Reply ↓  Report •

#1
December 16, 2017 at 23:55:13
Microsoft Technet page on how to use the ECHO command:

https://technet.microsoft.com/en-gb...

It might show why you are having the problem.


Reply ↓  Report •

#2
December 17, 2017 at 00:45:47
✔ Best Answer
Answering my own question... LOL

After researching another related article here in the archives, and some experimentation, I just figured out the proper syntax for this situation:

>> "%file%" echo FOR /F "tokens=2* delims= " %%%%y IN ('REG QUERY "HKEY_CURRENT_USER\Software\App\Sub" /v Location') DO SET rkd=%%%%z

So, a total of 4 '%' symbols will output the 'correct' 2 needed in the generated file.


So, problem solved. However, if there are alternative solutions to obtaining the registry info i would still be interested in learning other techniques.

message edited by drek


Reply ↓  Report •
Related Solutions


Ask Question