Luigi
Luigi

Reputation: 486

Store cmd Find result to a variable

I'm trying to store the count result from find /c /i to a variable

find /c /i "sa0" "C:\Users\Luigi.Elsa-PC\Desktop\XBR\Raw\SA00292.01"

This outputs ---------- C:\USERS\LUIGI.ELSA-PC\DESKTOP\XBR\RAW\SA00292.01: 2

I want the number 2 in a variable.

I've tried using for do

C:\Users\Luigi.Elsa-PC\Desktop>for /F %i in ('find /c /i "sa0" "C:\Users\Luigi.E
lsa-PC\Desktop\XBR\Raw\SA00292.01"') do (echo %i )

C:\Users\Luigi.Elsa-PC\Desktop>(echo ---------- )
----------

But as you can see. It's only getting the ---------- not the 2

Upvotes: 0

Views: 1314

Answers (2)

Stephan
Stephan

Reputation: 56189

to get the count only, not the complete line, you have to split it at a suitable delimiter. Splitting at : gives you two tokens, you need the second one:

for /F "tokens=2 delims=:" %i in ('find /c /i "sa0" "C:\Users\Luigi.Elsa-PC\Desktop\XBR\Raw\SA00292.01"') do set /a count=%i
echo %count%

for use in a batchfile, write %%i instead of %i (both times)

Standard-Token is 1, standard delimiters are <space>,<tab>,,,;, so your code gives you the first token delimited with standard delimiters, resulting in ----------.

Upvotes: 1

Luigi
Luigi

Reputation: 486

Add "tokens=*" parameter in For command

for /F "tokens=*" %i in ('find /c /i "sa0" "C:\Users\Luigi.Elsa-PC\Desktop\XBR\Raw\SA00292.01"') do (set var=echo %i)
set var=%var:~-1!%)

Upvotes: 0

Related Questions