-1
@echo off
setlocal enabledelayedexpansion
rem initialize all variables
set counter=1
set groupnumber=1
rem change groupcount value if you want a different number of files per zip
set groupcount=3
set zipfilenamePrefix=archive
rem start looping over...
for %%f in (*) do (
    if not "%%f"=="%~nx0" (
        set fileList=!fileList! %%f
        set /a reminder=!counter!%%!groupcount!
        if !reminder! equ 0 (
            set zipfilename=archive!groupnumber!.tz
            echo Zipping files: !fileList! into !zipfilename!
            rem your zipping utility goes here: input = !fileList! and output = !zipfilename!
            set /a groupnumber=!groupnumber!+1
            set fileList=
        )
        set /a counter=counter+1
    )
)
rem there could be some left over files - last group may be less than 3 files
if !reminder! equ 0 (
    set zipfilename=archive!groupnumber!.tz
    echo Zipping into files: !fileList! !zipfilename!
    rem your zipping utility goes here: input = !fileList! and output = !zipfilename!
)
4

2 回答 2

0

这是使用 7-Zip 创建 ZIP 文件的示例。应用了修复并添加了更多配置变量。

它将创建多个 ZIP 文件,每个 ZIP 中最多 100 个文件(可通过groupcount变量配置)并将 zip 文件保存为MyBackup##.zip,其中##是序列号。

单独键入MAKEZIPS没有任何参数以显示用法。将文件夹中的所有文件(不包括子文件夹)归档C:\My Data并将 ZIP 文件放置到的示例D:\My Backup

MAKEZIPS "C:\My Data" "D:\My Backup"

注意:请勿将 ZIP 文件与源文件夹放在同一文件夹中,否则可能会导致死循环。

如果您使用其他存档程序(例如:WinRAR),您将不得不更改程序路径和最可能的参数。

MAKEZIPS.BAT

@echo off
setlocal enabledelayedexpansion
rem initialize all variables
rem ***config start***
rem change groupcount value if you want a different number of files per zip
set groupcount=100
rem change zipfilenamePrefix value if you want a different base file name
set zipfilenamePrefix=MyBackup
rem change zipfileExt value if you are creating other archive type
set zipfileExt=zip
rem ***config end***
set counter=0
set groupnumber=1
if "%~2"=="" (
  echo Usage: MAKEZIPS {Source Folder} {Target Folder}
  goto :eof
)
if not exist "%~1\nul" (
  echo Source folder not found.
  goto :eof
)
if not exist "%~2\nul" (
  echo Target folder not found.
  goto :eof
)
pushd %2
rem start looping over...
for %%f in (*) do (
    if not "%%f"=="%~nx0" (
        set fileList=!fileList! "%%f"
        set /a counter=!counter!+1
        set /a reminder=!counter!%%!groupcount!
        if !reminder! equ 0 (
            set zipfilename="%~1\%zipfilenamePrefix%!groupnumber!.%zipfileExt%"
            echo Zipping files: !fileList! into !zipfilename!
            rem your zipping utility goes here: input = !fileList! and output = !zipfilename!
            "C:\Program Files\7-Zip\7z.exe" a !zipfilename! !fileList!
            if not exist !zipfilename! (
                echo ZIP creation failed.
                goto :eof
            )
            set /a groupnumber=!groupnumber!+1
            set fileList=
        )
    )
)
rem there could be some left over files - last group may be less than 3 files
if %reminder% gtr 0 (
    set zipfilename="%~1\%zipfilenamePrefix%%groupnumber%.%zipfileExt%"
    echo Zipping into files: %fileList% %zipfilename%
    rem your zipping utility goes here: input = %fileList% and output = %zipfilename%
    "C:\Program Files\7-Zip\7z.exe" a %zipfilename% %fileList%
    if not exist %zipfilename% echo ZIP creation failed.
)
popd
于 2012-08-10T09:41:42.913 回答
0

上面的代码没有做任何事情。

首先,您需要按照评论所说的去做(添加存档实用程序)。然后将代码保存到.bator.cmd文件并执行。

于 2012-08-10T08:54:24.947 回答