#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Global Const $FO_COPY = 0x0002
Global Const $FOF_SIMPLEPROGRESS = 0x0100
Global Const $FOF_NOCONFIRMATION = 0x0010
Global $Offenes_Projekt = "", $msg
$GUI = GUICreate("My GUI", 300, 80)
$Button_1 = GUICtrlCreateButton("复制文件", 160, 30, 100)
$Button_2 = GUICtrlCreateButton("复制文件夹", 20, 30, 100)
GUISetState(@SW_SHOW)
While 1
$msg = GUIGetMsg()
Select
Case $msg = $GUI_EVENT_CLOSE
ExitLoop
Case $msg = $Button_1
_Try_to_import_file()
Case $msg = $Button_2
_Try_to_import_folder()
EndSelect
WEnd
Func _Try_to_import_file();复制文件
$var = FileOpenDialog('选择复制源文件...', "::{20D04FE0-3AEA-1069-A2D8-08002B30309D}", '所有文件' & " (*.*)", 1 + 2 + 4, "", $GUI)
FileChangeDir(@ScriptDir)
If @error Then
Return
Else
$Count = 0
$res = FileSelectFolder('选择复制目标路径...', $Offenes_Projekt, 2, "", $GUI)
If @error Then
Return
Else
If StringInStr($var, "|") = 0 Then
$Count = 1
_FileOperationProgress($var, $res, 1, $FO_COPY, $FOF_SIMPLEPROGRESS + $FOF_NOCONFIRMATION)
If @extended == 1 Then ;ERROR
Return
EndIf
Else
$pfad = StringTrimRight($var, (StringLen($var) - StringInStr($var, "|")) + 1)
$filelist = StringTrimLeft($var, (StringInStr($var, "|"))) & "|"
$Count = 0
While StringLen($filelist) > 1
$Datei = StringTrimRight($filelist, (StringLen($filelist) - StringInStr($filelist, "|")) + 1)
_FileOperationProgress($pfad & "" & $Datei, $res, 1, $FO_COPY, $FOF_SIMPLEPROGRESS + $FOF_NOCONFIRMATION)
$filelist = StringTrimLeft($filelist, StringInStr($filelist, "|"))
$Count = $Count + 1
WEnd
EndIf
EndIf
EndIf
MsgBox(32, '信息', '文件复制成功!')
EndFunc ;==>_Try_to_import_file
Func _Try_to_import_folder() ; '复制文件夹'
$var = FileSelectFolder('选择要复制源文件夹...', "", 1, "", $GUI)
If @error Then
Return
Else
$Count = 0
$res = FileSelectFolder('选择复制目标文件夹...', $Offenes_Projekt, 2, "", $GUI)
If @error Then
Return
Else
_FileOperationProgress($var & "\*.*", $res, 1, $FO_COPY, $FOF_SIMPLEPROGRESS + $FOF_NOCONFIRMATION)
If @extended == 1 Then ;ERROR
Return
EndIf
EndIf
EndIf
FileChangeDir(@ScriptDir)
MsgBox(32, '信息', '文件夹复制完成!')
EndFunc ;==>_Try_to_import_folder
Func _FileOperationProgress(Const $source, Const $S_DEST = "", Const $I_CREATEDIR = 0, Const $HEX_MODE = 0x0002, Const $HEX_FLAGS = 0x0000)
Local $objShell = 0
Local $objFolder = 0
Local $objScripting = 0
Local $shfileopstruct = 0
Local $pFrom = 0
Local $pTo = 0
Local $a_dllresult
; 创建文件运算结构
$shfileopstruct = DllStructCreate("int;uint;ptr;ptr;uint;int;ptr;ptr")
If @error <> 0 Then
SetError(@error, 3)
Return (0)
EndIf
; 设置文件运算中元素1的数据为 0,元素2为$HEX_MODE
DllStructSetData($shfileopstruct, 1, 0) ;~ hwnd 成功返回 0
DllStructSetData($shfileopstruct, 2, $HEX_MODE) ;~ wFunc 成功返回 2
#region - pFrom
; 创建源路径结构
$pFrom = DllStructCreate("char[" & StringLen($source) + 2 & "]")
DllStructSetData($pFrom, 1, $source) ; 设置源路径中元素1为源路径字串
For $i = 1 To StringLen($source) + 2
If DllStructGetData($pFrom, 1, $i) = 10 Then
DllStructSetData($pFrom, 1, 0, $i)
EndIf
Next
DllStructSetData($pFrom, 1, 0, StringLen($source) + 2)
DllStructSetData($shfileopstruct, 3, DllStructGetPtr($pFrom))
#endregion - pFrom
#region - pTo
$pTo = DllStructCreate("char[" & StringLen($S_DEST) + 2 & "]")
DllStructSetData($pTo, 1, $S_DEST)
DllStructSetData($pTo, 1, 0, StringLen($S_DEST) + 2)
DllStructSetData($shfileopstruct, 4, DllStructGetPtr($pTo))
#endregion - pTo
DllStructSetData($shfileopstruct, 5, $HEX_FLAGS) ;~ fFlags
DllStructSetData($shfileopstruct, 6, 0) ;~ fAnyOperationsAborted
DllStructSetData($shfileopstruct, 7, 0) ;~ hNameMappings
DllStructSetData($shfileopstruct, 8, 0) ;~ lpszProgressTitle
$a_dllresult = DllCall("shell32.dll", "int", "SHFileOperation", "ptr", DllStructGetPtr($shfileopstruct))
If @error <> 0 Then
SetError(@error, 2)
Return (0)
EndIf
$shfileopstruct = 0
$pFrom = 0
$pTo = 0
If $a_dllresult[0] <> 0 Then
SetError($a_dllresult[0], 1)
Return (0)
EndIf
Return (1)
EndFunc ;==>_FileOperationProgress