Sandeep S D
Sandeep S D

Reputation: 647

"unexpected" error while executing bat file

I am getting this error

Z:\Utilities>Test.bat
    -10314679.html: was unexpected at this time.
Z:\Utilities>

while executing this bat file

@echo off
SETLOCAL EnableExtensions EnableDelayedExpansion

for /f "tokens=1 delims=@" %%A in (_HashList-1.tmp) do (
    call set myParam="%%A"
    call :myParseLine %%myParam%%
)
exit /b

:myParseLine
    call set myParam=%~1
    call set myPartLine=%myParam:~0,8%
    if "%myPartLine%" == "MD5 hash" ( 
        call set myPartLine=%myParam:~12%
        exit /b 
    )
exit /b

The file _HashList-1.tmp contains

MD5 hash of z:\Church\Messages\Emails\19981112-The Stranger- You got to read this.... (fwd)-10314679.html:
966b538d0f52fc66bbb7ef4fd98ec1ca
CertUtil: -hashfile command completed successfully.

Any clue what I am doing wrong?

If I comment out the line

call set myPartLine=%myParam:~12%

then it works. I need to refer to "myParam" there to do further processing.

I am writing a bat file that generates checksum for all the files in a drive and stores the same for later reference.

Upvotes: 0

Views: 349

Answers (2)

dbenham
dbenham

Reputation: 130819

The error is occurring in this block of code

if "%myPartLine%" == "MD5 hash" ( 
    call set myPartLine=%myParam:~12%
    exit /b 
)

The error occurs when the value of variable myParam is:

MD5 hash of z:\Church\Messages\Emails\19981112-The Stranger- You got to read this.... (fwd)-10314679.html:

This line:

    call set myPartLine=%myParam:~12%

expands to

    call set myPartLine=z:\Church\Messages\Emails\19981112-The Stranger- You got to read this.... (fwd)-10314679.html:

The ) in (fwd) is closing your IF block prematurely, and the parser sees -10314679.html: as an error.

You could prevent that error by enclosing your assignment within parentheses as follows:

    call set "myPartLine=%myParam:~12%"

The ) would be quoted, so it won't close the parenthesized block

But your code is a hot mess that really should be completely refactored (rewritten). It is obvious you have picked up snippets of batch coding techniques without understanding what they do, or how and when to apply them. For example:

  • Enabling delayed expansion without ever using it
  • Using CALL unnecessarily/inappropriately

I've got ideas how to more cleanly implement your existing code logic. But I'm not convinced your logic is actually what you want, so I am reluctant to take a stab at cleaning up your code.

Upvotes: 1

Compo
Compo

Reputation: 38579

Here's a basic example of one method which should work on all files in the current directory outputting the results to a text file, hashes.txt.

From the Command Prompt:

For %A In (*) Do @For /F "Delims=" %B In ('CertUtil -hashfile "%A" MD5 2^>Nul^|Find /V ":"') Do @(Echo=%A: %B)>>"hashes.txt"

From a batch file:

@For %%A In (*) Do @For /F "Delims=" %%B In ('CertUtil -hashfile "%%A" MD5 2^>Nul^|Find /V ":"') Do @(Echo=%%A: %%B)>>"hashes.txt"

I'll leave you to read For /? in order to modify the first For loop to recursively process all files in a directory/drive

Upvotes: 0

Related Questions