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.

4
  • Wait, could you be able to find certain strings in a file and then output those to a file using append (>>) and the use my command that I was using earlier?
    – user347636
    Jul 18, 2014 at 14:26
  • Given that you cannot install anything: (1) Could you use vbscript? (2) Could you find something in that output you could search and replace to break it up into multiple lines? (for example, replace > with > and then a newline)
    – Richard
    Jul 18, 2014 at 16:33
  • @Ramhound where did you read that rule? (quote the rule)
    – barlop
    Jul 21, 2014 at 3:24
  • @Ramhound There is typically more nuance than that, when people accept or reject an edit done by another person. They are very often accepted. The reasons why people reject an edit, just like the reasons why people may downvote an answer or upvote an answer, can be just as nuanced, which is more nuanced than you present when you say "invalid edit" and "shouldn't be made by another person, because of a comment". One can just as easily say valid edit. It's a question of whether it's deemed helpful/unhelpful by the questioner.
    – barlop
    Jul 21, 2014 at 13:37

3 Answers 3

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"
1
  • =) now, that is typical for BAT files... isn't it.
    – Hannu
    Jul 18, 2014 at 20:58
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 
2
  • No, that will count the number of lines that contain the string. grep -o null someFile.txt|grep -c . should work.
    – dbenham
    Jul 18, 2014 at 20:27
  • Ahh sorry, you're completely right. But still -- I didn't tell how to USE it ;-)
    – Hannu
    Jul 18, 2014 at 20:29
-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>
1
  • Yeah sadly I'm not able to install third-party software. Thanks for the help though.
    – user347636
    Jul 18, 2014 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .