Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to set command output to a variable 1

Status
Not open for further replies.

PerditaX

Technical User
Joined
Oct 24, 2006
Messages
3
Location
GB
Hi,

New to batch scripting & have not been able to find examples or info on the following...

want to assign the output of the 'find' command to a variable.

eg: MyVariable = find /c "myString" c:/myfile.log

where MyVariable contains the count of how may times 'myString' appeared in 'myFile'.

Win2000 help didn't have any info, and an internet search was fruitless... so still don't know how to make this 'work'.

Any pointers would be appreciated,

Thanks.
 
This is a simple example. There's probably a better way using sp_executesql, but I'm tired.

DECLARE @myresults varchar(256)
create table #results (rstring varchar(256))
insert into #results
exec xp_cmdshell 'find /c "myString" "c:/myfile.log"'
select @myresults = rstring from #results where rstring is not NULL
select @myresults as find_result
drop table #results

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'll have the roast duck with the mango salsa.
 
And of course, parse the results string after the colon that precedes the find count.

Like I said, I'm tired.

Phil Hegedusich
Senior Programmer/Analyst
IIMAK
-----------
I'll have the roast duck with the mango salsa.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top