using VB to move oldest file to a new folder

Hi, I have a little script that runs as a scheduled task The purpose of the task is to move the oldest files in a specific directory into a new directory. This works as expected when there are files in the directory. However if there are no files in the directory then I get an error message. Attached below is the script that I have used.

Set objFSo = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder("C:MASCompany")

Set colFiles = objFolder.Files

dtmOldestDate = Now

For Each objFile in colFiles
If objFile.DateCreated < dtmOldestDate Then
dtmOldestDate = objFile.DateCreated
strOldestFile = objFile.Path
End If

objFSO.MoveFile strOldestFile, ("C:MASCompanyFTP")

Any help would be much appreciated.

Check the number of files returned:

Check the number of files returned:

Hi, this works a treat, I have one more requirement. I only want it to move the file it is older than one minute. Is this a simple adjustment?

I added some output and commented the actual "MoveFile" command for easier testing; simply remove or comment the Wscript.Echo lines if you don't want them:

Sorry it has taken so long to close this off. The first example given does exactly what I need it to do.

Share this

Related Posts

There was an error in this gadget