2

I am using a command:

find /c "abc" "C:\Users\abc\Desktop\project\string.txt"

Output:

---------- C:\Users\abc\Desktop\project\string.txt: 4

I want to assign this value 4 to a variable so that I can use it for an if statement.

aschipfl
  • 31,767
  • 12
  • 51
  • 89
  • Possible duplicate: [Batch - Assign Command output to Variable](http://stackoverflow.com/questions/16203629/batch-assign-command-output-to-variable) – aschipfl Sep 29 '16 at 12:25

3 Answers3

2

I would use:

For /F %%A In ('Find /C "abc"^<"C:\Users\abc\Desktop\project\string.txt"') Do (
    Set "mlc=%%A")

Your %mlc% varaiable would hold the matched line count.

Compo
  • 34,184
  • 4
  • 22
  • 36
0

I'm not sure if this is the best method to do this, but it works:

find /c "abc" "C:\Users\abc\Desktop\project\string.txt" > tmpFile 
set /p myvar= < tmpFile 
del tmpFile 
Sujeet Sinha
  • 2,222
  • 2
  • 16
  • 25
0

with your snytax the output is ---------- C:\Users\abc\Desktop\project\string.txt: 4
There is another syntax: type file.txt|find /c "abc", which gives you a beautiful output of just:

15

To get it into a variable, use a for /f loop:

for /f %%a in ('type file.txt^|find /c "abc"') do set count=%%a
echo %count%

(for use directly on commandline (not in a batchfile) use %a instead of %%a)

Stephan
  • 50,835
  • 10
  • 55
  • 88