Page 1 of 1

Set a variable

Posted: 01 Dec 2010 08:33
by ardmore
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

Re: Set a variable

Posted: 01 Dec 2010 12:56
by aGerman
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

Re: Set a variable

Posted: 01 Dec 2010 13:39
by ardmore
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.

Re: Set a variable

Posted: 01 Dec 2010 19:31
by orange_batch
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.