如何使用 7-zip 对文件夹中的文件(每个文件)分别进行压缩?

如何使用 7-zip 对文件夹中的文件(每个文件)分别进行压缩?

我在 Windows 7 中使用 7-zip,并尝试为文件夹中的自动 zip 文件创建批处理。

我希望文件夹中的每个文件都单独压缩。

你能看看我的代码并建议如何最好地做到这一点吗?

我成功将其从一个文件夹压缩到另一个文件夹,但还需要对每个文件分别进行操作:

@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%

Set TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%

ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
ECHO.
7za a -tzip "C:\new-drive-%TODAY%.zip" "C:\zipush*" -mx5
ECHO.

ECHO Delete the files in orginal folder 
DEL "C:\zipush\*.*"
PAUSE

答案1

下面是您需要执行的操作,以完成一个循环,将每个文件压缩到其自己的 zip 文件中,并使用命名约定<orignalfilename>.<extension>.zip,然后删除原始如果前一个文件的 zip 压缩成功。如有必要,您可以更改%%~NXA.zip%%~NA.zip在新的 zip 文件中不包含原始文件扩展名。

您还应该确保如果目标目录尚不存在则创建它,以便存档命令在循环中按预期工作,所以我将其添加到脚本逻辑中,并且我还将源目录和目标目录设置为顶部的变量。

我还通过为了循环来处理单个 zip 和文件名。查看更多资源来自有关此主题的一些学习材料(FOR /?)。


脚本示例

@ECHO OFF
SET hr=%time:~0,2%
IF %hr% lss 10 SET hr=0%hr:~1,1%

SET TODAY=%date:~4,2%-%date:~7,2%-%date:~10,4%-%hr%%time:~3,2%%time:~6,2%%time:~9,2%

SET SrcDir=C:\zipush
SET DestDir=C:\new-drive-%TODAY%
IF NOT EXIST "%DestDir%" MD "%DestDir%"

ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new
ECHO.

ECHO Compressing files and folders in C:\zipush drive and moving to C:\new and then delete from C:\zipush
ECHO.
FOR %%A IN ("%SrcDir%\*.*") DO 7za a -tzip "%DestDir%\%%~NXA.zip" "%%~A" -mx5 && DEL /Q /F "%%~A"
ECHO.
PAUSE

更多资源

  • 为了
  • 条件执行
  • 医学博士

  • FOR /?从命令提示符

    In addition, substitution of FOR variable references has been enhanced.
    You can now use the following optional syntax:
    
    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path name
    %~dI        - expands %I to a drive letter only
    %~pI        - expands %I to a path only
    %~nI        - expands %I to a file name only
    %~xI        - expands %I to a file extension only
    %~sI        - expanded path contains short names only
    %~aI        - expands %I to file attributes of file
    %~tI        - expands %I to date/time of file
    %~zI        - expands %I to size of file
    %~$PATH:I   - searches the directories listed in the PATH
                   environment variable and expands %I to the
                   fully qualified name of the first one found.
                   If the environment variable name is not
                   defined or the file is not found by the
                   search, then this modifier expands to the
                   empty string
    

相关内容