Remove Carriage return in BATCH file

W

wayne

HELP this is driving me crazy.
I've been learning a lot of great things about batch commands recently, and
often use a | find trick to read things I need from various files or
registry queries. Specifically I use reg.exe a lot to get info out of the
registry and do something with it.
HOWEVER. the reg.exe I've been using is an old version that outputs each
"key" as a single line of text, so that if I use a
| find "SQL" on a key I would get, I can also get the path of what I found.
for example

HKEY_CLASSES_ROOTInstaller\Products\\\7DDFFFA258DE09A4C825D59ABECDB9F8\ProductName
: Microsoft SQL Server 2005 Express Edition CTP (SQLEXPRESS)

But the new reg.exe (That ships with Windows) puts it's output on multiple
lines like so...

HKEY_CLASSES_ROOT\Installer\Products\\7DDFFFA258DE09A4C825D59ABECDB9F8
ProductName REG_SZ Microsoft SQL Server 2005 Express Edition CTP
(SQLEXPRESS)
PackageCode REG_SZ FE472E251BF04D52CDDBFE8D28E45A77
Language REG_DWORD 0x409
Version REG_DWORD 0x900045c
Transforms REG_EXPAND_SZ :SqlRun01.mst;:InstID01.mst;:InstName01.mst
Assignment REG_DWORD 0x1
AdvertiseFlags REG_DWORD 0x184
ProductIcon REG_SZ
c:\WINDOWS\Installer\{2AFFFDD7-ED85-4A90-8C52-5DA9EBDC9B8F}\ARPIcon.ico
InstanceType REG_DWORD 0x1
AuthorizedLUAApp REG_DWORD 0x0
Clients REG_MULTI_SZ :\0\0

