[Gambas-user] Add new command to: Copy directories recursively

B Bruen bbruen at ...2308...
Tue Aug 26 02:01:57 CEST 2014


On Mon, 25 Aug 2014 22:28:29 +0200
Julio Sanchez <jusabejusabe at ...626...> wrote:

> Hi, could you add a new order within gambas3 to copy directories
> recursively.
> 
(snippped)

Here's my version, slightly different to Tobi's.  I find that it is better to provide an array of all the paths in the source tree and then iterate through it as you have more control over what is and is not copied. For example, in the following code I ignore all the svn control files. 

' Gambas module file

''' This module provides some generalised methods for handling files.

Export

Public Function MoveFile(source As String, dest As String, Optional overwrite As Boolean = False) As Boolean  '' Move a file specified by the source path to the dest dir.  If overwrite is specified and is true
                                                                                                              '' then an existing file of the same name in the dest dir will be overwritten (read deleted and replaced).

  Dim sourcedir As String
  Dim sourcefile As String

  sourcefile = File.Name(source)
  sourcedir = File.Dir(source)
  If sourcedir = dest Then Error.Raise("Source and destination directories are the same")

  'First check if file exists
  If Exist(source) Then
    'Now check if copyto directory exists
    If Not (Exist(dest)) Then
      Mkdir dest
    Endif
    'Now check if file exists
    If Exist(dest &/ sourcefile) Then 
      If overwrite Then 
        Kill dest &/ sourcefile
      Else
        Error.Raise("File already exists in destination dir")
      Endif
    Endif
    Move source To dest &/ sourcefile
  Else
    Error.Raise("Source file for move does not exist")
  Endif
  Return True

Catch
   Print Subst$("Err (&2) occurred at &4 : &1.", Error.Text, Error.Code, Error.Backtrace.Join("\n"), Error.Where)
  Return False

End

Public Function CopyFile(source As String, dest As String, Optional overwrite As Boolean = False) As Boolean  '' Copies a file specified by the source path to the dest dir
  
  Dim sourcedir As String
  Dim sourcefile As String

  sourcefile = File.Name(source)
  sourcedir = File.Dir(source)
  If sourcedir = dest Then Error.Raise("Source and destination directories are the same")

  'First check if file exists
  If Exist(source) Then
    'Now check if copyto directory exists
    If Not (Exist(dest)) Then
      Mkdir dest
    Endif
    'Now check if file exists
    If Exist(dest &/ sourcefile) Then 
      If overwrite Then 
        Kill dest &/ sourcefile
      Else
        Error.Raise("File already exists in destination dir")
      Endif
    Endif
    Copy source To dest &/ sourcefile
  Else
    Error.Raise("Source file for copy does not exist")
  Endif
  Return True

Catch
  Print Subst$("Err (&2) occurred at &4 : &1.", Error.Text, Error.Code, Error.Backtrace.Join("\n"), Error.Where)
  Return False

  
End

Public Function RecDir(root As String, rootext As String, pattern As String, filter As Integer) As String[]
  Dim wkrtn As New String[]
  Dim sdirs As New String[]
  Dim sd As String
  Dim item As String
  
  wkrtn = Dir(root &/ rootext, pattern, filter)
  sdirs = Dir(root &/ rootext, "*", gb.Directory)
  For Each sd In sdirs
    If sd = ".svn" Then Continue                                ' ignore subversion dirs
    For Each item In (RecDir(root &/ rootext, sd, pattern, filter))
      wkrtn.Add(sd &/ item)
    Next
  Next
  Return wkrtn.Sort()

Catch
  Error Subst("&1\nERR: &2 (&3)\n&4\n&1\n", String$(40, "-"), Error.Text, Error.Code, Error.Backtrace.Join("\n"))
  Error sd, root, rootext, pattern, filter
  
End

hth

-- 
B Bruen <bbruen at ...2308...>




More information about the User mailing list