Solved

Setting batch command result to variable

Posted on 2007-11-18
9
5,501 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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