Exchange Server Share

January 3, 2008

VBScript: Search Files Older than 30 Days (including sub folder) and Move to Another Location

Filed under: Exchange,VBScript — Amit Tank @ 6:47 am

Sometime we need an automated task to move some of the files from one location to another, examples are bleow.
· SMTP Logs
· SPAM Product Logs
· Some Application Logs
· Temporary Files

So I have created a script that allows you to search all files in a location (including sub folders) which are older than 30 days, move it to specified location and record it into a log file “Move-Result.txt”.

Give the input path where you want to search the files at below line in the script.
SPath = “c:\temp\”

Give the path of destination location at below line in the script.
objFSO.MoveFile FileName, “d:\test\”

====== Script Code ======
Dim objFSO, ofolder, objStream

Set objShell = CreateObject(“WScript.Shell”)
Set objFSO = CreateObject(“scripting.filesystemobject”)
Set objNet = CreateObject(“WScript.NetWork”)
Set FSO = CreateObject(“Scripting.FileSystemObject”)
set outfile = fso.createtextfile(“Move-Result.txt”,true)
SPath = “c:\temp\”

ShowSubfolders FSO.GetFolder(spath)

Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
CheckFolder(subfolder)
ShowSubFolders Subfolder
Next
End Sub

‘CheckFolder(objFSO.getFolder(SPath))

Sub CheckFolder(objCurrentFolder)
Dim strTempL, strTempR, strSearchL, strSearchR, objNewFolder, objFile
Const OverwriteExisting = TRUE
currDate = Date
dtmDate = Date – 30
strTargetDate = ConvDate(dtmDate)
For Each objFile In objCurrentFolder.Files
FileName = objFile
‘WScript.Echo FileName
strDate = ConvDate(objFile.DateCreated)
‘strDate = ConvDate(objFile.DateLastModified)
If strDate < strTargetDate Then
objFSO.MoveFile FileName, “d:\test\”
outfile.writeline filename
End If
Next
End Sub

Function ConvDate (sDate) ‘Converts MM/DD/YYYY HH:MM:SS to string YYYYMMDD
strModifyDay = day(sDate)
If len(strModifyDay) < 2 Then
strModifyDay = “0” & strModifyDay
End If
strModifyMonth = Month(sDate)
If len(strModifyMonth) < 2 Then
strModifyMonth = “0” & strModifyMonth
End If
strModifyYear = Year(sDate)
ConvDate = strModifyYear & strModifyMonth & strModifyDay
End Function
====== Script Code ======

About these ads

Theme: Rubric. Get a free blog at WordPress.com

Follow

Get every new post delivered to your Inbox.

Join 32 other followers

%d bloggers like this: