【问题标题】:Batch script - Zipping using 7-zip批处理脚本 - 使用 7-zip 压缩
【发布时间】:2022-06-15 13:51:14
【问题描述】:

我正在尝试在 Batch 中创建一个脚本,以便在每个包含具有某种扩展名的文件的文件夹中创建一个 zip,但问题是 7-zip zip 无论如何。即使他没有找到具有良好扩展名的文件,他也会制作一个空 zip。 你比我已经做过的要少。

setlocal enableDelayedExpansion
set /p ext="Write with the . which extension you want to zip. : "
set "currentdir="

for /f "delims=" %%b in ('dir /b /s /a-d "C:\Users\546802\Desktop\Test"') do (
 if "!currentdir!" neq "%%~dpb" ( 
  set "currentdir=%%~dpb"
  for /d %%c IN ("%%~dpb.") do "c:\Program Files\7-Zip\7z.exe" a -mx "%%~dpb%%~nxc.zip" %%c\*%ext% )
)

我该如何解决? 7-zip 是否有避免这种情况的命令? 我阅读了多个有关 7-zip 命令的主题,但没有找到满足我需要的命令。

【问题讨论】:

    标签: batch-file 7zip


    【解决方案1】:

    此 ZIP 存档文件创建任务的批处理文件是:

    @echo off
    setlocal EnableExtensions DisableDelayedExpansion
    set "FileExtension="
    
    rem Prompt the user in a loop until entering really a file extension.
    rem Pressing just RETURN or ENTER results in prompting the user again.
    rem Double quotes are always removed from input string and there must
    rem be entered something else than just straight double quotes one or
    rem more times. A dot at beginning of the file extension is always
    rem removed and there must be entered more than just one dot. The file
    rem extension entered by the user cannot contain / or \ or . or any
    rem other character not allowed in a file extension according to the
    rem definition by Microsoft.
    
    :PromptUser
    set /P "FileExtension=Enter the file extension to zip: " || goto PromptUser
    set "FileExtension=%FileExtension:"=%"
    if not defined FileExtension goto PromptUser
    if "%FileExtension:~0,1%" == "." set "FileExtension=%FileExtension:~1%"
    if not defined FileExtension goto PromptUser
    set "FailedSyntaxCheck=1"
    for /F "delims=*./:<>?\|" %%I in ("%FileExtension%") do if not "%%I" == "%FileExtension%" (goto PromptUser) else set "FailedSyntaxCheck="
    if defined FailedSyntaxCheck goto PromptUser
    
    for /F "delims=" %%I in ('dir "%USERPROFILE%\Desktop\Test" /AD-L /B /S 2^>nul') do if exist "%%I\*.%FileExtension%" "%ProgramFiles%\7-Zip\7z.exe" a -bso0 -bsp0 -mx=9 -r- -tzip -y -- "%%I\%%~nxI.zip" "%%I\*.%FileExtension%"
    endlocal
    

    批处理文件不是 100% 故障安全的。文件扩展名语法验证不是 100%。所以用户仍然可以输入一个对文件扩展名无效的字符串,正如微软在关于Naming Files, Paths, and Namespaces的文档页面上描述的那样。

    如果有一个名为 Test.txt目录 并且用户输入 .txt 或只是 txt 作为文件扩展名,则在此使用简单的 IF 条件尽管Test.txt 是一个文件夹而不是一个文件,但代码是正确的,因此 7-Zip 仍然被执行。如果也应处理此类用例,则可以改进代码以使条件更准确。

    7-Zip 的帮助中描述了使用的 7-Zip 开关。双击7-Zip程序文件夹中的7-zip.chm文件打开帮助,点击列表项Command上的第一个标签Contents行版本并阅读所有关于命令行语法命令开关的参考帮助页面。

    在包含具有指定文件扩展名的文件的文件夹中创建 ZIP 文件。可以在将 "%%I\%%~nxI.zip" 替换为 "%%I.zip" 时在包含具有指定文件扩展名的文件的目录的父目录中创建 ZIP 文件。该问题不包含目录树的明确信息,其中包含执行批处理文件之前和之后的文件,用户输入 txt.cmd 以真正了解此 ZIP 存档文件创建任务的所有要求。

    要了解所使用的命令及其工作原理,请打开command prompt 窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。

    • dir /?
    • echo /?
    • endlocal /?
    • for /?
    • goto /?
    • rem /?
    • set /?
    • setlocal /?

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 2013-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多