enderland
enderland

Reputation: 14145

Use find to return only the number

I have an external process which essentially needs to call:

int myInt = exec('find /v /c "" filepath');

It's counting the number of lines in a text file. If there's a better way to get the integer amount that'd also work great.

With find, the full command returns a fair bit of information other than just the count, for example it returns some dashes for each occurence, the filename, and then :10.

So:

---------- filepath :10

Is there any way to make find only return the integer part? Or do I need to pipe that output into another process (similar to grep on unix)?

Upvotes: 3

Views: 5483

Answers (5)

aschipfl
aschipfl

Reputation: 34929

You could try to replace the find /v /c "" filepath portion by the following command line:

cmd /V:ON /C "for /F "delims=" %L in ('find /V /C "" "filepath"') do set "LINE=%L" & echo LINE=!LINE:*: =!"

Or you create a batch file, say line_cnt.bat, with the following similar code, which you call in place of your find command line:

@echo off
setlocal EnableDelayedExpansion
for /F "delims=" %%L in ('
    find /V /C "" "filepath"
') do (
    set "LINE=%%L"
    echo !LINE:*: =!
)
endlocal

In order for this to work, you need either to place it in the proper working directory of your process, to call line_cnt.bat using its full path, or to include its containing directory in the search PATH of your system.

Upvotes: 1

enderland
enderland

Reputation: 14145

You can do this using this syntax:

type filepath | find /c /v ""

Original source here.

type filepath

returns the lines of the file, while

find /c /v ""

returns the number of them (apparently the "" search term is a bug feature of find allowing you to count lines).

Upvotes: 1

Marichyasana
Marichyasana

Reputation: 3154

This works for me on Windows 7:

type pretty-print-example.R |\windows\system32\find.exe /i /v /c ""

output is just the number of lines, 82

Upvotes: 0

Stephan
Stephan

Reputation: 56189

find gives the Output ---------- file.txt :10 when working with files. If it processes STDIN, it gives you only the number:

find /v /c "" <file.txt

to get this Output into a variable, use this for Statement:

for /f %%i in ('find /v /c "" ^<file.txt') do set myint=%%i
echo %myint%

(for use on the commandline, use single %i instead of %%i)

Upvotes: 2

user1345223
user1345223

Reputation:

I believe you'll need to pipe the results of find into another utility such as findstr2.

Reason being: The /C option Displays only the count of lines containing the string..

Depending upon the search filepath you provide, there could be multiple entries that return. In order to make sure the results are meaningful, find has to provide you with the filepath in order to provide you with the appropriate context.

So you would want to issue something1 like:

int myInt = exec('find /v /c "" 'filepath' | findstr /r ': [0-9]*');

1 Please note: I haven't validated that regex within findstr will work.
2 Apparently findstr returns the entire line as well so you'll need to find a different command.

Upvotes: 1

Related Questions