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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace existing folder when copying to destination

Status
Not open for further replies.

stevemarks59

Technical User
Apr 8, 2010
22
US
How can the script below be modified so when a folder is copied to the
destination and a folder exits there with the same name, it will completely REPLACE the old folder
instead of merging with it and only overwriting files with the same name?


----------------------------------------------------------------------------

On Error Resume Next
Const OverwriteExisting = True
Set objArgs = WScript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objArgs.Count < 1 Then
MsgBox "Thanks but.. Wrong!!" & vbCRLF & _
"" & vbCRLF & _
"Select a file in Explorer, drag and drop it on the script." & vbCRLF & _
"Or place a shortcut from the script into the SendTo folder" & vbCRLF & _
"This way the script will show in the sendto right click menu ." & vbCRLF & _
"" & vbCRLF & _
"To edit the sendto menu go to 'Start - Run - Sendto - OK" & vbCRLF & _
"Place the shortcut from DropFile.vbs in the SendTo folder, now right click a file and 'Send To'DropFile", _
vbInformation + vbOKOnly, Title
WScript.Quit
End If


For I = 0 to objArgs.Count - 1

Arg = objArgs(I)

objFSO.CopyFile Arg, "D:\SAVED\", OverwriteExisting
objFSO.CopyFile Arg, "D:\SAVED\", OverwriteExisting

Next
WScript.Quit
------------------------------------------------------------------
 
delete the old one first?
execute some sort of Robocopy command (perhaps using the Mirror stuff?
 
What about .MoveFile with explicitly naming the file. Kind of like renaming a file in DOS.

objFSO.MoveFile "source\path\file.ext", "dest\path\[red]file.ext[/red]"

-Geates
 
Oops, if destination is an existing file, an error occurs. You may just have to delete it first like mrmovie suggested.

-Geates
 
I should really test things before making claims. If you specify the destination file (not just the directory), it will overwrite the existing. This worked for me

Code:
objFSO.CopyFile Arg.Path, "D:\SAVED\" & Arg.Name

-Geates
 
My bad, the code I use is this:

-----------------------------------------
On Error Resume Next
Const OverwriteExisting = True
Set objArgs = WScript.Arguments
Set objFSO = CreateObject("Scripting.FileSystemObject")

If objArgs.Count < 1 Then
MsgBox "Thanks but.. Wrong!!" & vbCRLF & _
"" & vbCRLF & _
"Select a file in Explorer, drag and drop it on the script." & vbCRLF & _
"Or place a shortcut from the script into the SendTo folder" & vbCRLF & _
"This way the script will show in the sendto right click menu ." & vbCRLF & _
"" & vbCRLF & _
"To edit the sendto menu go to 'Start - Run - Sendto - OK" & vbCRLF & _
"Place the shortcut from DropFile.vbs in the SendTo folder, now right click a file and 'Send To'DropFile", _
vbInformation + vbOKOnly, Title
WScript.Quit
End If


For I = 0 to objArgs.Count - 1

Arg = objArgs(I)

objFSO.CopyFile Arg, "D:\SAVED\", OverwriteExisting
, "D:\SAVED\", OverwriteExisting

Next
WScript.Quit
---------------------------------------------------------

I use both "objFSO.CopyFile Arg" and "objFSO.CopyFolder Arg" in the same script so I can copy either files and folders. My question was how can I completely replace a previous FOLDER with a new FOLDER not merge the files inside the new folder with the files in the old folder. It would be fine if I have to use a separate script when I want to just replace folders. If anyone can tell me how to have the script first delete the old folders in the destination directories, then copy the new folder to those directories I would appreciate it. The folders being replaced will have the same name. On that subject, is there a script I could use to replace any folder being copied. One I could use for any folder. I mean one that would first check the destination directory for folders with rhe same name. If they existed the script would first delete the existing folders, then copy the new folders. If there were no existing folders with the same name the script would then copy the new folders there. I would like this done silently without any error messages or prompts requiring me to reply. Thank you.
 
How about
Code:
if fso.FolderExists(yourtargetfolder) then
  fso.DeleteFolder yourtargetfolder, True
end if
objFSO.CopyFile Arg, "D:\SAVED\"

Keep it simple!
:)

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
Argh!
That's
Code:
if [b]objFSO[/b].FolderExists(yourtargetfolder) then
  [b]objFSO[/b].DeleteFolder yourtargetfolder, True
end if
objFSO.CopyFile Arg, "D:\SAVED\"
[blush]

[navy]"We had to turn off that service to comply with the CDA Bill."[/navy]
- The Bastard Operator From Hell
 
'might be slightly safer
if objFSO.FolderExists(yourtargetfolder) then
intReturn = 20
Err.Clear
On Error Resume Next
objFSO.DeleteFolder yourtargetfolder, True
intReturn = Err.Number
On Error Goto 0
end if
If intReturn = 0 Then
objFSO.CopyFile Arg, "D:\SAVED\"
Else
wscirpt.echo "willg et a run time if we try and copy"
End If
'well something like that
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top