木头人 发表于 2011-5-13 18:21:45

请教,类似于此功能应该如何写呢?


就是这样,启动时自动搜索硬盘上3层文件夹内的所有GHO文件,并且将搜索结果放在下拉列表中,便于快速选取。
可能较为麻烦,诚请教如何实现,谢谢。

3mile 发表于 2011-5-13 18:35:58

#include <file.au3>
Local $arr, $num, $str
Local $chen
$chen=IniRead("onekey.ini","data","search","2")
If @error Then
      MsgBox(0,"警告","按默认只搜索2层目录")
      $chen=2
EndIf
$Drive = DriveGetDrive("FIXED")
For $i = 1 To $Drive
      _filelist($Drive[$i])
Next
MsgBox(0, 0, $str)

Func _filelist($searchdir)
      $search = FileFindFirstFile($searchdir & "\*.*") ;;;;查指定目录下的文件
      If $search = -1 Then Return -1 ;;;;如果找不到,返回值 -1
      While 1
                $file = FileFindNextFile($search) ;;;查找下一个文件
                If @error Then ;;;如果找不到文件
                        FileClose($search) ;;;则关闭此句柄
                        Return ;;;返回
                ElseIf $file = "." Or $file = ".." Then ;;如果找到的文件名为.或..则ContinueLoop
                        ContinueLoop ;;;在某些版本的AU3里面可以不需要上行和这行。
                ElseIf StringInStr(FileGetAttrib($searchdir & "\" & $file), "D") Then;;如果找到的是一个文件夹,则
                        $arr = StringRegExp($searchdir & "\" & $file, '\\', 3)
                        $num = UBound($arr)
                        If $num < $chen Then
                              _filelist($searchdir & "\" & $file) ;;递归调用filelist函数,并传参数"$searchdir & "\" & $file"
                        EndIf
                EndIf ;;;$file为查找到的文件夹名称,上一行意思就是进入此文件夹继续查找文件.如此循环
                If StringInStr($file, '.gho') Then $str &= $searchdir & "\" & $file & @CRLF;_ArrayAdd($filelist,$searchdir & "\" & $file );MsgBox( 0,0,$searchdir & "\" & $file & @crlf )
      WEnd
EndFunc   ;==>_filelist

netegg 发表于 2011-5-14 04:08:18

回复 2# 3mile
StringInStr($file, '.gho')
光这样是不行的,最简单的,比如*.gho.bk

3mile 发表于 2011-5-14 10:28:13

改成StringRight($file,4)='.gho'

haijie1223 发表于 2011-5-14 10:29:44

谢谢三笑牌代码

netegg 发表于 2011-5-14 10:57:26

回复 5# 3mile


    {:face (301):}三笑太可爱了

annybaby 发表于 2011-5-14 17:32:00

3mile不是"3英里"吗??
smile才是笑吧??

木头人 发表于 2011-5-14 18:24:18

本帖最后由 木头人 于 2011-5-14 18:27 编辑

回复 2# 3mile


    非常 感谢,看是看懂了,但是不知道怎么整合到我的程序的输入框中。我的代码如下Func _SetMYJX()
        Local $noide, $nousb, $no1394, $rb, $hfp, $hfpf1, $arr, $num, $str, $chen
        $MForm1 = GUICreate("还原镜像", 322, 315, -1, -1, BitOR($WS_SYSMENU, $WS_CAPTION, $WS_BORDER), 0, $XForm1)
        $MGroup1 = GUICtrlCreateGroup("镜像", 8, 8, 305, 153)
        $filelj = GUICtrlCreateCombo("请输入或选择镜像文件", 24, 51, 217, 21)
        GUICtrlSetData(-1, $str)
        $MButton1 = GUICtrlCreateButton("打 开", 248, 51, 51, 21)
        GUICtrlCreateLabel("请点击 <打开> 镜像文件:", 24, 32, 200, 17)
        GUICtrlCreateLabel("镜像恢复到分区:", 24, 84, 100, 17)
        $var2 = DriveGetDrive("FIXED")
        If Not @error Then
                For $i = 1 To $var2
                        $hfp3 = $var2[$i] & "|"
                        $hfp = $hfp & $hfp3
                        $hfp1 = $var2
                Next
        EndIf
        $MCombo1 = GUICtrlCreateCombo("", 128, 80, 113, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL, $CBS_UPPERCASE))
        GUICtrlSetData(-1, $hfp, $hfp1)
        $MButton2 = GUICtrlCreateButton("还原", 62, 283, 75, 25)
        $MButton3 = GUICtrlCreateButton("关闭", 190, 283, 75, 25)
        GUISetState(@SW_SHOW)

        While 1
                $nMsg = GUIGetMsg()
                Switch $nMsg
                        Case $GUI_EVENT_CLOSE, $MButton3
                                GUIDelete($MForm1)
                                Return 0
                        Case $MButton1
                                $drivedbs = ""
                                $jxfile = FileOpenDialog("选择镜像文件", "d:\", "(*.gho;*.ghs)|所有文件(*.*)", 1 + 2, "", $XForm1)
                                If Not @error Then
                                        FileDelete(@ScriptDir & "\ghostr.bat")
                                        $PFPD = StringUpper(StringLeft($jxfile, 3))
                                        If DriveGetType($PFPD) <> "Fixed" Then
                                                MsgBox(4096, "错误", "只支持硬盘恢复。")
                                                GUICtrlSetData($filelj, "", "")
                                                ContinueLoop
                                        EndIf
                                        GUICtrlSetData($filelj, $jxfile, "")
                                EndIf
                        Case $MButton2
                                $jxfile = GUICtrlRead($filelj)
                                FileDelete(@ScriptDir & "\ghostr.bat")
                                $PFPD = StringUpper(StringLeft($jxfile, 3))
                                If DriveGetType($PFPD) <> "Fixed" Then
                                        MsgBox(4096, "错误", "目前只支持硬盘恢复。
                                        ContinueLoop
                                EndIf
                                GUICtrlSetData($filelj, $jxfile, "")
                                $hfpf = GUICtrlRead($MCombo1) & "\"
                                $pd1 = 1
                                $pd2 = 1
                                $drivedb = StringSplit(StringUpper($disbk), "|")
                                For $i = 1 To $drivedb
                                        If $pd1 = 1 Then
                                                If $drivedb[$i] = $PFPD Then
                                                        $drivedbs = $drivedb[$i + 1]
                                                        $pd1 = 0
                                                EndIf
                                        EndIf

                                        If $pd2 = 1 Then
                                                If $drivedb[$i] = $hfpf Then
                                                        $hfpf1 = $drivedb[$i + 1]
                                                        $pd2 = 0
                                                EndIf
                                        EndIf
                                        If $pd1 = 0 And $pd2 = 0 Then ExitLoop
                                Next
                                If $drivedbs = $hfpf1 Then
                                        MsgBox(16 + 4096, "错误", "备份文件与恢复盘不能是同一分区。", 0, $MForm1)
                                        ContinueLoop
                                EndIf
                                FileWrite(@ScriptDir & "\ghostr.bat", "ghost -clone,mode=pload,src=" & $drivedbs & StringTrimLeft($jxfile, 2) & ":1,dst=" & $hfpf1 & " -sure")
                                _SetQJ()
                                Shutdown(6)
                EndSwitch
        WEnd
EndFunc   ;==>_SetMYJX请问如何将搜索出的镜像文件列表在
        $filelj = GUICtrlCreateCombo("请输入或选择镜像文件", 24, 51, 217, 21)
        GUICtrlSetData(-1, $str)
这个输入框中呢?没整明白,本人太菜不好意思,呵呵
GUICtrlCreateCombo框是一个组合框,可以使用“打开”按钮直接浏览并打开硬盘上的镜像。
也想实现程序打开后,自动将硬盘上存在的镜像列表在下拉框中,方便直接选取。

木头人 发表于 2011-5-15 01:51:15

为何大大们发的代码都有带解释连接,和颜色,我发的没有呢?

木头人 发表于 2011-5-15 01:51:21

为何大大们发的代码都有带解释连接,和颜色,我发的没有呢?

netegg 发表于 2011-5-15 03:22:07

本帖最后由 netegg 于 2011-5-15 03:43 编辑

回复 9# 木头人
不是很清楚你的目的到底是什么
3mile那个代码明白吧,你把
If StringInStr($file, '.gho') Then $str &= $searchdir & "\" & $file & 改成
If StringInStr($file, '.gho') Then $str &= $searchdir & "\" & $file & '|'
创建comdo的时候,GUICtrlSetData(-1, stringtrimright($str, 1) ,'')

淡定开源 发表于 2011-5-15 08:23:31

在我看起来挺复杂的。

木头人 发表于 2011-5-15 19:12:13

回复 12# netegg

谢谢蛋蛋回复,但是能告诉我怎么整合进我的程序里吗?谢谢

netegg 发表于 2011-5-15 19:59:25

整合什么,3mile不是给你写了个完整的吗

3mile 发表于 2011-5-15 20:48:15

不再使用全局变量,方便函数调用.
但因为有ByRef,所有要事先定义返回的变量,以及搜索的层数(默认搜索2层).
这样可以很方便的植入木头人兄的代码中了.
#include <file.au3>
#include <array.au3>

$abc = _Runsearchdir()
_ArrayDisplay($abc)

Func _Runsearchdir()
        Local $Array;提前定义返回数据的变量.
        Local $chen = 2;搜索的层数
        $Drive = DriveGetDrive("FIXED")
        For $i = 1 To $Drive
                _filelist($Drive[$i], $Array, $chen)
        Next
        Return $Array
EndFunc


Func _filelist($searchdir, ByRef $str, $chen = 2)
        $search = FileFindFirstFile($searchdir & "\*.*") ;;;;查指定目录下的文件
        If $search = -1 Then Return -1 ;;;;如果找不到,返回值 -1
        While 1
                $file = FileFindNextFile($search) ;;;查找下一个文件
                If @error Then ;;;如果找不到文件
                        FileClose($search) ;;;则关闭此句柄
                        Return ;;;返回
                ElseIf $file = "." Or $file = ".." Then ;;如果找到的文件名为.或..则ContinueLoop
                        ContinueLoop ;;;在某些版本的AU3里面可以不需要上行和这行。
                ElseIf StringInStr(FileGetAttrib($searchdir & "\" & $file), "D") Then;;如果找到的是一个文件夹,则
                        $arr = StringRegExp($searchdir & "\" & $file, '\\', 3)
                        $num = UBound($arr)
                        If $num < $chen Then
                                _filelist($searchdir & "\" & $file, $str, $chen) ;;递归调用filelist函数,并传参数"$searchdir & "\" & $file"
                        EndIf
                EndIf ;;;$file为查找到的文件夹名称,上一行意思就是进入此文件夹继续查找文件.如此循环
                If StringRight($file, 4) = '.gho' Then
;~                         $str &= $searchdir & "\" & $file & @CRLF;_ArrayAdd($filelist,$searchdir & "\" & $file );MsgBox( 0,0,$searchdir & "\" & $file & @crlf )
                        ReDim $str
                        $str = $searchdir & "\" & $file & @CRLF
                        $str = UBound($str) - 1
                EndIf
        WEnd
EndFunc
页: [1] 2
查看完整版本: 请教,类似于此功能应该如何写呢?