Google

Thursday, January 3, 2008

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

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 ======
Google Groups
Subscribe to IT_Discussions
Email:
Visit this group