Which is pretty, and useful if I want to pipe it to a text file (which I
don't need to do because I could export it instead) but is useless for using
find if I want to get back something else from the key. For example, the
above find would return:

ProductName REG_SZ Microsoft SQL Server 2005 Express Edition CTP
(SQLEXPRESS)

Which tells me nothing about the key itself. In other words, now all I can
do is see if Microsoft SQL Server 2005 Express Edition is installed, I
can't find it's icon, or it's GUID, or it's package code etc...

What I need to know is if there is a way to force it back to the old style,
or if I can have a FOR /f loop break it up on blank lines instead of line
feeds or anything that would let me get ALL of the information about a key
if I find the info in it's query that I'm keying in on.
 
M

Matthias Tacke

wayne said:
HELP this is driving me crazy.
I've been learning a lot of great things about batch commands recently, and
often use a | find trick to read things I need from various files or
registry queries. Specifically I use reg.exe a lot to get info out of the
registry and do something with it.
HOWEVER. the reg.exe I've been using is an old version that outputs each
"key" as a single line of text, so that if I use a
| find "SQL" on a key I would get, I can also get the path of what I found.
for example

HKEY_CLASSES_ROOTInstaller\Products\\\7DDFFFA258DE09A4C825D59ABECDB9F8\ProductName
: Microsoft SQL Server 2005 Express Edition CTP (SQLEXPRESS)

But the new reg.exe (That ships with Windows) puts it's output on multiple
lines like so...

HKEY_CLASSES_ROOT\Installer\Products\\7DDFFFA258DE09A4C825D59ABECDB9F8
ProductName REG_SZ Microsoft SQL Server 2005 Express Edition CTP
(SQLEXPRESS)
PackageCode REG_SZ FE472E251BF04D52CDDBFE8D28E45A77
Language REG_DWORD 0x409
Version REG_DWORD 0x900045c
Transforms REG_EXPAND_SZ :SqlRun01.mst;:InstID01.mst;:InstName01.mst
Assignment REG_DWORD 0x1
AdvertiseFlags REG_DWORD 0x184
ProductIcon REG_SZ
c:\WINDOWS\Installer\{2AFFFDD7-ED85-4A90-8C52-5DA9EBDC9B8F}\ARPIcon.ico
InstanceType REG_DWORD 0x1
AuthorizedLUAApp REG_DWORD 0x0
Clients REG_MULTI_SZ :\0\0

Which is pretty, and useful if I want to pipe it to a text file (which I
don't need to do because I could export it instead) but is useless for using
find if I want to get back something else from the key. For example, the
above find would return:

ProductName REG_SZ Microsoft SQL Server 2005 Express Edition CTP
(SQLEXPRESS)

Which tells me nothing about the key itself. In other words, now all I can
do is see if Microsoft SQL Server 2005 Express Edition is installed, I
can't find it's icon, or it's GUID, or it's package code etc...

What I need to know is if there is a way to force it back to the old style,
or if I can have a FOR /f loop break it up on blank lines instead of line
feeds or anything that would let me get ALL of the information about a key
if I find the info in it's query that I'm keying in on.
Hello Wayne,
I miss some information:
- which windows version
- which reg.exe,
- what is your actual code

Is your sample output from a reg query with /s (subkeys) ?

If you are after special values use /v (assuming reg.exe ver 3.0)

for parsing there are several possible ways.

Here is one to play with:


@echo off
setlocal EnableExtensions
set key=hkcr\installer\products
for /f "tokens=4 delims=\" %%A in (
'reg query "%key%" ^|findstr "products."'
) do (setlocal
for /f "skip=4 tokens=1-2,*" %%B in (
'reg query "%key%\%%A"'
) do set my_%%B=%%D
set my_
endlocal
pause
)


HTH
 
W

wayne

Windows ver is 2000 and up (seems to be when they changed the behavior)
My OLD reg version is 5.0.2
The NEW reg is 5.1.2600.2180
It's with a /list. /s is new, but they do almost the same thing.
/v doesn't help because I don't know the full path I need, I'm looking for
it.


My actual code is stuff like...
====================================
@for /f "tokens=3,4 delims=\" %%a in ('"reg.exe query HKCR\Installer\ /list
| find /i "Microsoft Office" | find /i "ProductName""') do @Call :getSource
%%a "%%b"
@GOTO :EOF

:getSource
@for /f "tokens=3 delims=;" %%a in ('"reg.exe query
HKCR\Installer\Products\%1\SourceList\LastUsedSource /list"') do @echo
%1,%2,"%%a" >> InstalledOffice.csv
@GOTO :EOF

:END

======================================

In this specific example, I'm finding installed office components, then
using the registry key it was found in to find out where it was installed
from, and the GUID they have in the registry, but I do things like this all
the time for various reasons.
 
M

Matthias Tacke

wayne said:
Windows ver is 2000 and up (seems to be when they changed the behavior)
My OLD reg version is 5.0.2
The NEW reg is 5.1.2600.2180
It's with a /list. /s is new, but they do almost the same thing.
/v doesn't help because I don't know the full path I need, I'm looking for
it.

You may use the xp version of reg.exe also in w2k to ease handling with
the newer unique syntax. The is also a more recent version for w2k
hidden somewhere in the ms site.

When using two nested for loops you _can_ use /v because the actual key
used is still present in the var %%A in my example.
You will have to check the contents of ProductName for every single
entry. My example stores all values in environment variables. You only
need to replace the "set my_" statement with your check commands.


Try this one with REG.exe v 3.0

::OfficeCheck.cmd::::::::::::::::::::::::::::::::::::::::::::::::::
@echo off
setlocal EnableExtensions EnableDelayedExpansion
set key=hkcr\installer\products
set prodN=Microsoft Office
for /f "tokens=4 delims=\" %%A in (
'reg.exe query "%key%" ^|findstr "products."'
) do (
setlocal
set my_=%%A
for /f "skip=4 tokens=1-2,*" %%B in (
'reg.exe query "%key%\%%A"'
) do set my_%%B=%%D
if defined my_ProductName (
if "!my_ProductName!" NEQ "!my_ProductName:%prodN%=!" (
call :getSource "%key%\%%A"))
endlocal
)
goto :eof
:getSource
for /f "tokens=3 delims=;" %%E in (
'reg.exe query "%~1\SourceList" /V LastUsedSource'
) do echo %1,"%my_ProductName%","%%E" >> InstalledOffice.csv
GOTO :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

HTH
Matthias
 
W

wayne

O.k. I'll have to take some time trying to understand all of what that does.
Thanks for the response, I'll let you know if I have any questions.
 
W

wayne

I see what you mean about the /v with the nested for, however I don't
understand the line

if "!my_ProductName!" NEQ "!my_ProductName:%prodN%=!"

Specifically I don't know what the ! is doing.
If you have a minute could you shed some light on that?

Thanks.
Wayne
 
F

foxidrive

I see what you mean about the /v with the nested for, however I don't
understand the line

if "!my_ProductName!" NEQ "!my_ProductName:%prodN%=!"

Specifically I don't know what the ! is doing.
If you have a minute could you shed some light on that?

They delimit an environment variable in the same way %var% does, but are
used with EnableDelayedExpansion. See the help on for-in-do

FOR /?
 
W

wayne

Thanks I'll look into it.

foxidrive said:
They delimit an environment variable in the same way %var% does, but are
used with EnableDelayedExpansion. See the help on for-in-do

FOR /?
 
M

Matthias Tacke

foxidrive said:
They delimit an environment variable in the same way %var% does, but are
used with EnableDelayedExpansion. See the help on for-in-do

FOR /?

Thanks for stepping in foxidrive.

If was offline for some days due to an DSL-upgrade (in speed and
hardware) which resulted in 5 days no go.

@wayne: That line checks for presence of "microsoft Office" by deleting
that string (case insensitive) in the found string and comparing with
the original. So I don't have to take care where these words occur in
the string.

HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top