一批档案文件 [英] Batch to archive files
问题描述
我想提出一个脚本,可以让我为之存档最后修改日期优于30天的指定目录中的所有文件。
该文件应该被移动到一个新的文件夹,这个文件夹必须事后压缩。
I am making a script that can allow me to archive all files of a given directory whom last modified date is superior to 30 days. The files should be moved to a new folder and this folder must be zipped afterwards.
Aditionaly - 这是一个位crutial - 在归档过程中,文件应该按月进行分组。压缩文件夹的名称应标明所包含文件的月份和年份。
Aditionaly - and this is a bit crutial - in the archiving process, the files should be grouped by month. The name of the zipped folder should indicate the month and year of the contained files.
的例如:的
2012_12.zip (包含2012年12月的所有文件);
2013_01.zip (包含2013年1月的所有文件)
Example: 2012_12.zip (contains all files from December 2012) ; 2013_01.zip (contains all files from January 2013)
这是我迄今为止:
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文件读取。
As a bonus feature, it would be very useful if instead of giving the directory as an input, the script would read from a csv file with more than one directory.
我有点在momement丢失。
我预先感谢每答复!
I'm a bit lost in the momement. I thank in advance for every reply!
推荐答案
既然你已经使用VBScript进行压缩和VBScript是对于日期执行运算好,还不如做整件事在VBScript。
Since you're already using vbscript for zipping and vbscript is also better for performing arithmetic with dates, might as well do the whole thing in vbscript.
我没有做的CSV的事情,但也许你可以调用脚本了一批为%%我在(csvfile)做CSCRIPT / NOLOGO archive.vbs %%我
或相似。如果没有传递参数,它归档当前的工作目录。
I didn't do the csv thing, but maybe you could call the script with a batch for %%I in (csvfile) do cscript /nologo archive.vbs %%I
or similar. If no argument is passed, it archives the current working directory.
' 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."
这篇关于一批档案文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!