找回密码
 加入
搜索
查看: 1005|回复: 5

以下代码如何只显示有2级目录的目录路径,一级除外,有代码

[复制链接]
发表于 2009-7-13 20:20:12 | 显示全部楼层 |阅读模式
本帖最后由 qddoit 于 2009-7-13 23:25 编辑
#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=i.ico
#AutoIt3Wrapper_outfile=ListAllDir.exe
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****

#Include <Array.au3>
$path = @ScriptDir

$DirList = myFileListToArray($path,0,2)
_ArrayDisplay($DirList) 
 
Func  myFileListToArray($sPath,  $rPath  =  0,  $iFlag  =  0,  $sPathExclude  =  0)
    Local  $asFileList[1]       
    $asFileList  =  myFileListToArrayTemp($asFileList,  $sPath,  $rPath,  $iFlag,  $sPathExclude)
    Return  $asFileList
EndFunc  
 
Func  myFileListToArrayTemp(ByRef  $asFileList,  $sPath,  $rPath  =  0,  $iFlag  =  0,  $sPathExclude  =  0)
    Local  $hSearch,  $sFile
    If  Not  FileExists($sPath)  Then  Return  SetError(1,  1,  "")
    If  Not  ($iFlag  =  0  Or  $iFlag  =  1  Or  $iFlag  =  2)  Then  Return  SetError(3,  3,  "")
    $hSearch  =  FileFindFirstFile($sPath  &  "\*")
    If  $hSearch  =  -1  Then  Return  SetError(4,  4,  "")
    While  1
        $sFile  =  FileFindNextFile($hSearch)
        If  @error  Then
            SetError(0)
            ExitLoop
        EndIf
 
 
        If  $sPathExclude  And  StringLen($sPathExclude)  >  0  Then  $sPathExclude  =  StringSplit($sPathExclude,  ",")
        $bExclude  =  False
        If  IsArray($sPathExclude)  Then
            For  $ii  =  1  To  $sPathExclude[0]  Step  1
                If  StringInStr($sPath  &  ""  &  $sFile,  $sPathExclude[$ii])  Then
                    $bExclude  =  True
                    ExitLoop
                EndIf
            Next
        EndIf
        If  $bExclude  Then  ContinueLoop
 
        Select
            Case  StringInStr(FileGetAttrib($sPath  &  ""  &  $sFile),  "D")  
                Select
                    Case  $iFlag  =  1   
                        myFileListToArrayTemp($asFileList,  $sPath  &  ""  &  $sFile,  $rPath,  $iFlag,  $sPathExclude)
                        ContinueLoop    
                    Case  $iFlag  =  2  Or  $iFlag  =  0    
                        If  $rPath  Then   
                            If  Not  StringRegExp($sPath  &  ""  &  $sFile,  $rPath,  0)  Then    
                                myFileListToArrayTemp($asFileList,  $sPath  &  ""  &  $sFile,  $rPath,  $iFlag,  $sPathExclude)
                                ContinueLoop  
                            Else                               
                                myFileListToArrayTemp($asFileList,  $sPath  &  ""  &  $sFile,  $rPath,  $iFlag,  $sPathExclude)
                            EndIf
                        Else    
                            myFileListToArrayTemp($asFileList,  $sPath  &  ""  &  $sFile,  $rPath,  $iFlag,  $sPathExclude)
                        EndIf
                EndSelect
 
            Case  Not  StringInStr(FileGetAttrib($sPath  &  ""  &  $sFile),  "D")    
                If  $iFlag  =  2  Then  ContinueLoop   
                If  $rPath  And  Not  StringRegExp($sPath  &  ""  &  $sFile,  $rPath,  0)  Then  ContinueLoop
        EndSelect
 
        ReDim  $asFileList[UBound($asFileList)  +  1]
        $asFileList[0]  =  $asFileList[0]  +  1
        ;为了方便查看所有多加了/\
                $asFileList[UBound($asFileList)  -  1]  =  $sPath  &  "\//////"  &  $sFile
 
    WEnd
    FileClose($hSearch)
    Return  $asFileList
