kn007 发表于 2009-9-29 21:24:34

求助,关于改UDF,我怎么改都老失败,做人也太失败了我觉得,找个人帮帮忙!

按以下源码,这样的话,是把D:\ABC中的所有东西copy到R:\temp
如:D:\abc只有个a.exe,那么运行后R:\temp\a.exe,而不是 R:\temp\abc\a.exe
下面的func要怎么改呢,如果能改的话,怎么改,哪位帮忙改下,然后在修改的源码后面加上4个“;”,即四个注释号


$save = D:\ABC
$open = R:\temp
$write = 1



ProgressCopy($save, $open, 1, 0, $write, 0)Func ProgressCopy($current, $destination, $UseMultiColour=0, $attrib = "-R", $overwrite = 1 ,$Run1 = 0 )

;FirstTimeRun Get original DirSize and set up Gui
If $Run1 = 0 Then
Global $OverallQty, $Overall, $source, $overallpercent, $Progress0Text, $progressbar1, $Progress1Text, $progressbar2, $Progress2Text, $LocalPercent
DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", 0)
If not FileExists ($Destination) then DirCreate ($Destination); This is why it was failing, the dir did not exist
$source = $current
If StringRight($current, 1) = '\' Then $current = StringTrimRight($current, 1)
If StringRight($destination, 1) <> '\' Then $destination = $destination & "\"
$tosearch = $current
$Overall = DirGetSize($tosearch, 1)
$OverallQty = $Overall
Global Const $PrCopyGui = GUICreate("进度条", 420, 100, -1, -1, -1, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
$Progress0Text = GUICtrlCreateLabel("Please Wait", 10, 5, 400, 20, $SS_LEFTNOWORDWRAP)
$progressbar1 = GUICtrlCreateProgress(10, 20, 400, 20)
GUICtrlSetColor(-1, 32250)
$Progress1Text = GUICtrlCreateLabel("", 10, 44, 400, 20, $SS_LEFTNOWORDWRAP)
$progressbar2 = GUICtrlCreateProgress(10, 60, 400, 20, $PBS_SMOOTH)
$Progress2Text = GUICtrlCreateLabel("", 10, 82, 400, 20, $SS_LEFTNOWORDWRAP)
GUISetFont(10, 600)
;$Progress2Text2 = GUICtrlCreateLabel("", 150, 62, 400, 20)
GUICtrlSetColor(-1, 32250); not working with Windows XP Style if not using windows classic style or dllcall above
GUISetState(@SW_SHOW)
GUICtrlSetData($Progress1Text, "Working Directory " & $tosearch)
$Run1 = 1
EndIf

$Size = DirGetSize($current, 3)
$Qty = $Size
Local $search = FileFindFirstFile($current & "\*.*")
While 1
Dim $file = FileFindNextFile($search)
If @error Or StringLen($file) < 1 Then ExitLoop
If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
$Qty -= 1
$LocalPercent = 100 - (($Qty / $Size) * 100)
$OverallQty -= 1
$overallpercent = 100 - (($OverallQty / $Overall) * 100)
GUICtrlSetData($Progress0Text, "Total Progress " & Int($overallpercent) & "% completed")
GUICtrlSetData($progressbar1, $overallpercent)
GUICtrlSetData($progressbar2, $LocalPercent)
GUICtrlSetData($Progress2Text, "Copying File " & $file)

If $useMultiColour then
GUICtrlSetColor($Progressbar2, _ChangeColour($LocalPercent))
GUICtrlSetColor($Progressbar1, _ChangeColour($OverallPercent))
EndIf

FileCopy($current & "\" & $file, $destination & StringTrimLeft($current, StringLen($source)) & "\" & $file,$overwrite)
FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
EndIf
If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
DirCreate($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file)
FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
GUICtrlSetData($Progress1Text, $current & "\" & $file)
ProgressCopy($current & "\" & $file, $destination, $UseMultiColour, $attrib, $overwrite,1)
EndIf
WEnd
FileClose($search)
;when overall percent = 100 set end gui text, delete gui and reset run1 to 0
If $overallpercent = 100 Then
GUICtrlSetData($Progress0Text, "Total Progress 100% completed")
GUICtrlSetData($progressbar1, 100)
GUICtrlSetData($progressbar2, 100)
GUICtrlSetData($Progress2Text, "Done!")
Sleep(2000)
GUIDelete($PRCopyGui)
$Run1 = 0
EndIf
EndFunc ;==>ProgressCopy

Func _ChangeColour($start)

$Redness = Int(255 - ($start / 100 * 512))
If $Redness < 0 Then $Redness = 0

$Greeness = Int(($start / 100 * 512) - 257)
If $Greeness < 0 Then $Greeness = 0

$Blueness = Int(255 - ($Redness + $Greeness))

Return ($Redness * 256 * 256) + ($Greeness * 256) + $Blueness

EndFunc

afan 发表于 2009-9-29 21:24:35

本帖最后由 afan 于 2009-9-30 19:45 编辑

修改了下,顺便增加了一个参数,使之可选择是否添加源路径文件夹#include <WindowsConstants.au3>
#include <StaticConstants.au3>
#include <ProgressConstants.au3>

;ProgressCopy("D:\autoit3", "E:\temp", 1) ;不添加源文件夹例子
ProgressCopy("D:\autoit3", "E:\temp", 1, '', '', '', 1) ;添加源文件夹例子

Func ProgressCopy($current, $destination, $UseMultiColour = 0, $attrib = "-R", $overwrite = 1, $Run1 = 0, $AddDir = 0) ;;;;修改增加$AddDir参数,默认0为不添加源文件夹
        ;FirstTimeRun Get original DirSize and set up Gui
        If $Run1 = 0 Then
                Global $OverallQty, $Overall, $source, $overallpercent, $Progress0Text, $progressbar1, $Progress1Text, $progressbar2, $Progress2Text, $LocalPercent, $szwjj
                DllCall("uxtheme.dll", "none", "SetThemeAppProperties", "int", 0)
                $source = $current
                If StringRight($current, 1) = '\' Then $current = StringTrimRight($current, 1)
                If StringRight($destination, 1) <> '\' Then $destination = $destination & "\"
                ;;;;------------------以下这段为新增部分
                If $AddDir = 1 Then
                        If StringInStr($current, '\') <> 0 Then
                                $szwjj = StringRegExp($current, '\\([^\\]+)$', 3)
                        Else
                                $szwjj = StringRegExp($current, '([^:]+):?', 3)
                        EndIf
                        $destination = $destination & $szwjj & "\"
                EndIf
                ;;;;------------------新增结束
                If Not FileExists($destination) Then DirCreate($destination); This is why it was failing, the dir did not exist;;;;变更此行代码位置
                $tosearch = $current
                $Overall = DirGetSize($tosearch, 1)
                $OverallQty = $Overall
                Global Const $PrCopyGui = GUICreate("进度条", 420, 100, -1, -1, -1, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
                $Progress0Text = GUICtrlCreateLabel("Please Wait", 10, 5, 400, 20, $SS_LEFTNOWORDWRAP)
                $progressbar1 = GUICtrlCreateProgress(10, 20, 400, 20)
                GUICtrlSetColor(-1, 32250)
                $Progress1Text = GUICtrlCreateLabel("", 10, 44, 400, 20, $SS_LEFTNOWORDWRAP)
                $progressbar2 = GUICtrlCreateProgress(10, 60, 400, 20, $PBS_SMOOTH)
                $Progress2Text = GUICtrlCreateLabel("", 10, 82, 400, 20, $SS_LEFTNOWORDWRAP)
                GUISetFont(10, 600)
                ;$Progress2Text2 = GUICtrlCreateLabel("", 150, 62, 400, 20)
                GUICtrlSetColor(-1, 32250); not working with Windows XP Style if not using windows classic style or dllcall above
                GUISetState(@SW_SHOW)
                GUICtrlSetData($Progress1Text, "Working Directory " & $tosearch)
                $Run1 = 1
        EndIf
        $Size = DirGetSize($current, 3)
        $Qty = $Size
        Local $search = FileFindFirstFile($current & "\*.*")
        While 1
                Dim $file = FileFindNextFile($search)
                If @error Or StringLen($file) < 1 Then ExitLoop
                If Not StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
                        $Qty -= 1
                        $LocalPercent = 100 - (($Qty / $Size) * 100)
                        $OverallQty -= 1
                        $overallpercent = 100 - (($OverallQty / $Overall) * 100)
                        GUICtrlSetData($Progress0Text, "Total Progress " & Int($overallpercent) & "% completed")
                        GUICtrlSetData($progressbar1, $overallpercent)
                        GUICtrlSetData($progressbar2, $LocalPercent)
                        GUICtrlSetData($Progress2Text, "Copying File " & $file)
                        If $UseMultiColour Then
                                GUICtrlSetColor($progressbar2, _ChangeColour($LocalPercent))
                                GUICtrlSetColor($progressbar1, _ChangeColour($overallpercent))
                        EndIf
                        FileCopy($current & "\" & $file, $destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $overwrite)
                        FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
                EndIf
                If StringInStr(FileGetAttrib($current & "\" & $file), "D") And ($file <> "." Or $file <> "..") Then
                        DirCreate($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file)
                        FileSetAttrib($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file, $attrib)
                        GUICtrlSetData($Progress1Text, $current & "\" & $file)
                        ProgressCopy($current & "\" & $file, $destination, $UseMultiColour, $attrib, $overwrite, 1, $AddDir) ;;;;修改增加$AddDir参数
                EndIf
        WEnd
        FileClose($search)
        ;when overall percent = 100 set end gui text, delete gui and reset run1 to 0
        If $overallpercent = 100 Then
                GUICtrlSetData($Progress0Text, "Total Progress 100% completed")
                GUICtrlSetData($progressbar1, 100)
                GUICtrlSetData($progressbar2, 100)
                GUICtrlSetData($Progress2Text, "Done!")
                Sleep(2000)
                GUIDelete($PrCopyGui)
                $Run1 = 0
        EndIf
EndFunc   ;==>ProgressCopy

Func _ChangeColour($start)
        $Redness = Int(255 - ($start / 100 * 512))
        If $Redness < 0 Then $Redness = 0
        $Greeness = Int(($start / 100 * 512) - 257)
        If $Greeness < 0 Then $Greeness = 0
        $Blueness = Int(255 - ($Redness + $Greeness))
        Return ($Redness * 256 * 256) + ($Greeness * 256) + $Blueness
EndFunc   ;==>_ChangeColour

顽固不化 发表于 2009-9-29 21:49:03

本帖最后由 顽固不化 于 2009-9-29 21:50 编辑

把filecopy一行改成下面2行试试?
$DesDIR=$destination & StringTrimLeft ($current ,3) & "\" & $file
FileCopy($current & "\" & $file, $DesDIR, 8+1)

kn007 发表于 2009-9-29 23:17:47

把FileCopy($current & "\" & $file, $destination & StringTrimLeft($current, StringLen($source)) & "\" & $file,$overwrite) 替换为$DesDIR=$destination & StringTrimLeft ($current ,3) & "\" & $file
FileCopy($current & "\" & $file, $DesDIR, 8+1)若是多文件和文件夹的话,就不行了
ProgressCopy("D:\autoit3", "E:\temp", 1),你试试
E:\temp\autoit3完整,很好,但E:\temp\下还有几个文件夹都是D:\autoit3\中的文件夹,里面有完整东西。。。这样就重复了

kn007 发表于 2009-9-29 23:23:50

第54行的DirCreate($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file) 问题。。。注释掉,不知有问题不?希望大大指示

kn007 发表于 2009-9-29 23:28:22

最终方案,把54的DirCreate($destination & StringTrimLeft($current, StringLen($source)) & "\" & $file) 和50的FileCopy($current & "\" & $file, $destination & StringTrimLeft($current, StringLen($source)) & "\" & $file,$overwrite) 注释掉
把49替换为
                        $DesDIR=$destination & StringTrimLeft ($current ,3) & "\" & $file
                        FileCopy($current & "\" & $file, $DesDIR,$overwrite)

并且$overwrite,在前面判断为8或者9

kn007 发表于 2009-9-29 23:30:49

If 顽固不化 大大 = 无意见 Or 其他网友 = 无意见 Then 2# = 最佳答案

afan 发表于 2009-9-30 01:35:38

发现一个不算问题的问题,如果源文件夹目录比较深,那么目标路径也会添加相同的目录结构,比如:
源文件夹 D:\111\222\333\444 目标文件夹 E:\a
如上代码的结果就会是 E:\a\111\222\333\444 ,而比较合理的目录结构应为 E:\a\444

kn007 发表于 2009-9-30 08:35:39

测试成功

If afan 大大 = 无意见 Or 其他网友 = 无意见 Then 8# = 最佳答案

顽固不化 发表于 2009-9-30 08:37:10

afan 金牌的正则运用的真灵活,佩服

kn007 发表于 2009-9-30 11:10:36

看来大家无异议了,结贴!

pusofalse 发表于 2009-9-30 11:31:50

Re 9#:
表达式有逻辑上的错误,“afan 大大”和“其他网友”是人,而“无意见”表示诸位坛友的态度,“人”不等于“态度”,所以表达式永远不成立。
两者只要有一者同意,那么2#就是最佳答案,如果其中有一方觉得并非如此,那么这一方将被否决,不管这一方是出有最佳答案的“afan 大大”,还是“其他网友”。总觉得还缺少其他必要的判断。^_^

afan 发表于 2009-9-30 11:58:23

12# pusofalse


老师真是严谨治学啊,哈
If xxxRead(afan) = 1 And xxxRead(其他网友) = 1 Then 2# = ok

pusofalse 发表于 2009-9-30 13:37:56

Re 13#:
见笑了。

kn007 发表于 2009-9-30 14:35:47

额,大家看明白了就行了吧。。。

以后不犯了
页: [1]
查看完整版本: 求助,关于改UDF,我怎么改都老失败,做人也太失败了我觉得,找个人帮帮忙!