Jump to content

Yet another copy with progress


 Share

Recommended Posts

Global Const $FO_MOVE                  = 0x0001
Global Const $FO_COPY                  = 0x0002
Global Const $FO_DELETE              = 0x0003
Global Const $FO_RENAME              = 0x0004

Global Const $FOF_MULTIDESTFILES        = 0x0001
Global Const $FOF_CONFIRMMOUSE        = 0x0002
Global Const $FOF_SILENT                = 0x0004
Global Const $FOF_RENAMEONCOLLISION  = 0x0008
Global Const $FOF_NOCONFIRMATION        = 0x0010
Global Const $FOF_WANTMAPPINGHANDLE  = 0x0020
Global Const $FOF_ALLOWUNDO          = 0x0040
Global Const $FOF_FILESONLY          = 0x0080
Global Const $FOF_SIMPLEPROGRESS        = 0x0100
Global Const $FOF_NOCONFIRMMKDIR        = 0x0200
Global Const $FOF_NOERRORUI          = 0x0400
Global Const $FOF_NOCOPYSECURITYATTRIBS = 0x0800
Global Const $FOF_NORECURSION          = 0x1000
Global Const $FOF_NO_CONNECTED_ELEMENTS = 0x2000
Global Const $FOF_WANTNUKEWARNING      = 0x4000
Global Const $FOF_NORECURSEREPARSE    = 0x8000

Dim $n

$n = _CopyWithProgress("C:\SourceDir1" & @LF & "C:\SourceDir2", "D:\DestDir")

Func _CopyWithProgress($sFrom, $sTo)
    Local $SHFILEOPSTRUCT
    Local $pFrom
    Local $pTo
    Local $aDllRet
    Local $nError = 0
    Local $i
    
    $SHFILEOPSTRUCT = DllStructCreate("int;uint;ptr;ptr;uint;int;ptr;ptr")
    If @error Then Return False     
; hwnd
    DllStructSetData($SHFILEOPSTRUCT, 1, 0)
; wFunc
    DllStructSetData($SHFILEOPSTRUCT, 2, $FO_COPY)
; pFrom
    $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
; pFrom will now be null-terminated at StringLen($sFrom)+1
    DllStructSetData($pFrom, 1, $sFrom)
    For $i = 1 To StringLen($sFrom)+2
        If DllStructGetData($pFrom, 1, $i) = 10 Then DllStructSetData($pFrom, 1, 0, $i)
    Next
; We need a second null at the end
    DllStructSetData($pFrom, 1, 0, StringLen($sFrom)+2)
    DllStructSetData($SHFILEOPSTRUCT, 3, DllStructGetPtr($pFrom))
; pTo
    $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
; pTo will now be null-terminated at StringLen($sTo)+1
    DllStructSetData($pTo, 1, $sTo)
; We need a second null at the end
    DllStructSetData($pTo, 1, 0, StringLen($sTo)+2)
    DllStructSetData($SHFILEOPSTRUCT, 4, DllStructGetPtr($pTo))
; fFlags
    DllStructSetData($SHFILEOPSTRUCT, 5, BitOR($FOF_NOCONFIRMMKDIR, _ 
        $FOF_NOCONFIRMATION, _ 
        $FOF_NOERRORUI))
; fAnyOperationsAborted
    DllStructSetData($SHFILEOPSTRUCT, 6, 0)
; hNameMappings
    DllStructSetData($SHFILEOPSTRUCT, 7, 0)
; lpszProgressTitle
    DllStructSetData($SHFILEOPSTRUCT, 8, 0)
    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    If @error Or $aDllRet[0] <> 0 Then
        $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
        If Not @error Then $nError = $aDllRet[0]
    EndIf
    DllStructDelete($pFrom)
    DllStructDelete($pTo)
    DllStructDelete($SHFILEOPSTRUCT)
    If $nError <> 0 Then 
        SetError($nError)
        Return False
    EndIf
    Return True
EndFunc

Edited by SumTingWong
Link to comment
Share on other sites

Is there a way to specify multiple input sources?

<{POST_SNAPBACK}>

pFrom

Address of a buffer to specify one or more source file names. These names must be fully qualified paths. Standard Microsoft MS-DOS wild cards, such as "*", are permitted in the file-name position. Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of pFrom.

pTo

