Set a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ardmore
Posts: 2
Joined: 01 Dec 2010 08:07

Set a variable

#1 Post by ardmore » 01 Dec 2010 08:33

Hi, I want to use "find" command to count the number of lines containing the string in a text file. The command is pretty straightforward.
My question is that can I save the result to a variable?
Because I am going to use it in microsoft .net application.
Thanks

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Set a variable

#2 Post by aGerman » 01 Dec 2010 12:56

Try this line:

Code: Select all

for /f "tokens=2,3 delims=:" %%i in ('find /c "SearchString" "file.ext"') do if "%%j"=="" (set /a var=%%i) else set /a var=%%j

The number of lines should be in %var% now.

But do you realy mean it is a good idea to use this in .net?

Regards
aGerman

ardmore
Posts: 2
Joined: 01 Dec 2010 08:07

Re: Set a variable

#3 Post by ardmore » 01 Dec 2010 13:39

I want to introduce it in a console application.

Code: Select all

Console.Write("Count"+lines);

The lines is the number of lines. I use the code like

Code: Select all

Process test = new Process();
test.StartInfo.FileName = "cmd";
test.StartInfo.Arguments = @....


I don't know how to combine your code with my snippet.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Set a variable

#4 Post by orange_batch » 01 Dec 2010 19:31

This is how to run CMD and pass code as an argument:

Code: Select all

cmd /c for /f "tokens=2,3 delims=:" %%i in ('find /c "SearchString" "file.ext"') do if "%%j"=="" (set /a var=%%i) else set /a var=%%j

cmd /c terminates cmd after the code. cmd /k keeps cmd running after the code.

Post Reply