Results 1 to 16 of 16

Thread: Copying a Folder

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    2

    Copying a Folder

    I know how to copy a file, but I simply can't figure out how to copy an entire folder. Any help is greatly appreciated.

  2. #2
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    This is quite versatile:

    In Module:

    VB Code:
    1. Option Explicit
    2.  
    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 Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
    15.  
    16. Const FOF_NOCONFIRMATION = &H10             '  Don't prompt the user.
    17. Const FOF_NOCONFIRMMKDIR = &H200            '  don't confirm making any needed dirs
    18. Const FO_COPY = &H2
    19.  
    20. Public Function NuCopy(source As String, target As String) As Boolean
    21. 'Nucleus
    22. 'Copy file(s)/directories from source to destination
    23. 'In path of source either file(s) or folder and path of target as folder
    24. 'Out: Boolean indicating success
    25. If Right(source, 1) = "\" Then source = Left(source, Len(source) - 1)
    26. If Len(Dir$(target, vbDirectory)) <> 0 And (Len(Dir$(source, vbDirectory)) <> 0 Or Len(Dir$(target, vbDirectory)) <> 0) Then
    27.     Dim SHFileOp As SHFILEOPSTRUCT  ' structure to pass to the function
    28.     With SHFileOp
    29.         .wFunc = FO_COPY
    30.         .pFrom = source
    31.         .pTo = target
    32.         .fFlags = FOF_NOCONFIRMATION Or FOF_NOCONFIRMMKDIR
    33.     End With
    34.     NuCopy = (SHFileOperation(SHFileOp) = 0)
    35. End If
    36. End Function

    Usage:
    NuCopy("c:\tmp\", "c:\test") ' copy whole folder including subdirectories
    NuCopy("c:\tmp\*.*", "c:\test") 'all files in a folder to a different folder
    NuCopy("c:\tmp\*.txt", "c:\test") ' copy all text files in a folder
    NuCopy("c:\tmp\american.txt", "c:\test") ' copy a file (note can use name to move a single file or filecopy to copy a single file)

  3. #3
    Addicted Member
    Join Date
    Feb 2005
    Posts
    168

    Re: Copying a Folder

    Hi,
    NuCopy("c:\tmp\*.*", "c:\test") 'all files in a folder to a different folder
    This only copies files which are in root folder. What about files in Sub Folders?
    Regards,

    Piyush


    Nothing is Impossible because the word impossible itself says I'mpossible

  4. #4
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Copying a Folder

    My goodness mate where did you dredge this up from?

    You could try
    VB Code:
    1. Name "c:\tmp" As "c:\test"

    Failing that I think you'd have to recursively iterate through all subfolders.

  5. #5
    Addicted Member
    Join Date
    Feb 2005
    Posts
    168

    Re: Copying a Folder

    Sorry Sorry I made a mistake while copying the code.
    My question was
    NuCopy("c:\tmp\*.txt", "c:\test") ' copy all text files in a folder

    This copies *.txt files for root folder only and not sub folder. Please tell me how do I do that?
    Regards,

    Piyush


    Nothing is Impossible because the word impossible itself says I'mpossible

  6. #6
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Copying a Folder

    You'd need to use iterative recursion.

    i.e. loop through all subfolders, and their subfolders, etc., calling that function for each one.

  7. #7

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Copying a Folder

    You can use Name x As y to move a file from one folder to another.

  9. #9

  10. #10

  11. #11
    Addicted Member
    Join Date
    Feb 2005
    Posts
    168

    Smile Re: Copying a Folder

    Guys here is what we want using FSO. The function Returns a string that contains list of paths of files in root as well as sub folders.

    VB Code:
    1. Option Explicit
    2.  
    3. Public Function get_all_directory_files_with_wildcard _
    4.  (ByVal tfolder As String, _
    5.  ByVal getsubdirs As Boolean, _
    6.  ByVal wildcard As String) _
    7.  As String
    8.  
    9.    
    10.     Dim objfile As File
    11.     Dim objfolder As Folder
    12.     Dim fso As New FileSystemObject
    13.    Dim kokovar As Variant
    14.    Dim k As Long
    15.    Dim wildext As String
    16.    Dim wildexts As String
    17.    Dim wildfirst As String
    18.    Dim wildexte As String
    19.    Dim wildfirsts As String
    20.       Dim wildfirste As String
    21. Dim examfirst As String
    22. Dim examext As String
    23. Dim afl_filetext As String
    24.  
    25. kokovar = Split(wildcard, ",")
    26.  
    27.     If tfolder <> "" Then
    28.  
    29.  
    30.         For Each objfile In fso.GetFolder(tfolder).Files
    31.             'do the stuff we want with the files
    32.             For k = 0 To UBound(kokovar)
    33.          wildext = LCase(cutgetExtension(kokovar(k)))
    34.          wildfirst = LCase(Mid(kokovar(k), 1, Len(kokovar(k)) - Len(wildext) - 1))
    35.            
    36.        If InStr(1, wildext, "*") = 0 Then
    37.        wildexts = "888NONE888"
    38.        wildexte = "888NONE888"
    39.        Else
    40.        wildexts = Mid(wildext, 1, InStr(1, wildext, "*") - 1)
    41.        wildexte = Mid(wildext, InStr(1, wildext, "*") + 1, Len(wildext) - InStr(1, wildext, "*"))
    42.        End If
    43.        
    44.         If InStr(1, wildfirst, "*") = 0 Then
    45.        wildfirsts = "888NONE888"
    46.        wildfirste = "888NONE888"
    47.        Else
    48.        wildfirsts = Mid(wildfirst, 1, InStr(1, wildfirst, "*") - 1)
    49.        wildfirste = Mid(wildfirst, InStr(1, wildfirst, "*") + 1, Len(wildfirst) - InStr(1, wildfirst, "*"))
    50.        End If
    51.            
    52.         examfirst = LCase(cutgetName(cutfilename(CStr(objfile))))
    53.         examext = LCase(cutgetExtension(CStr(objfile)))
    54.            
    55.         If wildexts = "888NONE888" Then
    56. 'we do not have a wildcard in the extension
    57.         If wildfirsts = "888NONE888" Then
    58.         'we do not have a wildcard neither on the beggining or the
    59.  
    60. 'extension
    61.         If examfirst = wildfirst And examext = wildext Then
    62.         afl_filetext = afl_filetext + objfile + vbNewLine
    63.         End If
    64.                
    65.         Else
    66.        
    67.         'we do have a wildcard in the beggining but not in
    68. 'the extension
    69.         If Mid(examfirst, 1, Len(wildfirsts)) = wildfirsts And _
    70.         Mid(examfirst, Len(wildfirst) - Len(wildfirste) + 1, Len(wildfirste)) = wildfirste And wildext = examext Then
    71.         afl_filetext = afl_filetext + objfile + vbNewLine
    72.         End If
    73.        
    74.         End If
    75.        
    76.         Else
    77.         'we do not have a wildcard in the extension
    78.         If wildfirsts = "888NONE888" Then
    79.         'we do have a wildcard in the beggining but not in the
    80. 'extension
    81.         If Mid(examext, 1, Len(wildexts)) = wildexts And _
    82.         Mid(examext, Len(wildext) - Len(wildexte) + 1, Len(wildexte)) = wildexte Then
    83.         afl_filetext = afl_filetext + objfile + vbNewLine
    84.         End If
    85.            
    86.         Else
    87.         'we have a wildcard in both beggining and extension
    88.        
    89.             If Mid(examext, 1, Len(wildexts)) = wildexts And _
    90.         Mid(examext, Len(wildext) - Len(wildexte) + 1, Len(wildexte)) = wildexte _
    91.          And Mid(examfirst, 1, Len(wildfirsts)) = wildfirsts And _
    92.         Mid(examfirst, Len(wildfirst) - Len(wildfirste) + 1, Len(wildfirste)) = wildfirste Then
    93.         afl_filetext = afl_filetext + objfile + vbNewLine
    94.         End If
    95.            
    96.             End If
    97.            
    98.             End If
    99.            
    100.             'telos if
    101.                    
    102.             Next k
    103.            
    104.         Next
    105.  
    106. If getsubdirs Then
    107.  
    108.         For Each objfolder In fso.GetFolder(tfolder).SubFolders
    109.            afl_filetext = afl_filetext & get_all_directory_files_with_wildcard(CStr(objfolder), getsubdirs, wildcard)
    110.         Next
    111.        
    112.     End If
    113. End If
    114.  
    115. Set fso = Nothing
    116. get_all_directory_files_with_wildcard = afl_filetext
    117. End Function
    118.  
    119. Public Function cutfilename(ByVal fname As String) As String
    120. Dim spos As Integer
    121. Dim ffn As String
    122. spos = InStrRev(fname, "\")
    123. ffn = Mid(fname, spos + 1, Len(fname) - spos)
    124. cutfilename = ffn
    125.  
    126. End Function
    127.  
    128. Public Function cutgetExtension(ByVal fname As String)
    129. Dim spos As Integer
    130. Dim koko As String
    131.  
    132. spos = InStrRev(fname, ".")
    133. If spos <> 0 Then
    134. koko = Mid(fname, spos + 1, Len(fname) - spos)
    135. End If
    136.  
    137. cutgetExtension = koko
    138.  
    139. End Function
    140.  
    141.  
    142. Public Function cutgetName(ByVal fname As String)
    143. Dim spos As Integer
    144. Dim koko As String
    145.  
    146. spos = InStrRev(fname, ".")
    147. If spos <> 0 Then
    148. koko = Mid(fname, 1, spos - 1)
    149. End If
    150. cutgetName = koko
    151.  
    152. End Function

    Usage :
    VB Code:
    1. get_all_directory_files_with_wildcard("C:\Documents and Settings\Piyush\Desktop\Resumes", True, "*.htm")
    Regards,

    Piyush


    Nothing is Impossible because the word impossible itself says I'mpossible

  12. #12
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Copying a Folder

    I don't know where are you comming from (idea wise) pbuddy, but to copy folder with all subfolders using FSO all you need is basically ONE line of code (besides declarations):
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim fso As FileSystemObject
    3.  
    4.     Set fso = New FileSystemObject
    5.     [b]fso.CopyFolder "c:\temp\temp1", "c:\temp\temp3", True[/b]
    6.  
    7. End Sub

  13. #13
    Addicted Member
    Join Date
    Feb 2005
    Posts
    168

    Re: Copying a Folder

    Man I wanted this to copy a specific type of files in a folder with sub folder. I know FSO functions.
    Regards,

    Piyush


    Nothing is Impossible because the word impossible itself says I'mpossible

  14. #14
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Copying a Folder

    Man, that is NOT the scope of this thread which clearly states Copying a Folder and nothing more than that. Also, if you read original post (#1) then you would find this:
    ... but I simply can't figure out how to copy an entire folder. ...

  15. #15
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    Re: Copying a Folder

    pbuddy_8,

    Start a new thread. And in future don't try to revive 3 year old threads with different topics.

  16. #16

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