Address of a buffer to contain the name of the destination file or directory. This parameter must be set to NULL if it is not used. Like pFrom, the pTo member is also a double-null terminated string and is handled in much the same way. However, pTo must meet the following specifications.

Wildcard characters are not supported.

Copy and Move operations can specify destination directories that do not exist and the system will attempt to create them. The system normally displays a dialog box to ask the user if they want to create the new directory. To suppress this dialog box and have the directories created silently, set the FOF_NOCONFIRMMKDIR flag in fFlags.

For Copy and Move operations, the buffer can contain multiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES.

Pack multiple names into the string in the same way as for pFrom.

Use only fully-qualified paths. Using relative paths will have unpredictable results.

Multiple sources support added. Edited by SumTingWong
Link to comment
Share on other sites

  • 11 months later...

:( very nice, aspecially with the multi input possibility.

Would it be possible (probably yes but easy?) to adinn a flag to only copy newer files?

So it can be used as a backup/sync tool?

I've been trying but :) I'm loosing hope in my capabilities.

Link to comment
Share on other sites

  • 6 months later...
  • 6 months later...

Hi, thx for this great function.

Is there a flag to hide the cancel button and the system menu (minimize, maximize close) ?

If no is there a way to intercept DllCall ? I don't know how to manipulate dll..

I try with running another script before _ExplorerCopy and it works but I'd like somthing clean

WinWait("Calcul du temps restant...") ;french title of the copy dialog box
ControlHide ("Calcul du temps restant...","","Button2") ;hidding the button
Edited by baghenamoth
Link to comment
Share on other sites

  • 10 months later...

Hey,

Sorry to bump an old post, but this is is such a good piece of code. I just want it to work. What I've got at the moment is the following:

Global Const $FO_MOVE                  = 0x0001
Global Const $FO_COPY                  = 0x0002
Global Const $FO_DELETE              = 0x0003
Global Const $FO_RENAME              = 0x0004

Global Const $FOF_MULTIDESTFILES        = 0x0001
Global Const $FOF_CONFIRMMOUSE        = 0x0002
Global Const $FOF_SILENT                = 0x0004
Global Const $FOF_RENAMEONCOLLISION  = 0x0008
Global Const $FOF_NOCONFIRMATION        = 0x0010
Global Const $FOF_WANTMAPPINGHANDLE  = 0x0020
Global Const $FOF_ALLOWUNDO          = 0x0040
Global Const $FOF_FILESONLY          = 0x0080
Global Const $FOF_SIMPLEPROGRESS        = 0x0100
Global Const $FOF_NOCONFIRMMKDIR        = 0x0200
Global Const $FOF_NOERRORUI          = 0x0400
Global Const $FOF_NOCOPYSECURITYATTRIBS = 0x0800
Global Const $FOF_NORECURSION          = 0x1000
Global Const $FOF_NO_CONNECTED_ELEMENTS = 0x2000
Global Const $FOF_WANTNUKEWARNING      = 0x4000
Global Const $FOF_NORECURSEREPARSE    = 0x8000

Dim $n

$n = _CopyWithProgress("F:\My Documents\Backups\files\Bus Driver " & @LF & " F:\Program Files\Valve", "D:\DestDir")

Func _CopyWithProgress($sFrom, $sTo)
    Local $SHFILEOPSTRUCT
    Local $pFrom
    Local $pTo
    Local $aDllRet
    Local $nError = 0
    Local $i
    
    $SHFILEOPSTRUCT = DllStructCreate("int;uint;ptr;ptr;uint;int;ptr;ptr")
    If @error Then Return False     
; hwnd
    DllStructSetData($SHFILEOPSTRUCT, 1, 0)
; wFunc
    DllStructSetData($SHFILEOPSTRUCT, 2, $FO_COPY)
; pFrom
    $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
; pFrom will now be null-terminated at StringLen($sFrom)+1
    DllStructSetData($pFrom, 1, $sFrom)
    For $i = 1 To StringLen($sFrom)+2
        If DllStructGetData($pFrom, 1, $i) = 10 Then DllStructSetData($pFrom, 1, 0, $i)
    Next
; We need a second null at the end
    DllStructSetData($pFrom, 1, 0, StringLen($sFrom)+2)
    DllStructSetData($SHFILEOPSTRUCT, 3, DllStructGetPtr($pFrom))
; pTo
    $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
; pTo will now be null-terminated at StringLen($sTo)+1
    DllStructSetData($pTo, 1, $sTo)
; We need a second null at the end
    DllStructSetData($pTo, 1, 0, StringLen($sTo)+2)
    DllStructSetData($SHFILEOPSTRUCT, 4, DllStructGetPtr($pTo))
; fFlags
    DllStructSetData($SHFILEOPSTRUCT, 5, BitOR($FOF_NOCONFIRMMKDIR, _
        $FOF_NOCONFIRMATION, _
        $FOF_NOERRORUI))
; fAnyOperationsAborted
    DllStructSetData($SHFILEOPSTRUCT, 6, 0)
; hNameMappings
    DllStructSetData($SHFILEOPSTRUCT, 7, 0)
; lpszProgressTitle
    DllStructSetData($SHFILEOPSTRUCT, 8, 0)
    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    If @error Or $aDllRet[0] <> 0 Then
        $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
        If Not @error Then $nError = $aDllRet[0]
    EndIf
    $pFrom = 0
    $pTo = 0
    $SHFILEOPSTRUCT = 0
    If $nError <> 0 Then
        SetError($nError)
        Return False
    EndIf
    Return True
EndFunc

Pretty much exactly the original, but updated.

Now why does it not work?? I can get 1 file working, but not the 2. I'll try to get it to work, but if anyone has a fix it would be apriciated.

Link to comment
Share on other sites

Got It! Just used

$n = _CopyWithProgress("F:\My Documents\Backups\files\Bus Driver" & Chr (0x00) & "F:\Program Files\Valve", "D:\DestDir")

EDIT: Well it works for the first folder... :)

