[RESOLVED] Copying one folder and overwriting the other
How can I make code that will copy a folder with other folders and files in it, and overwrite the other one. For example if I had two folders, both named "hello". And I want to overwrite the folder named hello in "C:\Program Files\Myapp" with the folder called "hello" in the app.path? How can I do this through code?
Re: Copying one folder and overwriting the other
VB Code:
Option Explicit
'Add a command button
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_MOVE As Long = &H1
Private Const FO_COPY As Long = &H2
Private Const FO_DELETE = &H3
Private Const FO_RENAME As Long = &H4
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_SIMPLEPROGRESS As Long = &H100
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Command1_Click()
Dim SHFileOp As SHFILEOPSTRUCT
With SHFileOp
.wFunc = FO_COPY
.pFrom = "D:\app" 'Select a folder and all its subdirectories/files
.pTo = "D:\TestMove" 'You can also rename the file if you want of leave it the same
.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS
End With
'perform file operation
SHFileOperation SHFileOp
MsgBox "The folder 'D:\app' has been copied to 'D:\TestMove'!", vbInformation + vbOKOnly, App.Title '
End Sub
:)
Re: Copying one folder and overwriting the other
How can I hide that alert, it works and all, but it doesn't overwrite the folder without showing a message.
Re: Copying one folder and overwriting the other
Here are some more flags that can help you customise it.
VB Code:
Private Const FOF_NOCONFIRMATION As Long = &H10
Private Const FOF_NOCONFIRMMKDIR As Long = &H200
Private Const FOF_NOERRORUI As Long = &H400
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_SILENT As Long = &H4
Private Const FOF_WANTNUKEWARNING As Long = &H4000
Re: Copying one folder and overwriting the other
VB Code:
Option Explicit
'Add a command button
Private Type SHFILEOPSTRUCT
hWnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAborted As Boolean
hNameMaps As Long
sProgress As String
End Type
Private Const FO_MOVE As Long = &H1
Private Const FO_COPY As Long = &H2
Private Const FO_DELETE = &H3
Private Const FO_RENAME As Long = &H4
Private Const FOF_ALLOWUNDO = &H40
Private Const FOF_SIMPLEPROGRESS As Long = &H100
Private Const FOF_NOCONFIRMATION As Long = &H10
Private Const FOF_NOCONFIRMMKDIR As Long = &H200
Private Const FOF_NOERRORUI As Long = &H400
Private Const FOF_RENAMEONCOLLISION As Long = &H8
Private Const FOF_SILENT As Long = &H4
Private Const FOF_WANTNUKEWARNING As Long = &H4000
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
Private Sub Form_Load()
Dim SHFileOp As SHFILEOPSTRUCT
With SHFileOp
.wFunc = FO_COPY
.pFrom = "C:\pb" 'Select a folder and all its subdirectories/files
.pTo = "C:\Program Files\EA GAMES" 'You can also rename the file if you want of leave it the same
.fFlags = FOF_NOCONFIRMATION
End With
'perform file operation
SHFileOperation SHFileOp
End Sub
I did that and it didn't overwrite the folder, it just put my contents from the C:\pb folder into the other one.
Re: Copying one folder and overwriting the other
Nevermind I figured out what was wrong.
Re: Copying one folder and overwriting the other
Here are the explainations of the flags that are relevant.
FOF_NOCONFIRMATION
Respond with "Yes to All" for any dialog box that is displayed.
FOF_NOCONFIRMMKDIR
Do not confirm the creation of a new directory if the operation requires one to be created.
FOF_RENAMEONCOLLISION
Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
Edit: Too late but glad its solved. :)