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!


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?
Please log in to comment



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
Answered 12/31/2013 by: KHaught
Senior Yellow Belt

Please log in to comment
Answer this question or Comment on this question for clarity