2012-08-10 20 views
-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! 
) 

回答

0

腳本上面的代碼沒有做任何事情。

起初,你需要做的評論說什麼(添加存檔實用程序)。然後將代碼保存到.bat.cmd文件並執行該操作。

0

下面是創建使用7-Zip的ZIP文件的例子。修復程序已應用並添加了更多配置變量。

它會創建與每個ZIP(經由groupcount可變配置的)最大的100個文件的多個ZIP文件,並保存在zip文件作爲爲MyBackup ##。拉鍊其中##是順序號。

打字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