Solved

Setting batch command result to variable

Posted on 2007-11-18
9
5,488 Views
Last Modified: 2008-02-01
I'm currently exporting the value of a registry key into a text file then using the FIND /C command to retrieve the amount of times a certain string appears. This is my code so far:

REG EXPORT "HKLM\SOFTWARE\National_Australia_Group\GLOBAL\CRM Project\Siebel_ActiveX_Messaging" "C:\Blah.txt"

SET count= FIND /C "INT2.CFG" "C:\Blah.txt"

Whenever I Echo count I only get the complete string. If I use the /A parameter for SET (Used for calculations) it tries to treat it as mathematical.

Any idea how I would get the output of that command rather than just the command in the %count% variable?
0
Comment
Question by:doyle007
9 Comments
 
LVL 18

Expert Comment

by:Andrej Pirman
ID: 20309549
Maybe this would work:

FIND /C "INT2.CFG" "C:\Blah.txt">temp.txt
SET /P count= <temp.txt
DEL /Q temp.txt

0
 

Author Comment

by:doyle007
ID: 20309572
Just tried it, no luck =\

I'm also open to any other suggestions as to how to check whether a string exists in a registry key value.
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 350 total points
ID: 20309655
Try this. The routine is necessary to get rid of the leading space from the count.

set count=

for /f "tokens=2 delims=:" %%a in ('FIND /C "INT2.CFG" "C:\Blah.txt" 2^>NUL') do call :SETCOUNT %%a

echo count=%count%

goto :EOF

:SETCOUNT

set count=%~1

goto :EOF
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 1

Expert Comment

by:linmm
ID: 20314445
Another easy way to get rid of that first empty line would be to use the FINDSTR command to pull out the line you need from your temp file.  The snippet below should speak for itself.
REG EXPORT "HKLM\SOFTWARE\National_Australia_Group\GLOBAL\CRM Project\Siebel_ActiveX_Messaging" "C:\Blah.txt"
 
FIND /C "INT2.CFG" "C:\Blah.txt" > C:\FIND.TMP
FINDSTR ":" C:\FINDSTR.TMP
 
SET /P COUNT= < C:\FINDSTR.TMP
 
DEL FIND.TMP
DEL FINDSTR.TMP

Open in new window

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20315288
Did you test that? Trick question, because it won't work.
0
 
LVL 1

Expert Comment

by:linmm
ID: 20316543
You got me :-P  A lot of careless mistakes in that one.  The concepts were in there, but here's one that actually works.
FIND /C "INT2.CFG" "C:\Blah.txt" > C:\FIND.TMP
FINDSTR ":" C:\FIND.TMP > C:\FINDSTR.TMP
 
SET /P COUNT= < C:\FINDSTR.TMP
 
DEL C:\FIND.TMP
DEL C:\FINDSTR.TMP

Open in new window

0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 20317629
Did you test that? Another trick question... That doesn't work either.

If you do this prior to set /p command and this after:

set count=

set /p ...

echo Count=%count%

You'll see something like this:

count=---------- BLAH.TXT: 1

Not what the question asker wants. At least that is my interpretation of the problem.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20649595
Forced accept.

Computer101
EE Admin
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
Introduction: Recently, I got a requirement to zip all files individually with batch file script in Windows OS. I don't know much about scripting, but I searched Google and found a lot of examples and websites to complete my task. Finally, I was ab…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question