#include <File.au3>
#include <ProgressConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Main()
Func Main()
; 选择源文件夹
Local $sSourceDir = FileSelectFolder("选择要复制的源文件夹", "")
If @error Then Exit
; 选择目标文件夹
Local $sDestDir = FileSelectFolder("选择目标文件夹", "")
If @error Then Exit
; 获取源文件夹名称
Local $sFolderName = StringRegExpReplace($sSourceDir, "^.*\", "")
; 创建目标路径
$sDestDir = $sDestDir & "" & $sFolderName
; 检查目标文件夹是否已存在
If FileExists($sDestDir) Then
Local $iAnswer = MsgBox(4, "警告", "目标文件夹已存在,是否覆盖?")
If $iAnswer <> 6 Then Exit ; 6 = "是"
EndIf
; 创建进度条GUI
Local $hGUI = GUICreate("复制进度", 400, 150)
Local $idProgress = GUICtrlCreateProgress(20, 50, 360, 30)
Local $idLabel = GUICtrlCreateLabel("准备复制...", 20, 20, 360, 20)
Local $idFileLabel = GUICtrlCreateLabel("", 20, 90, 360, 20)
GUISetState(@SW_SHOW, $hGUI)
; 计算总文件数用于进度
Local $iTotalFiles = _FileCountFiles($sSourceDir, "*", $DIR_EXTENDED)
Local $iCopiedFiles = 0
; 开始复制
_CopyFolderWithProgress($sSourceDir, $sDestDir, $iTotalFiles, $iCopiedFiles, $idProgress, $idLabel, $idFileLabel)
; 完成消息
GUICtrlSetData($idLabel, "复制完成!")
MsgBox(0, "完成", "文件夹复制完成!")
GUIDelete($hGUI)
EndFunc
; 递归复制文件夹并更新进度条
Func _CopyFolderWithProgress($sSource, $sDest, $iTotalFiles, ByRef $iCopiedFiles, $idProgress, $idLabel, $idFileLabel)
; 创建目标文件夹
DirCreate($sDest)
; 查找源文件夹中的所有文件和子文件夹
Local $hSearch = FileFindFirstFile($sSource & "\*")
If $hSearch = -1 Then Return
While 1
Local $sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
Local $sSourcePath = $sSource & "" & $sFile
Local $sDestPath = $sDest & "" & $sFile
; 如果是目录则递归处理
If @extended Then
_CopyFolderWithProgress($sSourcePath, $sDestPath, $iTotalFiles, $iCopiedFiles, $idProgress, $idLabel, $idFileLabel)
Else
; 复制文件并更新进度
GUICtrlSetData($idLabel, "正在复制文件 " & $iCopiedFiles + 1 & "/" & $iTotalFiles)
GUICtrlSetData($idFileLabel, "当前文件: " & $sFile)
FileCopy($sSourcePath, $sDestPath, $FC_OVERWRITE + $FC_CREATEPATH)
$iCopiedFiles += 1
Local $iPercent = ($iCopiedFiles / $iTotalFiles) * 100
GUICtrlSetData($idProgress, $iPercent)
; 保持界面响应
Sleep(10) ; 可以调整或删除这个延迟
If GUIGetMsg() = $GUI_EVENT_CLOSE Then Exit
EndIf
WEnd
FileClose($hSearch)
EndFunc
; 计算文件夹中文件总数的函数
Func _FileCountFiles($sPath, $sFilter = "*", $iFlags = 0)
Local $iCount = 0
Local $hSearch = FileFindFirstFile($sPath & "" & $sFilter)
If $hSearch <> -1 Then
While 1
Local $sFile = FileFindNextFile($hSearch)
If @error Then ExitLoop
If @extended Then
; 如果是目录且需要递归
If BitAND($iFlags, $DIR_EXTENDED) Then
$iCount += _FileCountFiles($sPath & "" & $sFile, $sFilter, $iFlags)
EndIf
Else
$iCount += 1
EndIf
WEnd
FileClose($hSearch)
EndIf
Return $iCount
EndFunc