I want to make a batch-script that counts the times that the word "parent" appear in the file,

http://api.github.com/repos/godzillamesel/disclovestory/commits

I'm currenly downloading this file with:

cd "$env:programfiles\DiscLovestory\sys\update" 
Remove-Item "git_commit.log" 
[Net.ServicePointManager]::SecurityProtocol =  tls12, tls11, tls" 
wget -O git_commit.log http://api.github.com/repos/godzillamesel/disclovestory/commits 
pause

Having this is file is also gonna be needed as a "update-note" to the user. This script went straight to powershell, since i don't know how bitsadmin works, but if you also know how that should work, be my guest to help me out! As my script is based to be a batch-only script.

I want the output to go to: "%programfiles%\DiscLovestory\sys\update\version.log" and also in registry:

REG ADD "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\DiscLovestory" /v DisplayVersion /t REG_SZ /d

In both output places it should be enough to just put version as follow: "x.xx" without quotations.

And if you wanna follow up on the script, or use it, be my guest. It is open-source at: https://github.com/godzillamesel/disclovestory


Solution 1:

You may try this:

@echo off
set /a COUNT=0
for /f %%i in ('findstr /i /c:"parent" commits.txt') do (
  set /a COUNT=COUNT + 1
)
echo "parent" words count: %COUNT% 
pause

Referrence:how to get the word count within batch file in MS DOS and manipulate to exit loop

Solution 2:

We finally figured it out. We used a script in C that a friend of mine made. Was not was i was searching for, but it works. `

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINESZ 1024

int main(int argc, char **argv)
{
    if(argc < 3){
        printf("Usage: count.exe <stack> <needle>\n");
        exit(0);
    }
    char* stack = argv[1];
    char* needle = argv[2];
    int count = 0;
    char buff[LINESZ];
    FILE *fin = fopen (stack, "r");
    if (fin != NULL) {
        while (fgets (buff, LINESZ, fin)) {
            if(strstr(buff,needle)){
                count += 1;
            }
        }
        fclose (fin);
    }
    printf("%d\n",count);
}

It is compiled to https://github.com/godzillamesel/disclovestory/blob/master/scripts/sys/update/count.exe if you are gonna need it. count.exe