Solved

Setting batch command result to variable

Posted on 2007-11-18
9
5,452 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
 
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now