3

I am currently trying to write a batch script and at this point, I need to find a string, "null", and determine how many times it is shown in the file.

I'm downloading an API from a website every-time that the script starts and within their API they have "null" every now and then. When their API is downloaded, it is downloaded to a single line and I've been trying this:

findstr /C:"null" %TEMP%\$SELECT%.txt | find /C "null" > $TEMP$\$SELECT$-status.txt

But this only displays a "1" due to the file only being 1 line long.

Is there some other way to output the amount of times that "null" is in the file?

added by barlop
OP has also just mentioned that he can't install 3rd party software.

barlop
  • 25,198

3 Answers3

2

I've got to believe this has been asked and answered before. But here is a simple batch script that can do the job.

::StringCount String File
::
::  Count the number of times that String appears in File.
::  The search is not case sensitive.
::  Enclosing quotes are not considered to be part of the string.
::  The string cannot contain =
::
@echo off
setlocal disableDelayedExpansion
set count=0
for /f usebackq^ delims^=^ eol^= %%A in (%2) do set "ln=%%A"&call :testLine %1
echo %count%
exit /b

:testLine
setlocal enableDelayedExpansion
:testLine2
if defined ln if "!ln:*%~1=!" neq "!ln!" (
  set /a count+=1
  set "ln=!ln:*%~1=!"
  goto testLine2
)
endlocal & set /a count=%count%
exit /b

Command line usage would be

StringCount null yourFile.txt

This could become quite slow with large files.


I've written a hybrid JScript/batch utility called REPL.BAT that can make the job easy and should be quite fast. The utility performs a regex search and replace on lines read from stdin, and writes the result to stdout. It has a fair number of options, including options that simplify this task. The utility is pure script that will run on any modern Windows machine from XP onward. Full documentation is embedded within the script.

Here is how it could be used to solve your problem, assuming the search is case sensitive.

<yourFile.txt repl (null) \n$1\n ax | find /c "null"

If you want the search to be case insensitive

<yourFile.txt repl (null) \n$1\n aix | find /i /c "null"
dbenham
  • 11,794
0

Do you have the option to download a single executable?

What you need is a well-ported grep utility.
e.g. grep (GNU grep) 2.16

The option you want is -c or --count .

As per @dbenham 's comment below, the way to actually count not just lines with 'null', but also multiple instances PER LINE is:

grep -o null file-to-read.txt | grep -c 
Hannu
  • 10,568
-1

This isn't a very purist approach. It requires that you get gnuwin32 with the packages- coreutils and grep.

If somebody can do it with pure batch then all the better in one sense.

But grep and wc is the GNU/linux way which you can do in Windows.

copy con a.a <-- creates the a.a file. Notice it has the rrr string 3 times.

C:\blah>copy con a.a
sdfsdfsdfsrrrsdfsdfsdfdrrrsdfsdf
dsfsdfsddsd
sdfsdfsdjsdjksdjklsdjlksdrrr^Z
        1 file(s) copied.

(the -o on grep -o is important so it doesn't return the whole line. And that's particularly important because if rrr occurs many times on one line you don't just want it to display rrr just once for that line. grep -o gets each occurrence of rrr on a new line. The total number of lines are then the total number of occurrences)

C:\blah>grep -o rrr a.a
rrr
rrr
rrr

C:\blah>grep -o rrr a.a | wc -l
3

C:\blah>
barlop
  • 25,198