|
-
Feb 24th, 2006, 08:56 PM
#1
Thread Starter
Fanatic Member
[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?
Last edited by JBD2; Feb 24th, 2006 at 09:12 PM.
Did you find a post in this thread useful? Please click Rate This Post.
-
Feb 24th, 2006, 10:39 PM
#2
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 25th, 2006, 12:01 PM
#3
Thread Starter
Fanatic Member
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.
Did you find a post in this thread useful? Please click Rate This Post.
-
Feb 25th, 2006, 12:09 PM
#4
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
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Feb 25th, 2006, 12:14 PM
#5
Thread Starter
Fanatic Member
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.
Did you find a post in this thread useful? Please click Rate This Post.
-
Feb 25th, 2006, 12:39 PM
#6
Thread Starter
Fanatic Member
Re: Copying one folder and overwriting the other
Nevermind I figured out what was wrong.
Did you find a post in this thread useful? Please click Rate This Post.
-
Feb 25th, 2006, 12:47 PM
#7
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.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|