Results 1 to 20 of 20

Thread: How To copy folder/file from A to B

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    21

    How To copy folder/file from A to B

    Can anyone please teach me,

    How can I copy a folder and all the sub folders and files in it, from a location A to a destination B. Replace the old content with this new one.

    In another words, I trying to make a back up module for my data folder and files.

  2. #2
    Addicted Member
    Join Date
    Dec 2008
    Posts
    164

    Re: How To copy folder/file from A to B

    make a new folder copy the folder or any of the sub folders, open your newly created folder then paste it there, if i am assuming that you do not do it in runtime.....

  3. #3
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How To copy folder/file from A to B

    Look for examples of Recursive folder handling on planetsourcecode.com. Then, you can make folders by using the MkDir() command, and copy files by using the FileCopy() method.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    21

    Re: How To copy folder/file from A to B

    Is the following a valid code, also please tell me how can I delete the whole folder and all the child folders and the files in that folder.

    <V.B 6.0>


    Code:
    Public Shared Sub Copy ( _
        sourceFileName As String, _
        destFileName As String _
    )
    
    Private Function FileCopy()
        Dim sourceFileName As String
        Dim destFileName As String
        MkDir("C:\temptest")
        sourceFileName="C:/temp/text.txt"
        destFileName="C:/temptest/text.txt"
        File.Copy(sourceFileName, destFileName)
    End Function

  5. #5
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How To copy folder/file from A to B

    by using the harvested folder and file structure you got from the recursive search, you can copy then delete the files also.

    to delete a file, use the Kill() method

    Is the following a valid code
    On vb.net could be. On vb6 it isnt.

  6. #6
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How To copy folder/file from A to B

    I prefer the API way...

    Code:
    '~~> Needs 1 Form, 2 textboxes and 1 Commandbutton
    Option Explicit
    
    Private Declare Function SHFileOperation _
    Lib "shell32.dll" Alias "SHFileOperationA" _
    (lpFileOp As SHFILEOPSTRUCT) As Long
    
    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_COPY = &H2
    
    Private Sub Command1_Click()
        '~~> Usage ~~> CopyFolder(From,To)
        '~~> Example ~~> CopyFolder("C:\Temp\1","C:\Temp\2")
        If CopyFolder(Text1.Text, Text2.Text) Then
            MsgBox "Copied"
        Else
            MsgBox "Not copied"
        End If
    End Sub
    
    Private Function CopyFolder(ByVal sFrom As String, _
    ByVal sTo As String) As Boolean
        Dim SHFileOp As SHFILEOPSTRUCT
        On Error GoTo CF_Err
        CopyFolder = False
        With SHFileOp
            .wFunc = FO_COPY
            .pFrom = sFrom
            .pTo = sTo
        End With
        SHFileOperation SHFileOp
        CopyFolder = True
        Exit Function
    CF_Err:
        MsgBox "Following error occurd while copying folder " & sFrom & vbCrLf & _
        Err.Description, vbExclamation, "Error message"
    End Function
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  7. #7
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How To copy folder/file from A to B

    @koolsid great, i didnt knew the api.

    Here are the details of the SHFILEOPSTRUCT: (for example you can also use the _MOVE to delete the source after copying)
    http://msdn.microsoft.com/en-us/libr...95(VS.85).aspx

    Some more details and remarks about the api:
    http://msdn.microsoft.com/en-us/libr...64(VS.85).aspx

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    21

    Re: How To copy folder/file from A to B

    Great!

    One last thing, I want to delete exhisting folder and its content before copying, else windows is prompting and asking if it can replace.

    So if I delete before copying this will not happen right.


  9. #9
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Re: How To copy folder/file from A to B

    you can use the scripting file system object (fso.deletefolder) to delete a specified folder and its contents...

    reference from msdn:
    http://msdn.microsoft.com/en-us/libr...xh(VS.85).aspx

  10. #10
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How To copy folder/file from A to B

    Code:
    '~~> Set reference to Microsoft Scripting Runtime Library
    
    Private Sub Command1_Click()
        '~~> Check if folder exists and if does then delete it
        If FolderExists("C:\temp\2") Then Call DeleteFolder("C:\temp\2")
        
        '~~> Rest of the Code to copy the folder + files as mentioned in POST#6
    End Sub
    
    '~~> Check If folder exists
    Private Function FolderExists(ByVal strPath As String) As Boolean
        Dim s As String
        '~~> Strip final slash from path
        If Right(strPath, 1) = "\" Then strPath = Left(strPath, Len(strPath) - 1)
        
        '~~> Check if directory exists
        s = Dir(strPath, vbDirectory)
        If s <> "" Then FolderExists = True
    End Function
    
    '~~> Deletes Folder and files inside it
    Sub DeleteFolder(ByVal strPath As String)
        On Error Resume Next
        Dim oFso As New Scripting.FileSystemObject
    
        If Right(strPath, 1) = "\" Then strPath = _
        Left(strPath, Len(strPath) - 1)
    
        If oFso.FolderExists(strPath) Then
            '~~> Deleting Read only files as well
            oFso.DeleteFolder strPath, True
        End If
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  11. #11

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    21

    Re: How To copy folder/file from A to B

    Thanks but
    " Dim oFso As New Scripting.FileSystemObject
    "

    giving problem

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How To copy folder/file from A to B

    Read the very first line in comments in the code that i posted above

    You need to set reference to Microsoft Scripting Runtime Library

    Click On Menu project=>References and select the Microsoft Scripting Runtime Library
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  13. #13

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Posts
    21

    Re: How To copy folder/file from A to B

    POWER it works.
    But, may Know what is "'~~> Set reference to Microsoft Scripting Runtime Library"
    I am unaware of this.

  14. #14
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How To copy folder/file from A to B

    A reference is an external file(s), such as a DLL. To use the file system object you need to set a reference to scrrun.dll

    If you have msdn installed in your pc, Search on the topic "Creating a Reference to an Object" It will explain it in detail....
    Last edited by Siddharth Rout; Feb 24th, 2009 at 08:33 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  15. #15
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How To copy folder/file from A to B

    What about the FO_DELETE flag on SHFILEOPSTRUCT? Will not delete the files and folders?

  16. #16
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How To copy folder/file from A to B

    Quote Originally Posted by Jim Davis
    What about the FO_DELETE flag on SHFILEOPSTRUCT? Will not delete the files and folders?
    Yes you can use that as well... The reson why I gave the other code is because this will ask you to confirm the operation and I feel that the OP didn't want that....

    Code:
    Private Declare Function SHFileOperation Lib "shell32.dll" _
    Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    
    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_DELETE = &H3
    Private Const FOF_ALLOWUNDO = &H40
    Sub DeleteFolder()
        Dim SHFileOp As SHFILEOPSTRUCT
        With SHFileOp
            '~~> Delete the folder
            .wFunc = FO_DELETE
            '~~> Select the folder
            .pFrom = "C:\temp\2"
            '~~> Move to Recycle Bin
            '~~> If you want to permanently delete it then comment the below
            .fFlags = FOF_ALLOWUNDO
        End With
        '~~> Perform Delete
        SHFileOperation SHFileOp
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  17. #17
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How To copy folder/file from A to B

    I see. So, there is no way to force the operation? For example, Total Commander also able to use the windows operations for file copy/deletions. You can set the "Force All" that there it will mass overwrite or/ delete the files without any further notification window. It also catches all widnows messages, then show its own UI window. I'm not sure how it does.

    Edit:
    FOF_NOCONFIRMATION
    Respond with Yes to All for any dialog box that is displayed.
    This flag might help you to catch the window messages.

    FOF_WANTMAPPINGHANDLE
    If FOF_RENAMEONCOLLISION is specified and any files were renamed, assign a name mapping object that contains their old and new names to the hNameMappings member. This object must be freed using SHFreeNameMappings when it is no longer needed.
    These flags seems to me the best combo:
    FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR
    I have found a post here that there you can find the declare of each 'extra' flags
    http://www.vbforums.com/showpost.php...02&postcount=4
    Last edited by Jim Davis; Feb 24th, 2009 at 11:28 AM.

  18. #18
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How To copy folder/file from A to B

    Hmmm let me check it out

    Edit

    Perfect! It works now

    Code:
    Private Declare Function SHFileOperation Lib "shell32.dll" _
    Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    
    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_DELETE = &H3
    Private Const FOF_ALLOWUNDO = &H40
    Private Const FOF_NOCONFIRMATION = &H10
    Sub DeleteFolder()
        Dim SHFileOp As SHFILEOPSTRUCT
        With SHFileOp
            '~~> Delete the folder
            .wFunc = FO_DELETE
            '~~> Select the folder
            .pFrom = "C:\temp\2"
            '~~> Delete Folder and it's contents without confirmation
            .fFlags = FOF_NOCONFIRMATION
        End With
        '~~> Perform Delete
        SHFileOperation SHFileOp
    End Sub
    Last edited by Siddharth Rout; Feb 24th, 2009 at 11:38 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  19. #19
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: How To copy folder/file from A to B

    The FOF_ALLOWUNDO will move the content to the Recycle Bin, am i right? (in case Recycle bin is available, but if it is just disabled by system settings, it will not move to there..)

  20. #20
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: How To copy folder/file from A to B

    No, In that case it won't... I believe. I could be wrong...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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