1

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

另外——这有点关键——在归档过程中,文件应该按月分组。压缩文件夹的名称应指明所包含文件的月份和年份。

示例: 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 文件中读取,这将非常有用。

我有点迷失在这一刻。我提前感谢每一个回复!

4

1 回答 1

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."
于 2013-02-22T16:34:47.680 回答