捐血一袋救人一命

2010年4月21日 星期三

自動將 Exchange Archive SPAM 按照日期歸檔

使用 Exchange 2003 的 SPAM Archive 功能,會發現每天都有一堆信被封存,

才隔幾天,要開啟 Archive Folder時,卻系統卻會停止回應。

因為檔案太多,系統很忙碌的要一次讀取所有的檔案清單,

所以才有寫以下幾支程式的構想。

這支程式一執行,會將 E:\Exchsrvr\UCEArchive目錄下所有封存的 SPAM,按照日期格式 YYYYMMDD 歸檔存到 YYYYMMDD 目錄下,這樣子每個 Folder 的檔案數量不大,開啟資料夾速度就快多了。 ( Batch File )

@echo off
setlocal EnableDelayedExpansion
for /F %%a in ( 'dir E:\Exchsrvr\UCEArchive\*.EML /b' ) do (
set FileName=%%a
set DateDir=!FileName:~5,8!
if not exist E:\Exchsrvr\UCEArchive\!DateDir! mkdir E:\Exchsrvr\UCEArchive\!DateDir!
move E:\Exchsrvr\UCEArchive\!FileName! E:\Exchsrvr\UCEArchive\!DateDir!
)

這支程式只會抓今天的日期,將今天的 SPAM 歸檔 ( Batch File )

@echo off
for /f "tokens=1-3 delims=/ " %%a in ('date /t') do (set date=%%a%%b%%c)
if not exist E:\Exchsrvr\UCEArchive\%date% mkdir E:\Exchsrvr\UCEArchive\%date%
move E:\Exchsrvr\UCEArchive\ARCH_%date%*.eml E:\Exchsrvr\UCEArchive\%date%

這支程式會抓昨天的日期,將昨天的 SPAM 歸檔 ( Windows Script Host )

' 取得昨天的日期
dtmYesterday = Date() - 1

' 將昨天的日期取出年份
strYear = left(dtmYesterday,4)

' 將昨天的日期取出月份
if mid(dtmYesterday,7,1) = "/" then
strMonth = "0" & mid(dtmYesterday,6,1)
else
strMonth = mid(dtmYesterday,6,2)
end if

' 將昨天的日期取出日期
if mid(dtmYesterday,len(dtmYesterday)-1,1) = "/" then
strDay = "0" & right(dtmYesterday,1)
else
strDay = right(dtmYesterday,2)
end if

strYesterday = strYear & strMonth & strDay

' 以昨天的日期來建立目錄
ParentFolder = "E:\Exchsrvr\UCEArchive"
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.NameSpace(ParentFolder)

objFolder.NewFolder strYesterday

' 將檔名包含昨天日期的檔案,全數搬移到昨天日期的目錄下
Set objFSO = CreateObject("Scripting.FileSystemObject")
FileNamePatten = "ARCH_" & strYesterday & "*.EML"
objFSO.MoveFile "E:\Exchsrvr\UCEArchive\" & FileNamePatten , "E:\Exchsrvr\UCEArchive\" & strYesterday

wscript.echo strYesterday & " Job is done !"

以下這支程式,會跳出視窗,讓您輸入日期,將指定日期的 SPAM 歸檔 ( Windows Script Host )

strDay = inputbox("請輸入日期 : ")

' 以昨天的日期來建立目錄
ParentFolder = "E:\Exchsrvr\UCEArchive"
set objShell = CreateObject("Shell.Application")
set objFolder = objShell.NameSpace(ParentFolder)

objFolder.NewFolder strDay

' 將檔名包含昨天日期的檔案,全數搬移到昨天日期的目錄下
Set objFSO = CreateObject("Scripting.FileSystemObject")
FileNamePatten = "ARCH_" & strDay & "*.EML"
objFSO.MoveFile "E:\Exchsrvr\UCEArchive\" & FileNamePatten , "E:\Exchsrvr\UCEArchive\" & strDay

wscript.echo strDay & " Job is done !"

這是改用 Batch File 語法寫的程式,功能與上一支程式相同

set /p DateDir="請輸入日期:"
if not exist E:\Exchsrvr\UCEArchive\%DateDir% mkdir E:\Exchsrvr\UCEArchive\%DateDir%
move E:\Exchsrvr\UCEArchive\ARCH_%DateDir%*.eml E:\Exchsrvr\UCEArchive\%DateDir%

反應:

0 意見: