?
Solved

Setting batch command result to variable

Posted on 2007-11-18
9
Medium Priority
?
5,543 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
8 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 1400 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 
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

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

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

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…
TOMORROW TOMORROW.BAT is inspired by a question I get asked over and over again; that is, "How can I use batch file commands to obtain tomorrow's date?" The crux of this batch file revolves around the XCOPY command - a technique I discovered w…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

749 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