Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

need to check for and if need be overwrite existing file

Status
Not open for further replies.

Briandr

MIS
Jul 11, 2003
177
US
Hi,

Code works good, but I need it to overwrite the existing file if it exists. Do I want filesys as opposed to copyfile?

const Hidden = 0
Dim FSO
const WaitOnReturn = true
set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set FSO = CreateObject("Scripting.FileSystemObject")
objNetwork.MapNetworkDrive "B:", "\\cmhwaltps\software"
objShell.CurrentDirectory = "B:\Onbase\"
FSO.CopyFile "B:\Onbase\dmMailServices.dll", "C:\Windows\system32\dmMailServices.dll", true
objShell.CurrentDirectory = "C:\"
objNetwork.RemoveNetworkDrive "B:"

Thanks.


 
Hi,

This is what I came up with:

const Hidden = 0
Dim filesys
const WaitOnReturn = true
set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set filesys = CreateObject("Scripting.FileSystemObject")
If filesys.FileExists("C:\Windows\System32\dmMailServices.dll") Then
filesys.DeleteFile "C:\Windows\System32\dmMailServices.dll"
objNetwork.MapNetworkDrive "B:", "\\cmhwaltps\software"
objShell.CurrentDirectory = "B:\Onbase\"
filesys.CopyFile "B:\Onbase\dmMailServices.dll", "C:\Windows\system32\dmMailServices.dll"
End If
objShell.CurrentDirectory = "C:\"
objNetwork.RemoveNetworkDrive "B:"

What about the other scenario where the file was not present previously? Would I want if a FileNotExists?

Sorry, not too good at piecing together code.

 
If the file exists, delete it then copy the file over.

Code:
const Hidden = 0
Dim filesys
const WaitOnReturn = true
set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set filesys = CreateObject("Scripting.FileSystemObject")

If filesys.FileExists("C:\Windows\System32\dmMailServices.dll") Then
  filesys.DeleteFile "C:\Windows\System32\dmMailServices.dll"
End If

objNetwork.MapNetworkDrive "B:", "\\cmhwaltps\software"
objShell.CurrentDirectory = "B:\Onbase\"
filesys.CopyFile "B:\Onbase\dmMailServices.dll", "C:\Windows\system32\dmMailServices.dll"
objShell.CurrentDirectory = "C:\"
objNetwork.RemoveNetworkDrive "B:"

You may also want to check for the existence of the file "B:\Onbase\dmMailServices.dll" before you perform the copy. If the drive didn't map or someone moved/deleted the file it could cause problems.
 
Hi,

I have had issues in the past when trying to disconnect drives mapped with vb.

objNetwork.RemoveNetworkDrive "B:", true

Will that force the drive mapping to close?
 
Hi,

This command in the vbscript is not deleting the file if it is present:

If filesys.FileExists("C:\Windows\System32\dmMailServices.dll") Then
filesys.DeleteFile "C:\Windows\System32\dmMailServices.dll"
End If

Any ideas why?
 
Hi,

I just adjusted my script to see if the file could be deleted off the root of C:\. It deleted without any problem. Is there issues executing this command on files in c:\windows\system32? If so, is there any other command I could run? I need to make sure the file in c:\windows\system32 is deleted as it may be an older file and it needs to be removed prior to copying the new one down.
 
If VBScript won't delete or overwrite a file, it usually has to do with permissions on the file/folder.
 
I am pretty sure the file does not have any permissions on it that would prevent it from being deleted.
 
If should either delete the file, or give a runtime error. Do you have "On Error Resume Next" in your script? This would suppress runtime errors
 
Can I specify account credentials within the vbscript itself so it can delete the file name? Not sure how to go about doing it and google is proving useless per usual. Thanks.
 
you could try the DOS command 'del'.

Code:
objShell.Run "cmd /c del c:\windows\system32\dmMailServices.dll"

-Geates

"I hope I can chill and see the change - stop the bleed inside and feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top