我如何运行下面的脚本

时间:2012-08-10 08:23:35

标签: batch-file

@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!
)

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分: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