Removing or Preventing leading spaces

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
P=W/T
Posts: 4
Joined: 28 Sep 2009 13:34

Removing or Preventing leading spaces

#1 Post by P=W/T » 28 Sep 2009 14:06

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 HKCU\Software\Microsoft\Windows\CurrentVersion\Run /S | FIND /I "123.exe" > %TEMP%\query.txt


Below is an example of the text file that was created...

Code: Select all

    A3 1   REG_SZ   C:\DOCUME~1\user\APPLIC~1\123.exe
    A   REG_SZ   C:\Documents and Settings\user\Local Settings\Temp\123.exe
    A1   REG_SZ   C:\DOCUME~1\user\LOCALS~1\Temp\123.exe
    A2   REG_SZ   C:\Documents and Settings\user\Application Data\123.exe


My question is this:

How can I either strip, or prevent the 4 leading spaces in this text file. My goal is to parse through the file and strip out the 1st token and then surround them in quotes like this:

Code: Select all

"A3 1"
"A"
"A1"
"A2"


...but I'm having difficulty because of the leading TAB/4 spaces that is created during the text file creation. Using this command line generates this output:

Code: Select all

FOR /F "TOKENS=1 DELIMS=    " %A IN (%TEMP%\query.txt) DO @ECHO "%A"

"    A3 1"
"    A"
"    A1"
"    A2"


...And this code generates this:

Code: Select all

FOR /F "TOKENS=1,2* " %A IN (%TEMP%\query.txt) DO @ECHO "%A %B"

"A3 1"
"A REG_SZ"
"A1 REG_SZ"
"A2 REG_SZ"


Thanks in advance for your help.

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 28 Sep 2009 16:10

I think you were close -- try this:

FOR /F "TOKENS=* " %A IN (%TEMP%\query.txt) DO @ECHO.%A

P=W/T
Posts: 4
Joined: 28 Sep 2009 13:34

#3 Post by P=W/T » 29 Sep 2009 06:00

Maybe I'm missing something (this wouldn't be the 1st time), but that command gives me the following results:

Code: Select all

A3 1   REG_SZ   C:\DOCUME~1\user\APPLIC~1\123.exe
A      REG_SZ   C:\Documents and Settings\user\Local Settings\Temp\123.exe
A1     REG_SZ   C:\DOCUME~1\user\LOCALS~1\Temp\123.exe
A2     REG_SZ   C:\Documents and Settings\user\Application Data\123.exe

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#4 Post by avery_larry » 29 Sep 2009 15:28

Sorry -- I was only answering the question about stripping the leading 4 characters like you asked -- I figured you could do the rest.

Your difficulty will be that you do NOT have a set number of tokens. (A3 1 is 2 tokens). So let's see -- you want to strip the leading whitespace, and strip the whitespace preceeding the REG_xxxx

Something like this:


*UNTESTED*

Code: Select all

setlocal enabledelayedexpansion
FOR /F "TOKENS=*" %%A IN (%TEMP%\query.txt) DO (
   set "tmpvar=%%A"
   for /f "tokens=*" %%B in ("!tmpvar:~0,7!") do echo."%%B"
)

P=W/T
Posts: 4
Joined: 28 Sep 2009 13:34

#5 Post by P=W/T » 30 Sep 2009 08:03

Thank you very much for your help. I was able to get exactly what I wanted by using the code below. I had tried something similar to your suggestion before, but was getting nowhere. The ENABLEDELAYEDEXPANSION property was exactly what I was missing. I've implemented the code below into my batch, and it works perfectly. Sorry for the confusion in my original post.

Code: Select all

SETLOCAL ENABLEDELAYEDEXPANSION 
FOR /F "TOKENS=1 DELIMS=   " %%A IN (%TEMP%\query.txt) DO (
   SET "TmpVar=%%A"
   FOR /F "TOKENS=*" %%B IN ("!TmpVar!") DO ECHO."%%B">>%TEMP%\results.txt
)

Post Reply