Copy, delete and compare folders recursively

Up to this day, 4D cannot move or delete folders recursively including content.

So I wrote a few methods to do it, and while I was at it, I also wrote a method that compares folders.

These methods are part of my AC_FileTools component to be published in a while. Till then feel free to serve yourself:

Deleting folders:

  // ----------------------------------------------------
  // Method: ACFT_DeleteFolder
  // ----------------------------------------------------
  // Call:   HasLockedContent:= ACFT_DeleteFolder(FolderPath{;BreakOnError})
  // ----------------------------------------------------
  // UserName (OS): Alexander Heintz
  // Date and Time: 28.10.14, 12:09:04
  // ----------------------------------------------------
  // Does:
  //      deletes  folder and all of its content.
  //      if locked content is encoundtered, it either breaks off
  //      or continues deleting he other stuff (Default)
  // ----------------------------------------------------
  // Parameters:
  // ->  $1     text        the folder path to delete
  // ->  $2     boolean     true if stop on error desired
  // ----------------------------------------------------
  // Parameters:
  // ->  $1     text        the folder path to delete
  // ->  $2     boolean     true if stop on error desired
  // ----------------------------------------------------
  // Parameter Definition
C_TEXT($1)
C_BOOLEAN($2)
C_BOOLEAN($0)
  // ----------------------------------------------------
  // Local Variable Definition
C_BOOLEAN($b_Break)
C_BOOLEAN($b_FolderLocked)
C_BOOLEAN($b_Locked)
C_BOOLEAN($b_StopOnLocked)
C_LONGINT($l)
C_TEXT($t_File)
C_TEXT($t_Folder)
C_TEXT($t_Path)
  // ----------------------------------------------------
  // Parameter Assignment
$t_Path:=$1
$b_StopOnLocked:=$2
  // ----------------------------------------------------

If (Test path name($t_Path)=Is a folder)
  ARRAY TEXT($at_Folders;0)
  ARRAY TEXT($at_Files;0)
    //get the documents and folders in the path
  DOCUMENT LIST($t_Path;$at_Files)
  FOLDER LIST($t_Path;$at_Folders)
    //first delete all documents
  For ($l;1;Size of array($at_Files))
    $t_File:=$t_Path+$at_Files{$l}
    DELETE DOCUMENT($t_File)
    Case of 
    : ((OK=0) & ($b_StopOnLocked))
      $b_Break:=True
      $b_Locked:=True
      $l:=Size of array($at_Files)
    : (OK=0)
      $b_Locked:=True
    End case 
  End for 
    //now delete the folders recursively
  If ($b_Break=False)
    For ($l;1;Size of array($at_Folders))
      $t_Folder:=$t_Path+$at_Folders{$l}+Folder separator
      If (ACFT_DeleteFolder ($t_Folder;$b_StopOnLocked))
        $b_FolderLocked:=True
        If ($b_StopOnLocked)
          $b_Break:=True
          $l:=Size of array($at_Folders)
        End if 
      End if 
    End for 
  End if 
  DELETE FOLDER($t_Path)
End if 

$0:=($b_FolderLocked | $b_Locked)

Copy a folder with all content:

  // ----------------------------------------------------
  // Method: ACFT_CopyFolder
  // ----------------------------------------------------
  // Call:   ACFT_CopyFolder(Sourcepath;TargetPath{;DeleteExisting})
  // ----------------------------------------------------
  // UserName (OS): Alexander Heintz
  // Date and Time: 28.10.14, 12:52:39
  // ----------------------------------------------------
  // Does:
  //      copies a folder to a new destination overweriting anything there already is
  //      if you want to first delete an already existing copy of the folder pass
  //      true in the third parameter
  // ----------------------------------------------------
  // Parameters:
  // ->  $1     type        description
  // ----------------------------------------------------
  // Parameter Definition
