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

[AU3基础] 如何获取磁盘目录不调用Dir命令[ 已解决]

[复制链接]
发表于 2010-9-22 12:05:45 | 显示全部楼层 |阅读模式
本帖最后由 黑色袜子 于 2010-9-24 20:40 编辑

RT
如何用au3实现获取目录并分别赋予变量$a,$b....(或者其他形式的)

不借助Dos能不能获取磁盘的盘符呢????

新手问这样的问题觉得很小白,但是没办法!!!

汗,打错字给扣了5大洋,杯具!!!

评分

参与人数 1金钱 -5 收起 理由
afan -5 以解决?

查看全部评分

发表于 2010-9-22 17:05:11 | 显示全部楼层
DriveGetDrive
 FileFindFirstFile
 FileFindNextFile
发表于 2010-9-22 19:48:32 | 显示全部楼层
有这方面的UDF,多搜下论坛能解决大部分问题
http://www.autoitx.com/thread-43-1-2.html


;~  example:
;~  $sPath  =  "D:\AutoIt\article\autoitv3.0"
;~  $aFile  =  myFileListToArray($sPath,  "\.au3$",  1,  "zt_,_private,BackUp,images")
;~  If  IsArray($aFile)  Then  
;~      For  $i  =  1  To  $aFile[0]    Step  1
;~          ConsoleWrite($i  &  "="  &  $aFile[$i]  &  @CRLF)
;~      Next
;~  EndIf
;===============================================================================
;
;  Description:        lists  files  or  directory(subdirectory)  in  a  specified  path  (Similar  to  using  Dir  with  the  /b  /s  Switch)
;                    列出目录下所有文件或文件夹,包括子文件夹.(yidabu.com注:autoit官方版本_FileListToArray不包括搜索子文件夹,也不支持正则表达式)
;  Syntax:                myFileListToArray($sPath,$rPath=0,  $iFlag  =  0,$sPathExclude=0)
;  Parameter(s):          $sPath  =  Path  to  generate  filelist  for  要搜索的路径
;                          $iFlag  =  determines  weather  to  return  file  or  folders  or  both
;                    $rPath  =  The  regular  expression  to  compare.  StringRegExp  Function  Reference  For  details
;                    用于搜索的正则表达式,用法同StringRegExp函数
;                        $iFlag=0(Default)  Return  both  files  and  folders  返回所有文件和文件夹
;                                $iFlag=1  Return  files  Only  仅返回文件
;                        $iFlag=2  Return  Folders  Only  仅返回文件夹
;                    $sPathExclude  =  when  word  in  path,  the  file/folder  will  exclude,mulite  words  separate  by  comma(,)
;                    在路径中要要排除的词语,多个有,分隔
;
;  Requirement(s):    autoit  v3.2.2.0
;  Return  Value(s):    On  Success  -  Returns  an  array  containing  the  list  of  files  and  folders  in  the  specified  path
;                                On  Failure  -  Returns  the  an  empty  string  ""  if  no  files  are  found  and  sets  @Error  on  errors
;                        @Error=1  Path  not  found  or  invalid
;                                @Error=3  Invalid  $iFlag
;                              @Error=4  No  File(s)  Found
;
;  Author(s):          by  http://www.yidabu.com  一大步成功社区    http://bbs.yidabu.com/forum-2-1.html
;  update:            20070125                                        
;  Note(s):            The  array  returned  is  one-dimensional  and  is  made  up  as  follows:
;                    $array[0]  =  Number  of  Files\Folders  returned  返回文件/文件夹的数量
;                    $array[1]  =  1st  File\Folder  (not  include  $sPath)  第一个文件/文件夹
;                    $array[2]  =  2nd  File\Folder  (not  include  $sPath)
;                    $array[3]  =  3rd  File\Folder  (not  include  $sPath)
;                    $array[n]  =  nth  File\Folder  (not  include  $sPath)
;                          Special  Thanks  to  SolidSnake  <MetalGX91  at  GMail  dot  com>  (his  _FileListToArray)
;===============================================================================



Func  myFileListToArray($sPath,  $rPath  =  0,  $iFlag  =  0,  $sPathExclude  =  0)
    Local  $asFileList[1]        ;yidabu.com提示:因为要用递归调用,$asFileList参数要单独出来
    $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
        
        ;yidabu.com提示:已经被排除的路径,就不要搜索子目录了
        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    ;1如果要求对路径进行正则匹配
                            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    ;2如果不要求对路径进行正则匹配,递归,并把本目录加入匹配成功,
                            myFileListToArrayTemp($asFileList,  $sPath  &  "\"  &  $sFile,  $rPath,  $iFlag,  $sPathExclude)
                        EndIf
                EndSelect
                
            Case  Not  StringInStr(FileGetAttrib($sPath  &  "\"  &  $sFile),  "D")    ;如果遇到文件
                If  $iFlag  =  2  Then  ContinueLoop    ;求目录时就跳过
                ;yidabu.com提示:要求正则匹配路径,且匹配失败时就跳过。遇文件就不要递归调用了。
                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    ;==>myFileListToArrayTemp
 楼主| 发表于 2010-9-22 20:39:47 | 显示全部楼层
回复 2# thesnow 这个基本解决,但是我不知道获取目录的那个怎样赋予变量能否给点范文!!!!
您需要登录后才可以回帖 登录 | 加入

本版积分规则

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

GMT+8, 2024-5-18 12:02 , Processed in 0.088660 second(s), 28 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

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