0

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

2 Answers 2

1

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
  )
)
Sign up to request clarification or add additional context in comments.

2 Comments

What if this is not a fix list. Sometimes Folder1 is not included in the verifying part but FOLDER2 onwards are included?
Thank you so much dbenham. I used the second command. That is what I needed! Thanks!
0

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)
    )
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.