Seite 1 von 1

CopyPath() und ComparePath()

Verfasst: 27.02.2008 03:36
von NicTheQuick
Ich hab hier grad mal zwei Nebenprodukte für euch, weil mich Windows
immer nervt, wenn es mal wieder eine Datei von zig tausend nicht
kopieren kann und dann alles abbricht.

CopyPath(in, out) kopiert einen Pfad zu einem zweiten. Jede Aktion
wird debuggt und kann durch eigene Ausgaben abgeändert werden. Die
Procedure ist rekursiv und terminiert nach dem Durchlaufen aller Dateien
im Ausgangspfad.
Vorsicht: Schon existierende Dateien in 'out' werden überschrieben.
Hinweis: Datums- und Zeitangaben werden beim Kopieren nicht
übernommen. Diese Funktion müsste selbst nachgerüstet werden.

ComparePath(in, out) ist eine reine Kontrollfunktion und gibt alle
Dateien und Ordner in 'out' an, die nicht vom Ausgangspfad kopiert
wurden oder deren Dateigrößen voneinander abweichen. Die Procedure ist
ebenfalls rekursiv und terminiert nach dem Durchlaufen aller Dateien im
Ausgangspfad.

Code: Alles auswählen

Procedure CopyPath(Path.s, out.s) ;kopiert einen Pfad zu einem anderen
  Protected DirID.l, Name.s
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  If Right(Path, 1) <> "/" : Path + "/" : EndIf
  If Right(out, 1) <> "/" : out + "/" : EndIf
  CompilerElse
  If Right(Path, 1) <> "\" : Path + "\" : EndIf
  If Right(out, 1) <> "\" : out + "\" : EndIf
  CompilerEndIf
  
  If FileSize(out) <> -2
    If CreateDirectory(out)
      Debug "Ordner erstellt: " + out + Name
    Else
      Debug "!Ordner nicht erstellt: " + out + Name
      ProcedureReturn #False
    EndIf
  Else
    Debug "Ordner besteht bereits: " + out + Name
  EndIf
  
  DirID = ExamineDirectory(#PB_Any, Path, "*.*")
  If DirID
    While NextDirectoryEntry(DirID)
      Name = DirectoryEntryName(DirID)
      If DirectoryEntryType(DirID) = #PB_DirectoryEntry_File
        If CopyFile(Path + Name, out + Name)
          Debug "Datei kopiert: " + Path + Name + " nach " + out + Name
        Else
          Debug "!Datei nicht kopiert: " + Path + Name
        EndIf
      Else
        If (Name <> "." And Name <> "..")
          CopyPath(Path + Name, out + Name)
        EndIf
      EndIf
    Wend
    FinishDirectory(DirID)
  EndIf
  
  ProcedureReturn #True
EndProcedure

Procedure ComparePath(Path.s, out.s) ;vergleicht zwei Pfade und gibt die Unterschiede aus
  Protected DirID.l, Name.s, Size.l
  
  CompilerIf #PB_Compiler_OS = #PB_OS_Linux
  If Right(Path, 1) <> "/" : Path + "/" : EndIf
  If Right(out, 1) <> "/" : out + "/" : EndIf
  CompilerElse
  If Right(Path, 1) <> "\" : Path + "\" : EndIf
  If Right(out, 1) <> "\" : out + "\" : EndIf
  CompilerEndIf
  If FileSize(out) <> -2
    Debug "Ordner existiert nicht: " + out
    ProcedureReturn
  EndIf
  
  DirID = ExamineDirectory(#PB_Any, Path, "*.*")
  If DirID
    While NextDirectoryEntry(DirID)
      Name = DirectoryEntryName(DirID)
      If DirectoryEntryType(DirID) = #PB_DirectoryEntry_File
        Size = FileSize(out + Name)
        If Size = -1
          Debug "Datei existiert nicht: " + out + Name
        Else Size <> DirectoryEntrySize(DirID)
          Debug "Dateigröße weicht ab: " + out + Name
        EndIf
      Else
        If (Name <> "." And Name <> "..")
          ComparePath(Path + Name, out + Name)
        EndIf
      EndIf
    Wend
    FinishDirectory(DirID)
  EndIf
  
  ProcedureReturn #True
EndProcedure

in.s = "C:\"
out.s = "X:\"

CopyPath(in, out)
ComparePath(in, out)
Getestet auf Windows 2000 mit PB V4.02.