7zip 批处理脚本在带有感叹号的文件夹上中断

7zip batch script breaking on folders with exclamation mark

我正在使用批处理脚本将数千个文件夹分别压缩到各自的存档中。我注意到 第 15 个 之后的每个包含 感叹号 (!) 的文件夹都会破坏脚本并出现以下错误:

Scan WARNINGS for files and folders: The system cannot find the file specified.

有问题的脚本:

@Echo OFF
SetLocal EnableDelayedExpansion



Rem //  7-Zip Executable Path
Set sevenZip="C:\Program Files-Zipz.exe"



Rem // START: NewLine Variable Hack
Set newLine=^


Rem // END: NewLine Variable Hack !! DO NOT DELETE 2 EMPTY LINES ABOVE !!



Rem //  Set ErrorLog Variables
Set errorCount=0
Set separator=--------------------------------------------------------
Set errorLog=!newLine!!newLine!!separator!!newLine!!newLine!
Set errorPrefix=ERROR @:
Set successMessage=All Files Were Successfully Archived



Rem //  Loop Through Each Argument
SetLocal DisableDelayedExpansion
for %%x in (%*) do (

    Rem //  Use Current Argument To set File, Folder, & Archive Paths
    SetLocal DisableDelayedExpansion
    Set filePath="%%~x"
    Set directoryFiles="%%~x\*"
    Set archivePath="%%~x.7z"
    SetLocal EnableDelayedExpansion

    Rem //  Source Is A Folder
    if exist !directoryFiles! (
            Set sourcePath=!directoryFiles!
    )

    Rem //  Source Is A File
    if not exist !directoryFiles! (
            Set sourcePath=!filePath!
    )

    Rem //  Print Separator To Divide 7-Zip Output
    echo !newLine!!newLine!!separator!!newLine!!newLine!

    Rem //  Add Files To Zip Archive
    !sevenZip! A -T7Z !archivePath! !sourcePath!

    Rem //  Log Errors
    if ErrorLevel 1 (
        Set /A errorCount=errorCount+1
        Set errorLog=!errorLog!!newLine!!errorPrefix!!sourcePath!
    )
)



Rem //  Print ErrorLog
if !errorCount!==0 (
    Set errorLog=!errorLog!!newLine!!successMessage!
)
Echo !errorLog!!newLine!!newLine!!newLine!



Rem //  Keep Window Open To View ErrorLog
pause

我做错了什么? 已使用 7-Zip 版本 19.00 和 21.01 alpha 进行测试。

你的问题是不平衡setlocal/endlocal。

在您的循环中,您 打开 两个 setlocal,但不会 关闭 它们与 endlocal。
但是setlocal限制为32级,然后就失败了。

您应该在循环右括号前简单地添加两个 endlocal