Koya JSev
Koya JSev

Reputation: 25

count number of rows per text file using batch file

I have this batch command that counts the number rows per log/text file. If I have a text file that has 3 or more rows meaning my md5deep verification has a mismatch. The problem is I have multiple folders with CheckMismatch.txt. I am looking for a looping that will CHECK all CheckMismatch.txt and count the rows one by one.

IF EXIST C:\BACKUPS\WEBAPP-UAT\WEBAPP\FOLDER1 (md5deep64 -x "C:\BACKUPS\WEBAPP-UAT\WEBAPP\FOLDER1\FOLDER1.md5" -r "C:\BACKUPS\WEBAPP-UAT\WEBAPP\FOLDER1" > C:\BACKUPS\WEBAPP-UAT\WEBAPP\FOLDER1\CheckMismatch.txt
@echo off
setlocal EnableDelayedExpansion
set "cmd=findstr /R /N "^^" C:\BACKUPS\WEBAPP-UAT\WEBAPP\FOLDER1\CheckMismatch.txt | find /C ":""
for /f %%a in ('!cmd!') do set number=%%a
if %number% gtr 2 (echo there is a mismatch in FOLDER1) ELSE (echo FOLDER1 HASHES matched)
)
  1. Folder1 CheckMismatch.txt
  2. Folder2 CheckMismatch.txt
  3. Folder3 CheckMismatch.txt
  4. Folder4 CheckMismatch.txt
  5. Folder5 CheckMismatch.txt

Upvotes: 0

Views: 525

Answers (2)

dbenham
dbenham

Reputation: 130819

There is no need for FINDSTR before FIND. You can count the rows directly with FIND alone. And you can use FINDSTR to test if the count is less than or equal to 2. This enables you to use && and || instead of IF.

If you want to process a fixed list of folders under "C:\BACKUPS\WEBAPP-UAT\WEBAPP\"

@echo off
setlocal
set "root=C:\BACKUPS\WEBAPP-UAT\WEBAPP"
for %%F in (
  Folder1
  Folder2
  Folder3
  Folder4
  Folder5
) do (
  md5deep64 -x "%root%\%%F\%%F.md5" -r "%root%\%%F" >"%root%\%%F\CheckMismatch.txt"
  type "%root%\%%F\CheckMismatch.txt" | find /c /v "" | findstr /x "0 1 2" && (
    echo There is a mismatch in %%F
  ) || (
    echo %%F hashes matched
  )
)

It is more likely that you want to process all folders under "C:\BACKUPS\WEBAPP-UAT\WEBAPP\"

@echo off
for /d %%F in ("C:\BACKUPS\WEBAPP-UAT\WEBAPP\*") do (
  md5deep64 -x "%%F\%%~nxF.md5" -r "%%F" >"%%F\CheckMismatch.txt"
  type "%%F\CheckMismatch.txt" | find /c /v "" | findstr /x "0 1 2" && (
    echo %%~nxF hashes matched
  ) || (
    echo There is a mismatch in %%~nxF
  )
)

If CheckMismatch.txt is just a temporary file that you don't need later on, then you can avoid creation of the file entirely

@echo off
for /d %%F in ("C:\BACKUPS\WEBAPP-UAT\WEBAPP\*") do (
  md5deep64 -x "%%F\%%~nxF.md5" -r "%%F" | find /c /v "" | findstr /x "0 1 2" && (
    echo %%~nxF hashes matched
  ) || (
    echo There is a mismatch in %%~nxF
  )
)

Upvotes: 1

user6811411
user6811411

Reputation:

There was a delayed expansion problem (using %number% in a code block instead of !number!)
Tried to format more clearly.

@Echo off
SetLocal EnableExtensions EnableDelayedExpansion
Set "Folder=C:\BACKUPS\WEBAPP-UAT\WEBAPP\FOLDER1
IF EXIST "%Folder%" (
    md5deep64 -x "%Folder%\FOLDER1.md5" ^
              -r "%Folder%" > "%Folder%\CheckMismatch.txt"
    set "cmd=findstr /R /N "^^" "%Folder%\CheckMismatch.txt" | find /C ":""
    Set "number=0"
    for /f %%a in ('!cmd!') do set number=%%a

    if !number! gtr 2 (
        echo there is a mismatch in FOLDER1
    ) ELSE (
        echo FOLDER1 HASHES matched)
    )
)

Upvotes: 0

Related Questions