HondaKillrsx
HondaKillrsx

Reputation: 349

Getting VBS to move file

I'm trying to run through a folder and check the accessed date, if the accessed date is older than 5 days, I need it to move to the "test2" folder and I'm not getting any error messages when ran. Below is what I have so far, can someone point me in the right direction, all I can find on Stack is powershell scripts that do this.

Set oFileSys = WScript.CreateObject("Scripting.FileSystemObject")


sRoot = "C:\test"                                                               
today = Date                                                                  
nMaxFileAge = 5                                                         

MoveFiles(sRoot)    


        Function MoveFiles(ByVal sFolder) 

            Set oFolder = oFileSys.GetFolder(sFolder)
            Set aFiles = oFolder.Files

            For Each file in aFiles
           FileAccessed = FormatDateTime(file.DateLastAccessed, "2")
                If DateDiff("d", FileAccessed, today) > nMaxFileAge Then
                    oFileSys.MoveFile sFolder, "C:\test2"
                End If
            Next

        End Function 

Upvotes: 0

Views: 294

Answers (1)

PatricK
PatricK

Reputation: 6433

Shouldn't it be oFileSys.MoveFile file, "C:\test2\" (you got sFolder instead of file)?

Or you can just file.move "C:\test2\" in the if block.

Update: You need to put \ at end of folder name

Upvotes: 1

Related Questions