vb 实现查找指定的文件,并按照文件名排序

 

Private Sub Command1_Click()
   Dim NowPath As String
   Dim FileArr() As String
   Dim mFileCount As Long
   Dim i As Long
   Dim OrderBy As String
   Dim orderByConditions As Boolean
  
   Dim DriverObject As Object
   Dim FileObject As Object
   Dim MyObject As Object
   Dim HaveFile As Long
  
   On Error Resume Next
   '初始化基本的数据
   List1.Clear             '清空列表数据
   OrderBy = Combo1.Text   '设置排序方式  "DESC"为反序排序,规则是 Z-A。   "ASC"为顺序排序,规则为 A-Z。
   mFileCount = 0          '初始化文件查询到目录中的总数
   HaveFile = 0            '初始化判断是否有文件的标志为 0,表示没有文件
   NowPath = Text1.Text    '初始化要查询的目录地址
   '格式化目录地址
   If Right(NowPath, 1) <> "\" Then
      NowPath = NowPath & "\"
   End If
'第一种方式:查找指定目录下的所有文件(具体能不能模糊匹配文件不清楚,没找到合适方法)   
 ' 通过FSO查询目录里的文件
'   Set DriverObject = CreateObject("Scripting.FileSystemObject")
'   If DriverObject.FolderExists(NowPath) Then
'      Set FileObject = DriverObject.GetFolder(NowPath).Files
'      mFileCount = FileObject.Count - 1
'      ReDim FileArr(mFileCount, 2)
'      i = 0
'      For Each MyObject In FileObject
'         FileArr(i, 0) = MyObject.Name
'         FileArr(i, 1) = MyObject.DateLastModified
'         FileName = MyObject.Name
'         FileArr(i, 2) = NowPath & FileName
'         i = i + 1
'      Next
'      Set FileObject = Nothing
'      If Int(i) >= 1 Then
'         HaveFile = 1
'      End If
'   End If

第二种方式:用Dir()方法(不怎么好用,但能实现)
    Static lngFiles As Long
    Dim sDir As String
    Dim sPath As String
    Dim pFileName As String
    'Dim FileArr() As String '存放子目录名称
    DoEvents
    pFileName = "001*POSMST01"   ’匹配的文件名
    sPath = "D:\cvsschome\ap\bin\"  '写死了文件的路径
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
        sDir = Dir(sPath & pFileName)          '调用dir方法(第一次必须指定完整路径)
        Do While Len(sDir)
            ReDim Preserve FileArr(lngFiles)  '动态改变数组维度
            FileArr(lngFiles) = sDir                  '数组赋值
            lngFiles = lngFiles + 1
            List1.AddItem sPath & sDir
            sDir = Dir                                       '循环调用dir方法 可以不带参数
        Loop
        If Int(lngFiles) >= 1 Then
           HaveFile = 1
        End If
   '对数组信息进行重新排序
   If Int(HaveFile) > 0 Then
      For One = 0 To UBound(FileArr)
         For Two = 0 To UBound(FileArr)
            If UCase(OrderBy) = "DESC" Then
               If StrComp(FileArr(One), FileArr(Two), vbTextCompare) = 1 Then
                  orderByConditions = True
               Else
                  orderByConditions = False
               End If
            ElseIf UCase(OrderBy) = "ASC" Then
               If StrComp(FileArr(One), FileArr(Two), vbTextCompare) = -1 Then
                  orderByConditions = True
               Else
                  orderByConditions = False
               End If
            End If
            If orderByConditions Then
               TempName = FileArr(Two)
               FileArr(Two) = FileArr(One)
               FileArr(One) = TempName
            End If
         Next
      Next
      For i = 0 To UBound(FileArr)
         List1.AddItem FileArr(i)
      Next
   End If
End Sub

不管怎么样 功能是实现了,不过vb确实不怎么好用,写了一上午的东西,程序关闭了 修改的东西不会自动保存,白写了,杯具

posted @ 2011-10-26 13:26  aspc  阅读(2168)  评论(0编辑  收藏  举报