VBscript FileSystemObject copy file gives permission denied error

This is something I forget about since I don't do a lot of file copying using VBS but I figured it is good information to have out there.

Any time you are copying to a folder using VBScript your folder path needs to have the trailing \ on it or your script will try to treat your folder like a file and try to overwrite it and give a permission denied.

So while this example will not work:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\test.txt")
objFile.Copy "c:\temp", True

This one will:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\test.txt")
objFile.Copy "c:\temp\", True

2 comments: