Monday, March 26, 2007

Move Files Script

There's a method called .MoveFile, but you can't pass it the True on the end which means Overwrite if already exists. Why I have no idea. So this logic copies the file and then does a delete.

Note the slash at the end of the copy command. This is needed to let it know to put it into a folder. Otherwise it tries to create it as a file.

Also includes logic for creating the destination folder.


sSource = "c:\test\myfile.txt"
sDestination = "d:\folder1"
set oFSO = CreateObject("Scripting.FileSystemObject")
If Not oFSO.FolderExists(sDestination) Then
Set oFolder = oFSO.CreateFolder(sDestination)
End If
set oFSO = CreateObject("Scripting.FileSystemObject")
If oFSO.FileExists(sSource) Then
oFSO.CopyFile sSource, sDestination & "\", True
oFSO.DeleteFile sSource
End If

No comments: