找回密码
 加入
搜索
查看: 1877|回复: 3

[转贴] XCOPY文件夹复制代码

[复制链接]
发表于 2018-7-1 14:13:35 | 显示全部楼层 |阅读模式
代码出自AU3英文论坛,翻译来自百度。给大家伙学习下。

#NoTrayIcon
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <GUIListBox.au3>
#include <ListViewConstants.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Date.au3>

$Xcopy_GUI = GUICreate("XCopy GUI Version2", 834, 600, -1, -1)

;target information
$Target_Information = GUICtrlCreateGroup(" 目标信息 ", 8, 16, 817, 73)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
GUICtrlCreateLabel("来源", 16, 40, 38, 17)
$SourceInput = GUICtrlCreateInput("", 80, 37, 577, 21)
GUICtrlSetColor(-1, 0xF0F0F0)
GUICtrlSetBkColor(-1, 0x008080)
$Sourcebtn = GUICtrlCreateButton("选择文件夹", 664, 36, 153, 23)
GUICtrlCreateLabel("目的地", 16, 64, 57, 17)
$DestInput = GUICtrlCreateInput("", 80, 60, 577, 21)
GUICtrlSetBkColor(-1, 0x1B80E4)
$Destbtn = GUICtrlCreateButton("选择目的地", 664, 60, 153, 21)
GUICtrlSetState($Destbtn, $GUI_DISABLE)
GUICtrlCreateGroup("", -99, -99, 1, 1)