Edited by Bert
Link to comment
Share on other sites

  • 1 month later...

Now I need to use this function in my project and it really doesn't work for more than one input source.

After some debugging and trying I have found BUG and created FIX:

Global Const $FO_MOVE                   = 0x0001
Global Const $FO_COPY                   = 0x0002
Global Const $FO_DELETE                 = 0x0003
Global Const $FO_RENAME                 = 0x0004

Global Const $FOF_MULTIDESTFILES        = 0x0001
Global Const $FOF_CONFIRMMOUSE          = 0x0002
Global Const $FOF_SILENT                = 0x0004
Global Const $FOF_RENAMEONCOLLISION     = 0x0008
Global Const $FOF_NOCONFIRMATION        = 0x0010
Global Const $FOF_WANTMAPPINGHANDLE     = 0x0020
Global Const $FOF_ALLOWUNDO             = 0x0040
Global Const $FOF_FILESONLY             = 0x0080
Global Const $FOF_SIMPLEPROGRESS        = 0x0100
Global Const $FOF_NOCONFIRMMKDIR        = 0x0200
Global Const $FOF_NOERRORUI             = 0x0400
Global Const $FOF_NOCOPYSECURITYATTRIBS = 0x0800
Global Const $FOF_NORECURSION           = 0x1000
Global Const $FOF_NO_CONNECTED_ELEMENTS = 0x2000
Global Const $FOF_WANTNUKEWARNING       = 0x4000
Global Const $FOF_NORECURSEREPARSE      = 0x8000

$n = _CopyWithProgress("F:\My Documents\Backups\files\Bus Driver" & @LF & "F:\Program Files\Valve", "D:\DestDir")
;~ If Not $n Then ConsoleWrite('error:' & @error & @CRLF)

