Solved

Setting batch command result to variable

Posted on 2007-11-18
9
5,477 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

816 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

8 Experts available now in Live!

Get 1:1 Help Now