;xcopy options
$Xcopy_Options = GUICtrlCreateGroup(" XCopy 选项 ", 8, 96, 817, 137)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Option_subdirs = GUICtrlCreateCheckbox("复制目录和子目录,包括空目录.", 16, 112, 400, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$option_assumedir = GUICtrlCreateCheckbox("如果目标不存在,且要复制多个文件,则假定目标必须是目录.", 16, 131, 400, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$option_overwrite = GUICtrlCreateCheckbox("取消提示以确认要覆盖现有目标文件", 16, 151, 400, 17)
$option_errorcontinue = GUICtrlCreateCheckbox("忽略错误提示.", 16, 172, 400, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
$option_verify = GUICtrlCreateCheckbox("验证每个新文件的大小.", 16, 191, 400, 17)
$option_noname = GUICtrlCreateCheckbox("在复制时不显示文件名.", 16, 210, 400, 17)
$option_encryptedfiles = GUICtrlCreateCheckbox("将加密文件复制到目的地,不支持.", 424, 210, 400, 17)
$option_copyhands = GUICtrlCreateCheckbox("复制隐藏文件和系统文件.", 424, 191, 400, 17)
$option_copyarchoff = GUICtrlCreateCheckbox("只复制带有存档属性的文件,然后取消它.", 424, 112, 400, 17)
$option_copyarchon = GUICtrlCreateCheckbox("只复制带有存档属性的文件,然后将其保留.", 424, 131, 400, 17)
$option_restartablemode = GUICtrlCreateCheckbox("在可重新启动模式下复制网络文件.", 424, 151, 400, 17)
If @OSVersion = "WIN_XP" Then
        $option_unbuffered = GUICtrlCreateCheckbox("使用非缓冲I/O.推荐用于非常大的文件.", 424, 172, 400, 17)
        GUICtrlSetState(-1, $GUI_UNCHECKED)
        GUICtrlSetState(-1, $GUI_DISABLE)
Else
        $option_unbuffered = GUICtrlCreateCheckbox("使用非缓冲I/O.推荐用于非常大的文件.", 424, 172, 400, 17)
        GUICtrlSetState(-1, $GUI_CHECKED)
EndIf

GUICtrlCreateGroup("", -99, -99, 1, 1)

;progress pain
$Session_Progress = GUICtrlCreateGroup(" 会话信息 ", 8, 240, 817, 321)
GUICtrlSetFont(-1, 8, 800, 0, "MS Sans Serif")
$Progress = GUICtrlCreateListView("", 16, 256, 801, 292, -1, $LVS_EX_GRIDLINES)
GUICtrlCreateGroup("", -99, -99, 1, 1)

;controls
$copy = GUICtrlCreateButton("开始复制", 677, 568, 147, 25)
GUICtrlSetState($copy, $GUI_DISABLE)

_GUICtrlListView_AddColumn($Progress, "描述", 580, 0)
_GUICtrlListView_AddColumn($Progress, "日期/时间", 200, 0)

GUISetState(@SW_SHOW)

While 1
        $nMsg = GUIGetMsg()
        Switch $nMsg
                Case $GUI_EVENT_CLOSE
                        Exit
                Case $Sourcebtn
                        $source = FileSelectFolder("选择源文件夹", "", 1 + 2 + 4)
                        If @error Then
                                ;check to see if no folder has been selected
                                MsgBox(48, "源文件夹", "没有选择文件夹或对话取消", 5)
                        Else
                                ;update source input
                                If $source = "computer" Then
                                        MsgBox(48, "警告!", "无效位置选择", 5)
                                Else
                                        GUICtrlSetData($SourceInput, $source, "")
                                        $tCur = _Date_Time_GetSystemTime()
                                        GUICtrlCreateListViewItem("选择源位置,检查大小|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                        Sleep(500)
                                        $sourcesize = DirGetSize(GUICtrlRead($SourceInput), 1)
                                        If Round($sourcesize[0] / 1024 / 1024) < 1000 Then
                                                $tCur = _Date_Time_GetSystemTime()
                                                GUICtrlCreateListViewItem("源目录大小(MB): " & Round($sourcesize[0] / 1024 / 1024) & "|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                        Else
                                                $tCur = _Date_Time_GetSystemTime()
                                                GUICtrlCreateListViewItem("源目录大小(GB): " & Round($sourcesize[0] / 1024 / 1024 / 1024, 2) & "|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                        EndIf

                                        GUICtrlSetState($Destbtn, $GUI_ENABLE)
                                EndIf
                        EndIf
                Case $Destbtn
                        $destination = FileSelectFolder("选择目标文件夹", "", 1 + 2 + 4)
                        ;read source location
                        $source = GUICtrlRead($SourceInput)
                        If @error Then
                                ;check to see if no folder has been selected
                                MsgBox(48, "目的文件夹", "没有选择文件夹或对话取消", 5)
                        ElseIf $source > "" Then
                                ;check source and destination are the same, if they are display 警告!
                                If $destination = $source Then
                                        MsgBox(48, "警告!", "源文件夹和目标文件夹不能相同!", 5)
                                Else
                                        If $destination = "computer" Then
                                                MsgBox(48, "警告!", "无效位置选择", 5)
                                        Else ;update destination input
                                                GUICtrlSetData($DestInput, $destination, "")
                                                $tCur = _Date_Time_GetSystemTime()
                                                GUICtrlCreateListViewItem("检查目的地位置和可用空间,请稍后|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                                If StringLen(GUICtrlRead($DestInput)) > 3 Then
                                                        $destloc = StringLeft(GUICtrlRead($DestInput), 3)
                                                        $destfreespace = DirGetSize($destloc, 1)
                                                Else
                                                        $destfreespace = DirGetSize(GUICtrlRead($DestInput), 1)
                                                EndIf
                                                ;check for Mb or Gb
                                                If Round($destfreespace[0] / 1024 / 1024) < 1000 Then
                                                        ;Check source is not bigger than destination
                                                        If Round($destfreespace[0] / 1024 / 1024) < Round($sourcesize[0] / 1024 / 1024) Then
                                                                MsgBox(48, "XCopy GUI", "在目的地上没有足够的空闲空间来复制所有数据。" & @CR & "请选择另一个目的地")
                                                                $tCur = _Date_Time_GetSystemTime()
                                                                GUICtrlCreateListViewItem("在目的地上没有足够的空闲空间|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                                                GUICtrlSetData($DestInput, "", "")
                                                        Else
                                                                $tCur = _Date_Time_GetSystemTime()
                                                                GUICtrlCreateListViewItem("目的地可用空间(MB): " & Round($destfreespace[0] / 1024 / 1024) & "|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                                                GUICtrlCreateListViewItem("足够的空间拷贝|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                                                GUICtrlSetState($copy, $GUI_ENABLE)
                                                        EndIf
                                                Else
                                                        If Round($destfreespace[0] / 1024 / 1024 / 1024, 2) < Round($sourcesize[0] / 1024 / 1024 / 1024, 2) Then
                                                                MsgBox(48, "XCopy GUI", "在目的地上没有足够的空闲空间来复制所有数据。" & @CR & "请选择另一个目的地")
                                                                $tCur = _Date_Time_GetSystemTime()
                                                                GUICtrlCreateListViewItem("在目的地上没有足够的空闲空间|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                                                GUICtrlSetData($DestInput, "", "")
                                                        Else
                                                                $tCur = _Date_Time_GetSystemTime()
                                                                GUICtrlCreateListViewItem("目的地可用空间(GB): " & Round($destfreespace[0] / 1024 / 1024 / 1024, 2) & "|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                                                GUICtrlCreateListViewItem("足够的自由空间拷贝|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                                                                GUICtrlSetState($copy, $GUI_ENABLE)
                                                        EndIf
                                                EndIf
                                        EndIf
                                EndIf
                        EndIf
                Case $copy
                        ; cmd line for xcopy is

                        ProgressOn("XCopy GUI", "请您稍候", "检查选项", -1, -1)
                        ProgressSet(10, "检查选项", "请您稍候")
                        Sleep(2000)
                        $subdirs = GUICtrlRead($Option_subdirs)
                        $assumedirs = GUICtrlRead($option_assumedir)
                        $overwrite = GUICtrlRead($option_overwrite)
                        $errorcontinue = GUICtrlRead($option_errorcontinue)
                        $verify = GUICtrlRead($option_verify)
                        $noname = GUICtrlRead($option_noname)
                        $encfiles = GUICtrlRead($option_encryptedfiles)
                        $copyhands = GUICtrlRead($option_copyhands)
                        $archoff = GUICtrlRead($option_copyarchoff)
                        $archon = GUICtrlRead($option_copyarchon)
                        $restartable = GUICtrlRead($option_restartablemode)
                        $unbuffered = GUICtrlRead($option_unbuffered)
                        ; 'XCOPY source destination /E /I /Y /C /V /Q /G /H /A /M /Z /J'

                        If $subdirs = 1 Then
                                $subdirs = "/E "
                        Else
                                $subdirs = ""
                        EndIf
                        If $assumedirs = 1 Then
                                $assumedirs = "/I "
                        Else
                                $assumedirs = ""
                        EndIf
                        If $overwrite = 1 Then
                                $overwrite = "/Y "
                        Else
                                $overwrite = ""
                        EndIf
                        If $errorcontinue = 1 Then
                                $errorcontinue = "/C "
                        Else
                                $errorcontinue = ""
                        EndIf
                        If $verify = 1 Then
                                $verify = "/V "
                        Else
                                $verify = ""
                        EndIf
                        If $noname = 1 Then
                                $noname = "/Q "
                        Else
                                $noname = ""
                        EndIf
                        If $encfiles = 1 Then
                                $encfiles = "/G "
                        Else
                                $encfiles = ""
                        EndIf
                        If $copyhands = 1 Then
                                $copyhands = "/H "
                        Else
                                $copyhands = ""
                        EndIf
                        If $archoff = 1 Then
                                $archoff = "/A "
                                If $archon = 1 Then
                                        GUICtrlSetState($option_copyarchon, $GUI_UNCHECKED)
                                EndIf
                        Else
                                $archoff = ""
                        EndIf
                        If $archon = 1 And $archoff = 4 Then
                                $archon = "/M "
                        Else
                                $archon = ""
                        EndIf
                        If $restartable = 1 Then
                                $restartable = "/Z "
                        Else
                                $restartable = ""
                        EndIf
                        If $unbuffered = 1 Then
                                $unbuffered = "/J"
                        Else
                                $unbuffered = ""
                        EndIf


                        ProgressSet(30, "选项集", "请您稍候")
                        Sleep(2000)
                        ProgressSet(40, "检查位置", "请您稍候")
                        $source = GUICtrlRead($SourceInput)
                        $destination = GUICtrlRead($DestInput)

                        If $source = "" Or $destination = "" Then
                                MsgBox(48, "警告!", "有一个路径有问题", 5)
                                Sleep(2000)
                                ProgressOff()
                        Else
                                Sleep(2000)
                                ProgressSet(50, "来源和目的地验证", "请您稍候")
                                Sleep(2000)
                        EndIf
                        ProgressSet(80, "准备执行", "请您稍候")
                        $cmdprep = 'Xcopy "' & $source & '" "' & $destination & '" ' & $subdirs & $assumedirs & $overwrite & $errorcontinue & $verify & $noname & $encfiles & $copyhands & $archoff & $archon & $restartable & $unbuffered


                        ProgressSet(100, "准备开始", "检查完成")
                        Sleep(2000)
                        ProgressOff()

                        $tCur = _Date_Time_GetSystemTime()
                        GUICtrlCreateListViewItem("提示 '" & $source & "' 到 '" & $destination & "' 开始|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                        ;subdirs,assumedir,overwrite,errorcontinue,verify,noname,encryptedfiles,copyhands,copyarchoff,copyarchon,restartablemode,unbuffered
                        ;disable the options
                        GUICtrlSetState($Option_subdirs, $GUI_DISABLE)
                        GUICtrlSetState($option_assumedir, $GUI_DISABLE)
                        GUICtrlSetState($option_overwrite, $GUI_DISABLE)
                        GUICtrlSetState($option_errorcontinue, $GUI_DISABLE)
                        GUICtrlSetState($option_verify, $GUI_DISABLE)
                        GUICtrlSetState($option_noname, $GUI_DISABLE)
                        GUICtrlSetState($option_encryptedfiles, $GUI_DISABLE)
                        GUICtrlSetState($option_copyhands, $GUI_DISABLE)
                        GUICtrlSetState($option_copyarchoff, $GUI_DISABLE)
                        GUICtrlSetState($option_copyarchon, $GUI_DISABLE)
                        GUICtrlSetState($option_restartablemode, $GUI_DISABLE)
                        GUICtrlSetState($option_unbuffered, $GUI_DISABLE)
                        GUICtrlSetState($Sourcebtn, $GUI_DISABLE)
                        GUICtrlSetState($Destbtn, $GUI_DISABLE)
                        GUICtrlSetState($copy, $GUI_DISABLE)
                        ;run the cmd
                        RunWait(@ComSpec & " /c " & $cmdprep, "", @SW_SHOW)
                        ;completed message
                        $sourcesize = DirGetSize(GUICtrlRead($SourceInput), 1)
                        $destsize = DirGetSize(GUICtrlRead($DestInput), 1)
                        ;check sizes match
                        If $destsize[0] = $sourcesize[0] Then
                                MsgBox(64, "Xcopy Gui", "'" & $source & "' 复制到 '" & $destination & "' 成功.", 3)
                                $tCur = _Date_Time_GetSystemTime()
                                GUICtrlCreateListViewItem("Xcopy来自 '" & $source & "' 到 '" & $destination & "' 完成|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)
                        Else
                                MsgBox(64, "Xcopy Gui", "'" & $source & "' 复制到 '" & $destination & "' ,但最后的大小不匹配。.", 3)
                                $tCur = _Date_Time_GetSystemTime()
                                GUICtrlCreateListViewItem("提示 '" & $source & "' 到 '" & $destination & "' 完成,但大小不匹配|" & _Date_Time_SystemTimeToDateTimeStr($tCur), $Progress)

                        EndIf

                        ;re-enable the options
                        GUICtrlSetState($Option_subdirs, $GUI_ENABLE)
                        GUICtrlSetState($option_assumedir, $GUI_ENABLE)
                        GUICtrlSetState($option_overwrite, $GUI_ENABLE)
                        GUICtrlSetState($option_errorcontinue, $GUI_ENABLE)
                        GUICtrlSetState($option_verify, $GUI_ENABLE)
                        GUICtrlSetState($option_noname, $GUI_ENABLE)
                        GUICtrlSetState($option_encryptedfiles, $GUI_ENABLE)
                        GUICtrlSetState($option_copyhands, $GUI_ENABLE)
                        GUICtrlSetState($option_copyarchoff, $GUI_ENABLE)
                        GUICtrlSetState($option_copyarchon, $GUI_ENABLE)
                        GUICtrlSetState($option_restartablemode, $GUI_ENABLE)
                        If @OSVersion <> "WIN_XP" Then
                                GUICtrlSetState($option_unbuffered, $GUI_ENABLE)
                        EndIf
                        GUICtrlSetState($Sourcebtn, $GUI_ENABLE)
                        GUICtrlSetState($Destbtn, $GUI_ENABLE)
                        GUICtrlSetState($copy, $GUI_ENABLE)



        EndSwitch
WEnd

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?加入

×

评分

参与人数 3威望 +2 金钱 +110 贡献 +10 收起 理由
lpxx + 20
chishingchan + 2 + 50 + 10 赞一个!
fybhwsx + 40 赞一个!

查看全部评分

发表于 2018-7-1 14:18:18 | 显示全部楼层
感谢版主分享。
发表于 2018-7-3 12:22:49 | 显示全部楼层
参考学习,谢谢分享!
发表于 2018-7-18 19:09:03 | 显示全部楼层

感谢版主分享.
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-3-29 20:05 , Processed in 0.076765 second(s), 22 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表