VBScript that Moves modified files to another folder

bmac423 picture bmac423 · Oct 11, 2012 · Viewed 7k times · Source

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!

Answer

Sorceri picture Sorceri · Oct 11, 2012

You are copying to the new location but do not supply the new name of the file. To fix the issue append a \ and the file name to the destination path.

file.Move destinationpath +"\" + file.name