• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1889
  • Last Modified:

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

0
schmir1
Asked:
schmir1
2 Solutions
 
LazarusCommented:
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
0
 
JustWorkingCommented:
Just add:
On Error Resume Next
at the start of your script. As long as the file exists on your T: drive it will copy it whether the original file exists or not.

On Error Resume Next
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

0
 
RobSampsonCommented:
Hi, the CopyFile method has a third parameter, whether to overwrite or not, so you should only need

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", True

to have it overwrite.

If that doesn't work, use

oFSO.DeleteFile "C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Devtest.lnk", True
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", True

So that is also tries to force the file to be deleted as well.

You may get permission denied for this script because you're trying to write to the All Users profile, and only Administrators have the privileges to write there.

You could run it as a StartUp script, but the StartUp script will run under the System account, and so won't have access to the T drive.  You'll need to place the lnk file on the NetLogon share of a domain controller, and use

ofso.CopyFile "\\domain.com\sysvol\domain.com\scripts\DTLDB.accdb Dev.lnk","C:\Documents and Settings\All Users\Desktop\DTLDB.accdb Devtest.lnk", True

as the System account only has access to that network location.

Regards,

Rob.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
schmir1Author Commented:
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

0
 
JustWorkingCommented:
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
0
 
schmir1Author Commented:
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

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now