Results 1 to 7 of 7

Thread: [RESOLVED] Copying one folder and overwriting the other

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Resolved [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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Copying one folder and overwriting the other

    VB Code:
    1. Option Explicit
    2. 'Add a command button
    3. Private Type SHFILEOPSTRUCT
    4.     hWnd As Long
    5.     wFunc As Long
    6.     pFrom As String
    7.     pTo As String
    8.     fFlags As Integer
    9.     fAborted As Boolean
    10.     hNameMaps As Long
    11.     sProgress As String
    12. End Type
    13.  
    14. Private Const FO_MOVE As Long = &H1
    15. Private Const FO_COPY As Long = &H2
    16. Private Const FO_DELETE = &H3
    17. Private Const FO_RENAME As Long = &H4
    18.  
    19. Private Const FOF_ALLOWUNDO = &H40
    20. Private Const FOF_SIMPLEPROGRESS As Long = &H100
    21.  
    22. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    23.  
    24. Private Sub Command1_Click()
    25.     Dim SHFileOp As SHFILEOPSTRUCT
    26.     With SHFileOp
    27.         .wFunc = FO_COPY
    28.         .pFrom = "D:\app" 'Select a folder and all its subdirectories/files
    29.         .pTo = "D:\TestMove" 'You can also rename the file if you want of leave it the same
    30.         .fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS
    31.     End With
    32.     'perform file operation
    33.     SHFileOperation SHFileOp
    34.     MsgBox "The folder 'D:\app' has been copied to 'D:\TestMove'!", vbInformation + vbOKOnly, App.Title '
    35. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    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.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Copying one folder and overwriting the other

    Here are some more flags that can help you customise it.
    VB Code:
    1. Private Const FOF_NOCONFIRMATION As Long = &H10
    2. Private Const FOF_NOCONFIRMMKDIR As Long = &H200
    3. Private Const FOF_NOERRORUI As Long = &H400
    4. Private Const FOF_RENAMEONCOLLISION As Long = &H8
    5. Private Const FOF_SILENT As Long = &H4
    6. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    Re: Copying one folder and overwriting the other

    VB Code:
    1. Option Explicit
    2. 'Add a command button
    3. Private Type SHFILEOPSTRUCT
    4.     hWnd As Long
    5.     wFunc As Long
    6.     pFrom As String
    7.     pTo As String
    8.     fFlags As Integer
    9.     fAborted As Boolean
    10.     hNameMaps As Long
    11.     sProgress As String
    12. End Type
    13.  
    14. Private Const FO_MOVE As Long = &H1
    15. Private Const FO_COPY As Long = &H2
    16. Private Const FO_DELETE = &H3
    17. Private Const FO_RENAME As Long = &H4
    18.  
    19. Private Const FOF_ALLOWUNDO = &H40
    20. Private Const FOF_SIMPLEPROGRESS As Long = &H100
    21. Private Const FOF_NOCONFIRMATION As Long = &H10
    22. Private Const FOF_NOCONFIRMMKDIR As Long = &H200
    23. Private Const FOF_NOERRORUI As Long = &H400
    24. Private Const FOF_RENAMEONCOLLISION As Long = &H8
    25. Private Const FOF_SILENT As Long = &H4
    26. Private Const FOF_WANTNUKEWARNING As Long = &H4000
    27.  
    28. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    29.  
    30. Private Sub Form_Load()
    31. Dim SHFileOp As SHFILEOPSTRUCT
    32.     With SHFileOp
    33.         .wFunc = FO_COPY
    34.         .pFrom = "C:\pb" 'Select a folder and all its subdirectories/files
    35.         .pTo = "C:\Program Files\EA GAMES" 'You can also rename the file if you want of leave it the same
    36.         .fFlags = FOF_NOCONFIRMATION
    37.     End With
    38.     'perform file operation
    39.     SHFileOperation SHFileOp
    40. 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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2005
    Posts
    546

    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.

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width