Results 1 to 16 of 16

Thread: Copying a Folder

Hybrid View

  1. #1
    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.

  2. #2

  3. #3

  4. #4
    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

  5. #5
    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

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