Func _CopyWithProgress($sFrom, $sTo)
    Local $SHFILEOPSTRUCT, $pFrom, $pTo, $aDllRet, $i, $nError = 0
        $SHFILEOPSTRUCT = DllStructCreate("hwnd hwnd;uint wFunc;ptr pFrom;ptr pTo;int fFlags;int fAnyOperationsAborted;ptr hNameMappings;ptr lpszProgressTitle")
    DllStructSetData($SHFILEOPSTRUCT, "hwnd", 0)
    DllStructSetData($SHFILEOPSTRUCT, "wFunc", $FO_COPY)
        $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
    DllStructSetData($pFrom, 1, $sFrom)
    For $i = 1 To StringLen($sFrom)+2
        If DllStructGetData($pFrom, 1, $i) = Chr(10) Then DllStructSetData($pFrom, 1, Chr(0), $i)
    Next
    DllStructSetData($pFrom, 1, Chr(0), StringLen($sFrom)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pFrom", DllStructGetPtr($pFrom))
        $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
    DllStructSetData($pTo, 1, $sTo)
    DllStructSetData($pTo, 1, Chr(0), StringLen($sTo)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pTo", DllStructGetPtr($pTo))
        DllStructSetData($SHFILEOPSTRUCT, "fFlags", BitOR($FOF_NOCONFIRMMKDIR, $FOF_NOCONFIRMATION, $FOF_NOERRORUI))
    DllStructSetData($SHFILEOPSTRUCT, "fAnyOperationsAborted", 0)
    DllStructSetData($SHFILEOPSTRUCT, "hNameMappings", 0)
    DllStructSetData($SHFILEOPSTRUCT, "lpszProgressTitle", 0)
    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    If @error Or $aDllRet[0] <> 0 Then
        $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
        If Not @error Then $nError = $aDllRet[0]
    EndIf
        ; test if button Abort was pressed
    If DllStructGetData($SHFILEOPSTRUCT, "fAnyOperationsAborted") Then $nError = -1

    $pFrom = 0
    $pTo = 0
    $SHFILEOPSTRUCT = 0
        If $nError <> 0 Then
        SetError($nError)
        Return False
    EndIf
    Return True
EndFunc

Note:

- you can use also fileName/filenames with full path as source file.

- fAnyOperationsAborted not tested

Here is MSDN link:

http://msdn.microsoft.com/en-us/library/bb762164(VS.85).aspx

http://msdn.microsoft.com/en-us/library/bb759795(VS.85).aspx

Link to comment
Share on other sites

And here is simplified version when you call it with Chr(0) instead of @LF:

Global Const $FO_MOVE                   = 0x0001
Global Const $FO_COPY                   = 0x0002
Global Const $FO_DELETE                 = 0x0003
Global Const $FO_RENAME                 = 0x0004

Global Const $FOF_MULTIDESTFILES        = 0x0001
Global Const $FOF_CONFIRMMOUSE          = 0x0002
Global Const $FOF_SILENT                = 0x0004
Global Const $FOF_RENAMEONCOLLISION     = 0x0008
Global Const $FOF_NOCONFIRMATION        = 0x0010
Global Const $FOF_WANTMAPPINGHANDLE     = 0x0020
Global Const $FOF_ALLOWUNDO             = 0x0040
Global Const $FOF_FILESONLY             = 0x0080
Global Const $FOF_SIMPLEPROGRESS        = 0x0100
Global Const $FOF_NOCONFIRMMKDIR        = 0x0200
Global Const $FOF_NOERRORUI             = 0x0400
Global Const $FOF_NOCOPYSECURITYATTRIBS = 0x0800
Global Const $FOF_NORECURSION           = 0x1000
Global Const $FOF_NO_CONNECTED_ELEMENTS = 0x2000
Global Const $FOF_WANTNUKEWARNING       = 0x4000
Global Const $FOF_NORECURSEREPARSE      = 0x8000

$n = _CopyWithProgress("F:\My Documents\Backups\files\Bus Driver" & Chr(0) & "F:\Program Files\Valve", "D:\DestDir")
;~ If Not $n Then ConsoleWrite('error:' & @error & @CRLF)

Func _CopyWithProgress($sFrom, $sTo)
    Local $SHFILEOPSTRUCT, $pFrom, $pTo, $aDllRet, $nError = 0
        $SHFILEOPSTRUCT = DllStructCreate("hwnd hwnd;uint wFunc;ptr pFrom;ptr pTo;int fFlags;int fAnyOperationsAborted;ptr hNameMappings;ptr lpszProgressTitle")
    DllStructSetData($SHFILEOPSTRUCT, "hwnd", 0)
    DllStructSetData($SHFILEOPSTRUCT, "wFunc", $FO_COPY)
        $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
    DllStructSetData($pFrom, 1, $sFrom)
    DllStructSetData($pFrom, 1, Chr(0), StringLen($sFrom)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pFrom", DllStructGetPtr($pFrom))
        $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
    DllStructSetData($pTo, 1, $sTo)
    DllStructSetData($pTo, 1, Chr(0), StringLen($sTo)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pTo", DllStructGetPtr($pTo))
        DllStructSetData($SHFILEOPSTRUCT, "fFlags", BitOR($FOF_NOCONFIRMMKDIR, $FOF_NOCONFIRMATION, $FOF_NOERRORUI))
    DllStructSetData($SHFILEOPSTRUCT, "fAnyOperationsAborted", 0)
    DllStructSetData($SHFILEOPSTRUCT, "hNameMappings", 0)
    DllStructSetData($SHFILEOPSTRUCT, "lpszProgressTitle", 0)
    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    If @error Or $aDllRet[0] <> 0 Then
        $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
        If Not @error Then $nError = $aDllRet[0]
    EndIf
        ; test if button Abort was pressed
    If DllStructGetData($SHFILEOPSTRUCT, "fAnyOperationsAborted") Then $nError = -1

    $pFrom = 0
    $pTo = 0
    $SHFILEOPSTRUCT = 0
        If $nError <> 0 Then
        SetError($nError)
        Return False
    EndIf
    Return True
EndFunc
Link to comment
Share on other sites

I have problems with using lpszProgressTitle struct param.

I have always memory read/write error message. I tried every possible combinations.

Please can somebody help me with this?

this script is without constants (they are the same as in above scripts):

$n = _CopyWithProgress("F:\My Documents\Backups\files\Bus Driver" & Chr(0) & "F:\Program Files\Valve", "D:\DestDir", "Test Title...")

Func _CopyWithProgress($sFrom, $sTo, $sTitle = '')
    Local $SHFILEOPSTRUCT, $pFrom, $pTo, $pTitle, $aDllRet, $nError = 0
    Local $flags = BitOR($FOF_NOCONFIRMMKDIR, $FOF_NOCONFIRMATION, $FOF_NOERRORUI)
        $SHFILEOPSTRUCT = DllStructCreate("hwnd hwnd;uint wFunc;ptr pFrom;ptr pTo;int fFlags;int fAnyOperationsAborted;ptr hNameMappings;ptr lpszProgressTitle")
    DllStructSetData($SHFILEOPSTRUCT, "hwnd", 0)
    DllStructSetData($SHFILEOPSTRUCT, "wFunc", $FO_COPY)
        $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
    DllStructSetData($pFrom, 1, $sFrom)
    DllStructSetData($pFrom, 1, Chr(0), StringLen($sFrom)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pFrom", DllStructGetPtr($pFrom))
        $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
    DllStructSetData($pTo, 1, $sTo)
    DllStructSetData($pTo, 1, Chr(0), StringLen($sTo)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pTo", DllStructGetPtr($pTo))
        DllStructSetData($SHFILEOPSTRUCT, "fAnyOperationsAborted", 0)
    DllStructSetData($SHFILEOPSTRUCT, "hNameMappings", 0)
        If $sTitle <> '' Then
        $pTitle = DllStructCreate("char[" & StringLen($sTitle)+1 & "]")
        ;$pTitle = DllStructCreate("char[4]")
        DllStructSetData($pTitle, 1, $sTitle)
        ;DllStructSetData($pTitle, 1, 'aaa')
        DllStructSetData($pTitle, 1, Chr(0), StringLen($sTitle)+1) ; null at the end

        $flags = BitOR($flags, $FOF_SIMPLEPROGRESS)
        DllStructSetData($SHFILEOPSTRUCT, "lpszProgressTitle", DllStructGetPtr($pTitle))
;~      DllStructSetData($SHFILEOPSTRUCT, "lpszProgressTitle", 'aaa')
    Else
        DllStructSetData($SHFILEOPSTRUCT, "lpszProgressTitle", 0)
    EndIf
        DllStructSetData($SHFILEOPSTRUCT, "fFlags", $flags)

    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    If @error Or $aDllRet[0] <> 0 Then
        $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
        If Not @error Then $nError = $aDllRet[0]
    EndIf
        ; test if button Abort was pressed
    If DllStructGetData($SHFILEOPSTRUCT, "fAnyOperationsAborted") Then $nError = -1

    $pFrom = 0
    $pTo = 0
    $pTitle = 0
    $SHFILEOPSTRUCT = 0
        If $nError <> 0 Then
        SetError($nError)
        Return False
    EndIf
    Return True
EndFunc
Link to comment
Share on other sites

  • 2 months later...
  • 2 months later...
  • 1 year later...

And here is simplified version when you call it with Chr(0) instead of @LF:

Global Const $FO_MOVE                   = 0x0001
Global Const $FO_COPY                   = 0x0002
Global Const $FO_DELETE                 = 0x0003
Global Const $FO_RENAME                 = 0x0004

Global Const $FOF_MULTIDESTFILES        = 0x0001
Global Const $FOF_CONFIRMMOUSE          = 0x0002
Global Const $FOF_SILENT                = 0x0004
Global Const $FOF_RENAMEONCOLLISION     = 0x0008
Global Const $FOF_NOCONFIRMATION        = 0x0010
Global Const $FOF_WANTMAPPINGHANDLE     = 0x0020
Global Const $FOF_ALLOWUNDO             = 0x0040
Global Const $FOF_FILESONLY             = 0x0080
Global Const $FOF_SIMPLEPROGRESS        = 0x0100
Global Const $FOF_NOCONFIRMMKDIR        = 0x0200
Global Const $FOF_NOERRORUI             = 0x0400
Global Const $FOF_NOCOPYSECURITYATTRIBS = 0x0800
Global Const $FOF_NORECURSION           = 0x1000
Global Const $FOF_NO_CONNECTED_ELEMENTS = 0x2000
Global Const $FOF_WANTNUKEWARNING       = 0x4000
Global Const $FOF_NORECURSEREPARSE      = 0x8000

$n = _CopyWithProgress("F:\My Documents\Backups\files\Bus Driver" & Chr(0) & "F:\Program Files\Valve", "D:\DestDir")
;~ If Not $n Then ConsoleWrite('error:' & @error & @CRLF)

Func _CopyWithProgress($sFrom, $sTo)
    Local $SHFILEOPSTRUCT, $pFrom, $pTo, $aDllRet, $nError = 0
        $SHFILEOPSTRUCT = DllStructCreate("hwnd hwnd;uint wFunc;ptr pFrom;ptr pTo;int fFlags;int fAnyOperationsAborted;ptr hNameMappings;ptr lpszProgressTitle")
    DllStructSetData($SHFILEOPSTRUCT, "hwnd", 0)
    DllStructSetData($SHFILEOPSTRUCT, "wFunc", $FO_COPY)
        $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
    DllStructSetData($pFrom, 1, $sFrom)
    DllStructSetData($pFrom, 1, Chr(0), StringLen($sFrom)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pFrom", DllStructGetPtr($pFrom))
        $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
    DllStructSetData($pTo, 1, $sTo)
    DllStructSetData($pTo, 1, Chr(0), StringLen($sTo)+2) ; second null at the end
    DllStructSetData($SHFILEOPSTRUCT, "pTo", DllStructGetPtr($pTo))
        DllStructSetData($SHFILEOPSTRUCT, "fFlags", BitOR($FOF_NOCONFIRMMKDIR, $FOF_NOCONFIRMATION, $FOF_NOERRORUI))
    DllStructSetData($SHFILEOPSTRUCT, "fAnyOperationsAborted", 0)
    DllStructSetData($SHFILEOPSTRUCT, "hNameMappings", 0)
    DllStructSetData($SHFILEOPSTRUCT, "lpszProgressTitle", 0)
    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    If @error Or $aDllRet[0] <> 0 Then
        $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
        If Not @error Then $nError = $aDllRet[0]
    EndIf
        ; test if button Abort was pressed
    If DllStructGetData($SHFILEOPSTRUCT, "fAnyOperationsAborted") Then $nError = -1

    $pFrom = 0
    $pTo = 0
    $SHFILEOPSTRUCT = 0
        If $nError <> 0 Then
        SetError($nError)
        Return False
    EndIf
    Return True
EndFunc

yet another sorry for bumping old post.

I'm searching the way to use dircopy without my gui being freezed.

I found this thread and tried your code it work except that I think I found a bug.

$n = _CopyWithProgress(@DesktopDir, @MyDocumentsDir & "\desktopbackup")

It doesn't copy the desktop dir, but it worked if use another macro like @programfiles which I think it might be the macro problem.

Unless I use fake dir(which the dir not even exist) like this

$n = _CopyWithProgress(@DesktopDir & Chr(0) & "c:\fake", @MyDocumentsDir & "\desktopbackup")

It will copy the desktop dir.

thanks

Link to comment
Share on other sites

  • 2 years later...

An updated version of the original post. This catches the user pressing 'Cancel' while copying. The primary difference is in the definition of the struct. The idea for this came from this post

Global Const $FO_MOVE                 = 0x0001
Global Const $FO_COPY                 = 0x0002
Global Const $FO_DELETE           = 0x0003
Global Const $FO_RENAME           = 0x0004

Global Const $FOF_MULTIDESTFILES        = 0x0001
Global Const $FOF_CONFIRMMOUSE      = 0x0002
Global Const $FOF_SILENT                = 0x0004
Global Const $FOF_RENAMEONCOLLISION  = 0x0008
Global Const $FOF_NOCONFIRMATION        = 0x0010
Global Const $FOF_WANTMAPPINGHANDLE  = 0x0020
Global Const $FOF_ALLOWUNDO       = 0x0040
Global Const $FOF_FILESONLY       = 0x0080
Global Const $FOF_SIMPLEPROGRESS        = 0x0100
Global Const $FOF_NOCONFIRMMKDIR        = 0x0200
Global Const $FOF_NOERRORUI       = 0x0400
Global Const $FOF_NOCOPYSECURITYATTRIBS = 0x0800
Global Const $FOF_NORECURSION         = 0x1000
Global Const $FOF_NO_CONNECTED_ELEMENTS = 0x2000
Global Const $FOF_WANTNUKEWARNING     = 0x4000
Global Const $FOF_NORECURSEREPARSE  = 0x8000

Func _CopyWithProgress($sFrom, $sTo)
    Local $SHFILEOPSTRUCT
    Local $pFrom
    Local $pTo
    Local $aDllRet
    Local $nError = 0
    Local $i

    $SHFILEOPSTRUCT = DllStructCreate("align 1;hwnd;uint;ptr;ptr;ushort;bool;ptr;ptr")
    If @error Then Return False
; hwnd
    DllStructSetData($SHFILEOPSTRUCT, 1, 0)
; wFunc
    DllStructSetData($SHFILEOPSTRUCT, 2, $FO_COPY)
; pFrom
    $pFrom = DllStructCreate("char[" & StringLen($sFrom)+2 & "]")
; pFrom will now be null-terminated at StringLen($sFrom)+1
    DllStructSetData($pFrom, 1, $sFrom)
    For $i = 1 To StringLen($sFrom)+2
        If DllStructGetData($pFrom, 1, $i) = 10 Then DllStructSetData($pFrom, 1, 0, $i)
    Next
; We need a second null at the end
    DllStructSetData($pFrom, 1, 0, StringLen($sFrom)+2)
    DllStructSetData($SHFILEOPSTRUCT, 3, DllStructGetPtr($pFrom))
; pTo
    $pTo = DllStructCreate("char[" & StringLen($sTo)+2 & "]")
; pTo will now be null-terminated at StringLen($sTo)+1
    DllStructSetData($pTo, 1, $sTo)
; We need a second null at the end
    DllStructSetData($pTo, 1, 0, StringLen($sTo)+2)
    DllStructSetData($SHFILEOPSTRUCT, 4, DllStructGetPtr($pTo))
; fFlags
    DllStructSetData($SHFILEOPSTRUCT, 5, BitOR($FOF_NOCONFIRMMKDIR, _
        $FOF_NOCONFIRMATION, _
        $FOF_NOERRORUI))
; fAnyOperationsAborted
    DllStructSetData($SHFILEOPSTRUCT, 6, 0)
; hNameMappings
    DllStructSetData($SHFILEOPSTRUCT, 7, 0)
; lpszProgressTitle
    DllStructSetData($SHFILEOPSTRUCT, 8, 0)
    $aDllRet = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($SHFILEOPSTRUCT))
    If @error Or $aDllRet[0] <> 0 Then
        $aDllRet = DllCall("kernel32.dll", "long", "GetLastError")
        If Not @error Then $nError = $aDllRet[0]
    EndIf
    Dim $aborted = DllStructGetData($SHFILEOPSTRUCT, 6)
    DllStructDelete($pFrom)
    DllStructDelete($pTo)
    DllStructDelete($SHFILEOPSTRUCT)
    If $nError <> 0 Then
        SetError($nError)
        Return False
    elseif $aborted Then
        Return False
    EndIf
    Return True
EndFunc

Func DllStructDelete(ByRef $vStruct)
    $vStruct = ""
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...