Link to home
Start Free TrialLog in
Avatar of schmir1
schmir1Flag for United States of America

asked on

VBScript CopyFile not working when it exists

The CopyFile throws an error if it finds the file and when I added Deletefile then that throws an error if it doesn't find the file.  Sometimes I get a permission error.  Is it just because it's the desktop?

I need some code that will overwrite the file and not complain.
oFSO.DeleteFile "C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Devtest.lnk",false
ofso.CopyFile "T:\MCL_Dbase\Development_db\DTLDB_SQLVer\Setup_Dev\DTLDB.accdb Dev.lnk","C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Devtest.lnk"

Open in new window

Avatar of Lazarus
Lazarus
Flag of United States of America image

Try this:

Set WshNetwork = WScript.CreateObject("WScript.Network")
strUser = WshNetwork.UserName
On Error Resume Next
'Change to 1 if you want it to overwrite
fso.CopyFile "\\SERVER\netlogon\faculty.jpg", "c:\temp\samplefile.jpg", -0

Set fs = Nothing
end
ASKER CERTIFIED SOLUTION
Avatar of JustWorking
JustWorking

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of schmir1

ASKER

I needed to add the On Error Resume Next before the DeleteFile.  I am the administrator on this PC but it will not overwrite a file in the All Users\Desktop space no matter what True/False options I put on the DeleteFile and CopyFile commands.

I only need On Error Resume Next before the DeleteFile command so how do I turn back on the debug error handler after that?


On Error Resume Next
    oFSO.DeleteFile "C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Test.lnk",true
	ofso.CopyFile "T:\MCL_Dbase\Development_db\DTLDB_SQLVer\Setup_Dev\DTLDB.accdb Test.lnk","C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Test.lnk", true

Open in new window

Avatar of JustWorking
JustWorking

To see the errors you would have to remark out the On Error Resume Next with a '
So the line would be:
'On Error Resume Next
Avatar of schmir1

ASKER

Don't want to comment it out.  I want to see all the real errors.

The following seems to work using the
On Errror GoTo 0

Thanks guys for your help.
On Error Resume Next  'Need to turn of error handling in case the lnk file does not exist
oFSO.DeleteFile "C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Test.lnk",true
On Error GoTo 0
ofso.CopyFile "T:\MCL_Dbase\Development_db\DTLDB_SQLVer\Setup_Dev\DTLDB.accdb Test.lnk","C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Test.lnk", true

Open in new window