FOR command paths in quotes

Batch, ASP, JScript, Kixtart, etc.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 14 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
P0vv3R
Posts: 2
Last visit: Tue Jun 10, 2014 2:09 pm

FOR command paths in quotes

Post by P0vv3R »

Hello,

I am extracting certain values out of the registry using REG QUERY by piping them through FIND and then writing the data to a text file. An example of the code...

Code: Select all

REG QUERY HKCUSoftwareMicrosoftWindowsCurrentVersionRun /S | FIND /I "123.exe" > %TEMP%query.txt

Once the text file is in place, I want to use FOR to extract the complete path as a TOKEN so that I can use it later. The text file looks like this:

Code: Select all

    A1 REG_SZ C:DOCUME~1userLOCALS~1Temp123.exe
    A REG_SZ "C:Documents and SettingsuserLocal SettingsTemp123.exe"

The problem I am having is that when I use the FOR command to extract the path:

Code: Select all

FOR /F "TOKENS=3" %%A IN (%TEMP%query.txt) DO ECHO Binary is %%A

I get these results:

Code: Select all

C:>ECHO Binary is C:DOCUME~1userLOCALS~1Temp123.exe
Binary is C:DOCUME~1userLOCALS~1Temp123.exe
C:>ECHO Binary is "C:Documents
Binary is "C:Documents

As you can see, if the path doesn't have spaces everything works great. My question is this; how can I obtain the full path including the quotes using FOR so that I can pass this on to additional code.

Thanks in advance for your help.
User avatar
Gyorgy Nemesmagasi
Posts: 50
Last visit: Wed Mar 16, 2022 12:58 pm

FOR command paths in quotes

Post by Gyorgy Nemesmagasi »

The reg.exe produce a tab separated list. The FOR command use a spaces or tab delimiter. You need to tell the FOR command to use only tab as a delimiter.
Change the command to line below. There is a TAB after the delims= . Ensure your text editor don't change the tab to space. The Notepad is a good choice it inserts tab if you press press TAB button. :)

Code: Select all

FOR /F "TOKENS=3 delims= " %%A IN (%TEMP%query.txt) DO ECHO Binary is %%A
User avatar
P0vv3R
Posts: 2
Last visit: Tue Jun 10, 2014 2:09 pm

FOR command paths in quotes

Post by P0vv3R »

Thanks Gyorgy! I tried using similar code originally, but my editor was in fact converting the TAB to a SPACE. This is exactly what I was looking for. Thanks again!
This topic is 14 years and 5 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked