执行bat文件时出现“意外”错误

时间:2018-10-06 13:59:10

标签: batch-file

我收到此错误

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

在执行此bat文件时

@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

_HashList-1.tmp文件包含

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

任何提示我在做什么错吗?

如果我将这一行注释掉

call set myPartLine=%myParam:~12%

然后它起作用。我需要参考那里的“ myParam”进行进一步处理。

我正在编写一个bat文件,该文件将生成驱动器中所有文件的校验和并将其存储以供以后参考。

2 个答案:

答案 0 :(得分:1)

此代码块中发生错误

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

当变量myParam的值为:

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

此行:

    call set myPartLine=%myParam:~12%

扩展到

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

)中的(fwd)正在过早关闭您的IF块,解析器将-10314679.html:视为错误。

您可以通过将作业放在括号内来防止该错误,如下所示:

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

)被加引号,因此它不会关闭带括号的块

但是您的代码是一团糟,实际上应该完全重构(重写)。很明显,您在不了解批处理编码技术功能或应用方式和时间的情况下选择了这些编码技术。例如:

  • 不使用延迟扩展即可
  • 不必要/不当使用CALL

我已经有了如何更干净地实现现有代码逻辑的想法。但是我不相信您的逻辑实际上就是您想要的,因此我不愿意花时间清理您的代码。

答案 1 :(得分:0)

这是一种方法的基本示例,该方法应适用于当前目录中的所有文件,并将结果输出到文本文件hashes.txt

从命令提示符处:

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

从批处理文件中:

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

我将让您阅读For /?,以便修改第一个For循环以递归方式处理目录/驱动器中的所有文件