/build/static/layout/Breadcrumb_cap_w.png

Script that will delete and replace a file in multiple locations

Hello I'm new to scripting and need a little assistance.  I'm trying to write a vb script that will delete a file (same file name) in 2 locations and replace them with a file on a network share.  Below is a script I believe that will work if it were just 1 file in 1 place.  I'm guessing I would just need to add 2 more commands in to point to the 2nd location to be deleted and added?

Set objFSO = CreateObject("Scripting.FileSystemObject")

 

' File to be deleted.

strFile = "C:\scripts\example2.txt"

 

' Source file.

strSource = "\\MyServer\MyShare\example1.txt"

 

' Target location.

strTarget = "c:\scripts"

 

If (objFSO.FileExists(strFile) = True) Then

    obFSO.DeleteFile strFile, True

End If

 

' Copy file.

objFSO.CopyFile strSource, strTarget, True

 

Also there are some computers that won't have the file at all and I would like for it to ignore those.  I'm not sure but I think if it's not there the script will fail and an error message will show up.  Which I don't want since I'll be pushing this out to all the computers in our environment.

Any help is greatly appreciated.  Thanks for your time!

Tino


1 Comment   [ + ] Show comment
  • When you state that you want the script to "ignore" the computers that do not have the file, do you just want to suppress the possible error message and copy the file anyways or do you want it to skip the computer entirely? - M P 10 years ago

Answers (1)

Posted by: KHaught 10 years ago
Senior Yellow Belt
0

You would need to use the fileexists test to make sure you don't encounter errors on the machines that do not have the file. You are correct that you would just delete one, then the other,wrapped by checking to see if the file exists.

If My.Computer.FileSystem.FileExists("c:\scripts\example2.txt") Then "YOUR DELETE CODE HERE" Else End If
 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