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

摘要:
“ASC”指顺序排序,规则为A-Z。

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确实不怎么好用,写了一上午的东西,程序关闭了 修改的东西不会自动保存,白写了,杯具

免责声明:文章转载自《vb 实现查找指定的文件,并按照文件名排序》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇MyBatis映射文件(编写SQL语句;可有可无(无的时候,使用注解编程))常用和不常用端口一览表下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

[MySQL] 常用SQL的优化--18.4

  这里介绍下,Insert、Group By等SQL语句的优化方法: 1、大批量数据插入当load命令导入数据的时候,可以进行适当的设置提高导入速度。 1.1 对于MyISAM表,可以先禁用非唯一索引更新,再导入数据来快速导入大量的数据。   alter table table_name disable keys;   load date infile...

易优CMS:arclist 文档列表

arclist 文档列表(配合arcpagelist标签可实现ajax瀑布流分页)  [基础用法] 名称:arclist 功能:获取系统主从表模型(如:文章、软件、图集、产品等)的一列文档,也称自由列表块标记。 语法: {eyou:arclist typeid='栏目ID' limit='0,10' flag='c' titlelen='30' infol...

PostreSQL取出每组第一条(最高)记录(6种方法 )

Select first row in each GROUP BY group?stackflow上面的一个问题。用窗口函数比较简单,但是那些没有窗口函数的数据库怎么办? id | customer | total ---+----------+------ 1 | Joe | 5 2 | Sally | 3 3 | Joe...

Mybatis检查SQL注入

Mybatis 的 Mapper.xml 语句中 parameterType 向SQL语句传参有两种方式:#{ } 和 ${ }。 使用#{ }是来防止SQL注入,使用${ }是用来动态拼接参数。 如何排查出 1. 检查是否有$号 如果你使用的是ide代码编辑器,那么可以通过全局搜索${ , 快速定位到使用${ }拼接SQL的语句,在去找到外部传入参数的入...

Top应用选项及SQL执行顺序

写在前面的话 我们常常使用top来查询前几条语句,或使用嵌套的top的方式获取分页的数据,那么我们对top及SQL执行的顺序真正了解吗. 具体如下例. 实例 建表toptest,表结构如下: 列 类型 sid int sname nvarchar(50) 建表语句如下: create table toptest ( sid int...

c# 扩展方法 奇思妙用 高级篇 九:OrderBy(string propertyName, bool desc)

下面是 Queryable 类 中最常用的两个排序的扩展方法: 1 2 public static IOrderedQueryable<TSource> OrderBy<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<T...