Search code examples
vbscriptmovedatecreatedmodified-date

VBScript that Moves modified files to another folder


Basically, I need a script to move files to another folder that have been accessed and modified.

I'm new to scripting, so this may be a simple problem, but I'm stumped. Here's the error I'm getting:

Script: C:\Users\bmcwilliams\Desktop\pssitest.vbs

Line: 17

Char: 10

Error: File already exists

Code: 800A003A

Source: Microsoft VBScript runtime error

The destination folder is empty, so I'm not sure what's going on.

Below is the code I have. It's modified from the code listed in this post:

How to move files from a directory to another directory based on file size

' use a default source path
dim sourcepath: sourcepath = "C:\users\bmcwilliams\Desktop\TestUncompleted"

' use a default destination path
dim destinationpath: destinationpath = "C:\users\bmcwilliams\Desktop\TestCompleted"

dim fso: set fso = CreateObject("Scripting.FileSystemObject")
dim sourcefolder: set sourcefolder = fso.GetFolder(sourcepath)

' loop through each file in the directory, compare size property against
' the limit and copy as appropriate
dim file, count: count = 0
for each file in sourcefolder.Files
    dim createDate: createDate = file.DateCreated
    dim modifyDate: modifyDate = file.DateLastModified
    if createDate <> modifyDate Then
         file.Move destinationpath
         count = count + 1
    end if
next

WScript.Echo("complete: " & count & " file(s) moved")

Any ideas? Any input is greatly appreciated. Thanks!


Solution

  • If the destination path for moving a file is a folder and not the full path (including the destination filename), it must have a trailing backslash:

    destinationpath = "C:\users\bmcwilliams\Desktop\TestCompleted\"
    

    Otherwise the Move operation would detect that the destination (the folder) already exists and would thus fail.