'***************************************************************************************************
'* This script will check all files listed in the folder indicated by SrcFolder and if they are older than the number of
'* seconds indicated in SecondsOld the files will be moved to the destination indicated in DestFolder.
'***************************************************************************************************
Option Explicit
Dim SrcFolder, DestFolder, RightNow, SecondsOld, FileExt, objFSO, refFolder, refFile, objFileCopy, LastModified, ErrMsg
Dim outstr, objTextFile
'###### Set user defined variables here. ######
SrcFolder = "J:\" ' Source folder to look for files
DestFolder="\\servername\job\" ' Destination to move files to
FileExt="jrf" ' File extension of files to process
SecondsOld=30 ' Number of seconds old file must be before it can be moved
Const ForAppending = 8
On Error Resume Next
If Right(DestFolder, 1) <> "\" Then DestFolder = DestFolder & "\" ' Make sure DestFolder ends with a back slash
RightNow=Date & " " & Time ' Current Date and Time to use to compare against files DateLastModified property
Set objFSO = CreateObject ("Scripting.FileSystemObject")
Set refFolder = objFSO.GetFolder(SrcFolder)
If Err.Number Then
ErrMsg = Now() & " Could not open source folder. VB Error: " & Err.Description & vbCrLf
WriteLog(ErrMsg) ' Write error message to log file and then quit script
End If
'Process files in directory
For Each refFile in refFolder.Files ' Loop through all files in folder
If Err.Number Then
ErrMsg = Now() & " Lost access to files. VB Error: " & Err.Description & vbCrLf
WriteLog(ErrMsg) ' Write error message to log file and then quit script
End If
LastModified = (refFile.DateLastModified) ' DateLastModifed is in format of MM/DD/YYYY HH:MM PM
If ((DateDiff("s",LastModified,RightNow)) > SecondsOld) Then ' If number of seconds since file was last modified exceeds SecondsOld
Set objFileCopy = objFSO.GetFile(refFile.Path) ' Create an object pointing to current file
If LCase(Right(objFileCopy.Path, 3)) = FileExt Then ' If the last three letters of the file name match FileExt
objFileCopy.Move (DestFolder) ' Move the file to the folder indicated by DestFolder
If Err.Number Then
ErrMsg = Now() & " Could not write file to " & DestFolder & ". VB Error: " & Err.Description & vbCrLf
WriteLog(ErrMsg) ' Write error message to log file and then quit script
End If
End If
End If
Next
Set refFolder = Nothing
Set objFSO = Nothing
Function WriteLog(outstr)
Err.Clear ' Clear existing error so that new errors in writing to log file can be detected.
outstr = outstr & vbCrLf & vbCrLf
' If log file does not exist then create it
If Not objFSO.FileExists("ErrorLog.txt") Then
Set objTextFile = objFSO.CreateTextFile("ErrorLog.txt")
objTextFile.Close
End If
'Open the log file.
Set objTextFile = objFSO.OpenTextFile("ErrorLog.txt", ForAppending)
If Err.Number Then
'If error occured then log file could not be written to and script will abort.
Set objFSO = Nothing
Wscript.Quit
End If
objTextFile.Write outstr
objTextFile.Close
Set objTextFile = Nothing
Set objFSO = Nothing
Wscript.Quit
End Function