C_TEXT($1)
C_TEXT($2)
C_BOOLEAN($3)
  // ----------------------------------------------------
  // Local Variable Definition
C_BOOLEAN($b_Break)
C_LONGINT($l)
C_TEXT($t_FolderName)
C_TEXT($t_NewDocPath)
C_TEXT($t_NewPath)
C_TEXT($t_OldDocPath)
C_TEXT($t_OldFOlder)
C_TEXT($t_OldPath)
C_TEXT($t_Path)
  // ----------------------------------------------------
  // Parameter Assignment
$t_OldPath:=$1
$t_NewPath:=$2
If (Count parameters>2)
  $b_DeleteFirst:=$3
End if 
  // ----------------------------------------------------

If (Test path name($t_OldPath)=Is a folder)
  If (Test path name($t_NewPath)#Is a folder)
    CREATE FOLDER($t_NewPath;*)
  End if 
  $t_FolderName:=$t_OldPath
  If ($t_FolderName=("@"+Folder separator))
    $t_FolderName:=Substring($t_FolderName;1;Length($t_FolderName)-1)
  End if 
  While (Position(Folder separator;$t_FolderName)>0)
    $t_FolderName:=Substring($t_FolderName;Position(Folder separator;$t_FolderName)+1)
  End while 
  $t_NewPath:=$t_NewPath+$t_FolderName+Folder separator
  If (Test path name($t_NewPath)#Is a folder)
    CREATE FOLDER($t_NewPath)
  Else 
    If ($b_DeleteFirst)
      If (ACFT_DeleteFolder ($t_NewPath))
        $b_Break:=True
      Else 
        CREATE FOLDER($t_NewPath)
      End if 
    End if 
  End if 

  If ($b_Break=False)
    ARRAY TEXT($at_Folders;0)
    ARRAY TEXT($at_Files;0)
  //get the documents and folders in the path
    DOCUMENT LIST($t_OldPath;$at_Files)
    FOLDER LIST($t_OldPath;$at_Folders)
  //first copy all documents
    For ($l;1;Size of array($at_Files))
      $t_OldDocPath:=$t_OldPath+$at_Files{$l}
      $t_NewDocPath:=$t_NewPath+$at_Files{$l}
      COPY DOCUMENT($t_OldDocPath;$t_NewDocPath;*)
    End for 
  //now copy folders recursively
    For ($l;1;Size of array($at_Folders))
      $t_OldFOlder:=$t_OldPath+$at_Folders{$l}+Folder separator
      ACFT_CopyFolder ($t_OldFOlder;$t_NewPath)
    End for 
  End if 
Else 
  ALERT("Some existing items could not be deleted")
End if 

And a last, compare folders:

  // ----------------------------------------------------
  // Method: ACFT_CompareFolder
  // ----------------------------------------------------
  // Call:   FoldersDiffer:=ACFT_CompareFolder(Folder1;Folder2)
  // ----------------------------------------------------
  // UserName (OS): Alexander Heintz
  // Date and Time: 28.10.14, 14:28:37
  // ----------------------------------------------------
  // Does:
  //      
  //      
  // ----------------------------------------------------
  // Parameters:
  // ->  $1     text        first folder path
  // ->  $2     text        second folder path
  // ->  $0     boolean     true if folders are different
  // ----------------------------------------------------
  // Parameter Definition
C_TEXT($1)
C_TEXT($2)
C_BOOLEAN($0)
  // ----------------------------------------------------
  // Local Variable Definition
C_BOOLEAN($b_Different)
C_BOOLEAN($b_Invisible)
C_BOOLEAN($b_Lock)
C_DATE($d_Created1)
C_DATE($d_Created2)
C_DATE($d_Modified1)
C_DATE($d_Modified2)
C_TIME($h_Created1)
C_TIME($h_Created2)
C_TIME($h_Modified1)
C_TIME($h_Modified2)
C_LONGINT($l)
C_TEXT($t_DocPath1)
C_TEXT($t_DocPath2)
C_TEXT($t_Folder1)
C_TEXT($t_Folder2)
C_TEXT($t_FolderPath1)
C_TEXT($t_FolderPath2)
  // ----------------------------------------------------
  // Parameter Assignment
$t_Folder1:=$1
$t_Folder2:=$2
  // ----------------------------------------------------

Case of 
: (Test path name($t_Folder1)#Is a folder)
  $b_Different:=True
: (Test path name($t_Folder2)#Is a folder)
  $b_Different:=True
Else 
  //check the files first
  ARRAY TEXT($at_Files1;0)
  ARRAY TEXT($at_Files2;0)
  DOCUMENT LIST($t_Folder1;$at_Files1)
  DOCUMENT LIST($t_Folder2;$at_Files2)
  SORT ARRAY($at_Files1)
  SORT ARRAY($at_Files2)
  If (Size of array($at_Files1)=Size of array($at_Files2))
    For ($l;1;Size of array($at_Files1))
      If (Find in array($at_Files2;$at_Files1{$l})<0)
        $l:=Size of array($at_Files1)
        $b_Different:=True
      End if 
    End for 
    If (Not($b_Different))
      For ($l;1;Size of array($at_Files1))
        If ($at_Files1{$l}#".@")
          $t_DocPath1:=$t_Folder1+$at_Files1{$l}
          $t_DocPath2:=$t_Folder2+$at_Files1{$l}
          If (Get document size($t_DocPath1)#Get document size($t_DocPath2))
            $l:=Size of array($at_Files1)
            $b_Different:=True
          Else 
            GET DOCUMENT PROPERTIES($t_DocPath1;$b_Lock;$b_Invisible;$d_Created1;$h_Created1;$d_Modified1;$h_Modified1)
            GET DOCUMENT PROPERTIES($t_DocPath2;$b_Lock;$b_Invisible;$d_Created2;$h_Created2;$d_Modified2;$h_Modified2)
            Case of 
            : ($d_Modified1#$d_Modified2)
              $b_Different:=True
              $l:=Size of array($at_Files1)
            : ($h_Modified1#$h_Modified2)
              $b_Different:=True
              $l:=Size of array($at_Files1)
            End case 
          End if 
        End if 
      End for 
    End if 
  Else 
    $b_Different:=True
  End if 
  If (Not($b_Different))
    ARRAY TEXT($at_Folders1;0)
    ARRAY TEXT($at_Folders2;0)
    FOLDER LIST($t_Folder1;$at_Folders1)
    FOLDER LIST($t_Folder2;$at_Folders2)
    SORT ARRAY($at_Folders1)
    SORT ARRAY($at_Folders2)
    If (Size of array($at_Folders1)#Size of array($at_Folders2))
      $b_Different:=True
    Else 
      For ($l;1;Size of array($at_Folders1))
        If (Find in array($at_Folders2;$at_Folders1{$l})<0)
          $l:=Size of array($at_Folders1)
          $b_Different:=True
        End if 
      End for 
      If (Not($b_Different))
        For ($l;1;Size of array($at_Folders1))
          $t_FolderPath1:=$t_Folder1+$at_Folders1{$l}+Folder separator
          $t_FolderPath2:=$t_Folder2+$at_Folders1{$l}+Folder separator
          If (ACFT_CompareFolder ($t_FolderPath1;$t_FolderPath2))
            $b_Different:=True
            $l:=Size of array($at_Folders1)
          End if 
        End for 
      End if 
    End if 
  End if 
End case 

$0:=$b_Different

2 thoughts on “Copy, delete and compare folders recursively

    1. admin Post author

      Danke, da ist mir im Editor von WordPress was quer gegangen…
      Der mag keinen Code…
      Ist korrigiert, sollte jetzt gehen.

      Reply

Leave a Reply to Axel Kohlepp Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.