Seite 1 von 1

Copy, Move oder Delete mit Fortschrittsanzeige

Verfasst: 15.06.2006 14:39
von ts-soft

Code: Alles auswählen

Procedure CopyWithProgress(Sources.s(), Destination.s, Function.l = #FO_COPY, Flags.l = #FOF_FILESONLY, ParendID.l = 0)
  Protected File.SHFILEOPSTRUCT, *Mem.l, Length.l, Pos.l
  *Mem = AllocateMemory(1)
  With File
    If ParendID = 0
      \hwnd = GetForegroundWindow_()
    Else
      \hwnd = ParendID
    EndIf
    \wFunc = Function
    ForEach Sources()
      Length = Len(Sources()) + 1
      *Mem = ReAllocateMemory(*Mem, Pos + Length)
      PokeS(*Mem + Pos, Sources(), Length)
      Pos + Length
    Next
    \pFrom = *Mem
    \pTo = @Destination
    \fFlags =Flags
  EndWith
  If SHFileOperation_(File) = #S_OK
    FreeMemory(*Mem)
    ProcedureReturn #True
  EndIf
  FreeMemory(*Mem)
  ProcedureReturn #False
EndProcedure
Wer ausser kopieren was anderes wünscht, möge die Flags im PSDK nachschlagen
Beispiel:

Code: Alles auswählen

NewList MySources.s()
AddElement(MySources())
MySources() = #PB_Compiler_Home + "PureBasic.exe"
AddElement(MySources())
MySources() = #PB_Compiler_Home + "SmartUpdate.exe"
AddElement(MySources())
MySources() = #PB_Compiler_Home + "Visual Designer.exe"
AddElement(MySources())
MySources() = #PB_Compiler_Home + "Examples\Sources\Billboard.pb"
AddElement(MySources())
MySources() = #PB_Compiler_Home + "Examples\Sources\Camera.pb"
AddElement(MySources())
MySources() = #PB_Compiler_Home + "Examples\Sources\CDAudio.pb"
AddElement(MySources())
MySources() = #PB_Compiler_Home + "Examples\Sources\Cipher.pb"
; hier bitte anpassen, etwas grösseres, sonst sieht man nicht viel
AddElement(MySources())
MySources() = "G:\Installation\CD-Images\Betriebssysteme\Kanotix\KANOTIX-2005-04.iso"
; usw.

CopyWithProgress(MySources(), GetTemporaryDirectory())
Ein paar etwas grössere Dateien sind schon sinnvoll, damit man den
Fortschritt auch angezeigt bekommt

Verfasst: 28.05.2007 15:06
von Kurzer
Hallo ts-soft,
danke für diesen Code, genau danach habe ich gesucht. :allright:

Was mich jedoch wundert ist, warum das mit

Code: Alles auswählen

\pTo = @Destination
funktioniert.

Lt. API HIlfe müssen sowohl pFrom als auch pTo Doppelnull-terminierte Listen sein.
pFrom ...Pointer to a buffer that specifies one or more source file names. Multiple names must be null-separated. The list of names must be double null-terminated...
pTo ...Pointer to a buffer that contains the name of the destination file or directory. The buffer can contain mutiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES. Multiple names must be null-separated. The list of names must be double null-terminated.



Wenn man nur eine einzelne Datei kopieren möchte, dann kann man sich das Allokieren des Memeories und das Anlegen einer Liste übrigens sparen. Ich habe es mit Strings gelöst, in die direkt die zweite Null gepoked wird.

Code: Alles auswählen

Procedure CopyWithProgress(Source$, Destination$)
  Protected File.SHFILEOPSTRUCT, Length.l
  
  Source$ = Source$ + " " : PokeB(@Source$ + Len(Source$)-1, 0)
  Destination$ = Destination$ + " " : PokeB(@Destination$ + Len(Destination$)-1, 0)
  
  With File
    \hwnd = WindowID(0)
    \wFunc = #FO_COPY
    \pFrom = @Source$
    \pTo = @Destination$
    \fFlags = #FOF_FILESONLY|#FOF_NOCONFIRMATION
  EndWith

  Res = SHFileOperation_(File)
  
  If Res = #S_OK
    ProcedureReturn #True
  ElseIf Res = #DE_OPCANCELLED ; User-Abort, #DE_OPCANCELLED muß mit 117 definiert werden
    ProcedureReturn #DE_OPCANCELLED
  EndIf
  ProcedureReturn #False
EndProcedure
Gruß Markus