Jump to content

Problem With Copy Files Older than 3 Days


Recommended Posts

Hello people,

I'm having a problem with a script I just wrote. I hope someone can fix my problem.

I'm trying to copy files to a new location if they are older than 3 days.

On Error Resume Next 
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld

iDaysOld = 3
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\testfolder"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files

For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFSO.CopyFile "C:\testfolder\*.*", "D:\test2", OverwriteExisting
End If
Next

Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

Many thanks

Edited by Volser
Link to comment
Share on other sites


Here's yours fixed

On Error Resume Next
Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld

iDaysOld = 3
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\testfolder\"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files

For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFSO.CopyFile oFile, "D:\test2\", OverwriteExisting
End If
Next

Set oFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

Or you could try this!

Dim fso, f, f1

SrcDir = "C:\testfolder\"
DesDir = "D:\test2\"
MorOld = 3

Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(SrcDir)

For Each f1 in f.Files
If DateDiff("d", f1.DateLastModified, Now) > MorOld Then
fso.CopyFile f1, DesDir, True
End If
Next

Set fso = Nothing
Set f = Nothing

Link to comment
Share on other sites

Thanks for posting an new one. I just wanted to post my own fix. I just made it and finnaly it works like it shut.

'On error resume next

Dim oFSO
Dim sDirectoryPath
Dim oFolder
Dim oFileCollection
Dim oFile
Dim iDaysOld

iDaysOld = 3
Set oFSO = CreateObject("Scripting.FileSystemObject")
sDirectoryPath = "C:\testfolder"
strTarget = "D:\test2\"
set oFolder = oFSO.GetFolder(sDirectoryPath)
set oFileCollection = oFolder.Files
Const OverwriteExisting = TRUE

For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
Set iFSO = CreateObject("Scripting.FileSystemObject")
iFSO.CopyFile oFile, strTarget, OverwriteExisting
End If
Next

For each oFile in oFileCollection
If oFile.DateLastModified < (Date() - iDaysOld) Then
oFile.Delete(True)
End If
Next

Set oFSO = Nothing
Set iFSO = Nothing
Set oFolder = Nothing
Set oFileCollection = Nothing
Set oFile = Nothing

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...