EndFunc    
目前此代码直接列出来当前所有目录路径
请教。如何只列出2级以上的目录,只有1级的目录不显示
发表于 2009-7-13 21:06:39 | 显示全部楼层
懒得改代码 写了个判断
[au3]#Region ;**** 参数创建于 ACNWrapper_GUI ****
#AutoIt3Wrapper_icon=i.ico
#AutoIt3Wrapper_outfile=ListAllDir.exe
#EndRegion ;**** 参数创建于 ACNWrapper_GUI ****

#Include <Array.au3>
$path = "h:\"

$DirList = myFileListToArray($path, 0, 2)
_ArrayDisplay($DirList)

Func myFileListToArray($sPath, $rPath = 0, $iFlag = 0, $sPathExclude = 0)
        Local $asFileList[1]
        $asFileList = myFileListToArrayTemp($asFileList, $sPath, $rPath, $iFlag, $sPathExclude)
        Return $asFileList
EndFunc   ;==>myFileListToArray

Func myFileListToArrayTemp(ByRef $asFileList, $sPath, $rPath = 0, $iFlag = 0, $sPathExclude = 0)
        Local $hSearch, $sFile
        If Not FileExists($sPath) Then Return SetError(1, 1, "")
        If Not ($iFlag = 0 Or $iFlag = 1 Or $iFlag = 2) Then Return SetError(3, 3, "")
        $hSearch = FileFindFirstFile($sPath & "\*")
        If $hSearch = -1 Then Return SetError(4, 4, "")
        While 1
                $sFile = FileFindNextFile($hSearch)
                If @error Then
                        SetError(0)
                        ExitLoop
                EndIf


                If $sPathExclude And StringLen($sPathExclude) > 0 Then $sPathExclude = StringSplit($sPathExclude, ",")
                $bExclude = False
                If IsArray($sPathExclude) Then
                        For $ii = 1 To $sPathExclude[0] Step 1
                                If StringInStr($sPath & "\" & $sFile, $sPathExclude[$ii]) Then
                                        $bExclude = True
                                        ExitLoop
                                EndIf
                        Next
                EndIf
                If $bExclude Then ContinueLoop

                Select
                        Case StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D")
                                Select
                                        Case $iFlag = 1
                                                myFileListToArrayTemp($asFileList, $sPath & "\" & $sFile, $rPath, $iFlag, $sPathExclude)
                                                ContinueLoop
                                        Case $iFlag = 2 Or $iFlag = 0
                                                If $rPath Then
                                                        If Not StringRegExp($sPath & "\" & $sFile, $rPath, 0) Then
                                                                myFileListToArrayTemp($asFileList, $sPath & "\" & $sFile, $rPath, $iFlag, $sPathExclude)
                                                                ContinueLoop
                                                        Else
                                                                myFileListToArrayTemp($asFileList, $sPath & "\" & $sFile, $rPath, $iFlag, $sPathExclude)
                                                        EndIf
                                                Else
                                                        myFileListToArrayTemp($asFileList, $sPath & "\" & $sFile, $rPath, $iFlag, $sPathExclude)
                                                EndIf
                                EndSelect

                        Case Not StringInStr(FileGetAttrib($sPath & "\" & $sFile), "D")
                                If $iFlag = 2 Then ContinueLoop
                                If $rPath And Not StringRegExp($sPath & "\" & $sFile, $rPath, 0) Then ContinueLoop
                EndSelect
                If Not StringInStr($sPath, "\", 0, 3) = 0 Then
                        ReDim $asFileList[UBound($asFileList) + 1]
                        $asFileList[0] = $asFileList[0] + 1
                        ;为了方便查看所有多加了/\
                        $asFileList[UBound($asFileList) - 1] = $sPath & "\//////" & $sFile
                EndIf
        WEnd
        FileClose($hSearch)
        Return $asFileList
EndFunc   ;==>myFileListToArrayTemp[/au3]
 楼主| 发表于 2009-7-13 23:24:25 | 显示全部楼层
呵呵~好用哦

超级感谢~
 楼主| 发表于 2009-7-13 23:24:51 | 显示全部楼层
[img][/img]
[code][/code]


棒啊!!!
 楼主| 发表于 2009-7-14 00:19:05 | 显示全部楼层
If Not StringInStr($sPath, "\", 0, 3) = 0 Then

能不能再解释下这句什么意思?
发表于 2009-7-14 11:18:49 | 显示全部楼层
如果路径里只包含3个以下的“\” 则
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-5 07:38 , Processed in 0.071521 second(s), 19 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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