Im trying to move all files from "C:\SSIS\Test1\" that are new than 2 days old to the C:\SSIS\Test\" but for some reason it keeps skipping over the if statement marked in red.

Any help please. Very amateur programmer here!

Sub Text()
        Dim sDay As DateTime
        sDay = DateAdd(DateInterval.Day, -2, Date.Now)
        Console.WriteLine(sDay)
        Dim FSO As Object
        'Dim FromPath As String
        Dim ToPath As String
        ToPath = "C:\SSIS\Test\"
        Dim di As New DirectoryInfo("C:\SSIS\Test1\")
        ' Get a reference to each file in that directory.
        Dim fiArr As FileInfo() = di.GetFiles()
        ' Display the names of the files.
        Dim fri As FileInfo
        For Each fri In fiArr
            Console.WriteLine(fri.Name)
            Try
                FSO = CreateObject("scripting.filesystemobject")
                If fri.Name > sDay Then 'skips over this part even when the file was just created
                    FSO.CopyFile(Source:=di, Destination:=ToPath)
                End If
            Catch ex As Exception
            End Try

        Next fri

You're comparing file's name to a date. The comparison should be

If fri.LastAccessTime > sDay Then

You should also move FSO = CreateObject("scripting.filesystemobject") before For Each fri In fiArr because now your loop creates a new object in each iteration. After the loop (line Next fri ) dispose the filesystem object with FSO = Nothing HTH

Hi! Nice to hear that you got answer to your problem. Could you please mark the thread as solved. Thank you!

This article has been dead for over six months. Start a new discussion instead.