批量归档文件

时间:2013-02-21 17:24:10

标签: batch-file robocopy

我正在制作一个脚本,可以让我归档给定目录的所有文件,其最后修改日期优于30天。 应将文件移动到新文件夹,然后必须压缩此文件夹。

Aditionaly - 这有点重要 - 在归档过程中,文件应按月分组。压缩文件夹的名称应指示包含文件的月份和年份。

示例: 2012_12.zip (包含2012年12月以来的所有文件); 2013_01.zip (包含2013年1月以来的所有文件)

这是我到目前为止所做的:

ECHO OFF

ECHO.
SET /p folder=Which folder you want to archive?
ECHO.
ECHO %folder% 

CHDIR %folder%
MKDIR Archive

ROBOCOPY "%folder%" "%folder%\Arquivo" /E /V /ETA /MOVE /XD "%folder%\Archive"
:: Exclude files newer than 30 days

FORFILES /P "%folder%\Archive" /D -31/12/2012 /D +1/12/2012 /C GOTO :ZIP




CALL:ZIP
SET filetozip="%folder%\Archive"
set tempdir=C:\Users\tiago.campos\Documents\OMS\FilesArchiver\tempdir
mkdir %tempdir%
copy %filetozip% %tempdir%
mkdir "%filetozip%\Archive"
rmdir %tempdir%
realdate 

echo Set objArgs = WScript.Arguments > _zipIt.vbs
echo InputFolder = objArgs(0) >> _zipIt.vbs
echo ZipFile = objArgs(1) >> _zipIt.vbs
echo CreateObject("Scripting.FileSystemObject").CreateTextFile(ZipFile, True).Write "PK" ^& Chr(5) ^& Chr(6) ^& String(18, vbNullChar) >> _zipIt.vbs
echo Set objShell = CreateObject("Shell.Application") >> _zipIt.vbs
echo Set source = objShell.NameSpace(InputFolder).Items >> _zipIt.vbs
echo objShell.NameSpace(ZipFile).CopyHere(source) >> _zipIt.vbs
echo wScript.Sleep 2000 >> _zipIt.vbs


CScript  _zipIt.vbs  %tempdir%  "%filetozip%\Archive\2013.01.zip"

del "_zipIt.vbs"

pause

作为一个额外的功能,如果不将目录作为输入,脚本将从具有多个目录的csv文件中读取,这将非常有用。

我有点迷失了。 我提前感谢你的回复!

1 个答案:

答案 0 :(得分:0)

由于你已经使用vbscript进行压缩,而vbscript对于使用日期执行算术也更好,所以也可以在vbscript中完成整个操作。

我没有做csv的事情,但也许你可以用批for %%I in (csvfile) do cscript /nologo archive.vbs %%I或类似的方式调用脚本。如果没有传递参数,它会归档当前工作目录。

' archive.vbs
' usage: cscript archive.vbs (directory, optional)

if not wscript.arguments.count then
    dir = left(WScript.ScriptFullName,(Len(WScript.ScriptFullName))-(len(WScript.ScriptName)))
else
    dir = wscript.arguments(0)
    if not Right(dir, 1) = "\" then
        dir = dir & "\"
    end if
end if

set fso = createobject("scripting.filesystemobject")
set shl = createobject("shell.application")
set osh = createobject("wscript.shell")
set files = fso.getFolder(dir).files
dim zips
dim folders
redim zips(-1)
redim folders(-1)

for each file in files
    if DateDiff("d", file.datelastmodified, Date()) > 30 then
        yyyymm = DatePart("yyyy", file.datelastmodified) & "_" & Right("0" & DatePart("m", file.datelastmodified), 2)
        dest = dir & yyyymm & "\"
        if not fso.folderexists(dest) then
            fso.createFolder dest
            zip = dir & yyyymm & ".zip"
            redim preserve zips(ubound(zips) + 1)
            redim preserve folders(ubound(folders) + 1)
            zips(ubound(zips)) = zip
            folders(ubound(folders)) = dest
        end if
        Wscript.echo "Moving " & file & " to " & dest
        fso.moveFile file, dest
    end if
next

set files = nothing

Wscript.echo "Copying finished."

on error resume next
for i = lbound(zips) to ubound(zips)
    Wscript.echo i & ": Zipping " & folders(i) & " to " & zips(i)
    set zip = fso.createtextfile(zips(i))
    zip.write "PK" & chr(5) & chr(6) & string(18, chr(0))
    zip.close
    set zip = Nothing
    shl.namespace(zips(i)).copyhere shl.namespace(folders(i)).items
    do until shl.namespace(zips(i)).items.count = shl.namespace(folders(i)).items.count
        wscript.sleep 100
    loop

    ' This method of deleting folders is more reliable than fso.deletefolder
    ' for paths with long filenames.
    osh.run "cmd /c rmdir /q /s """ & folders(i) & """", 1, true
next

Wscript.Echo "Done."