PixPath 是 jpg 文件夹的完整路径,我使用如下代码来处理每个 jpg。
fileName = Dir(PixPath)
Do Until fileName = ""
If Right$(fileName, 4) = ".jpg" Then
fileName = Dir()
<process this jpg>
End If
Loop
这工作正常,除了文件按字母数字顺序返回,而不是按文件在文件夹中列出的顺序显示。有没有办法解决?
你不能这样做Dir
.
另一种选择是:
- Use
FileSystemObject
访问目录中的所有文件
- 阅读所有
.jpg
文件放入数组X
- Use
Val
来比较.jpg
s 按值按数字升序排序
-
最终的数组X包含排序后的文件
Sub Test()
Dim objFSO As Object
Dim objFolder As Object
Dim objFiles As Object
Dim objFile As Object
Dim X
Dim lngFileCnt As Long
Dim lngCnt As Long
Dim i As Long
Dim j As Long
Dim strBuffer1 As String
Dim strFolder As String
Set objFSO = CreateObject("Scripting.fileSystemObject")
strFolder = "C:\temp"
Set objFolder = objFSO.getFolder(strFolder)
Set objFiles = objFolder.Files
lngFileCnt = objFiles.Count
ReDim X(1 To lngFileCnt)
'grab all jpg files
For Each objFile In objFiles
If Right$(objFile.Name, 3) = "jpg" Then
lngCnt = lngCnt + 1
X(lngCnt) = objFile.Name
End If
Next
'resize array to number of jpg files
ReDim Preserve X(1 To lngCnt)
'sort array by numeric value
For i = 1 To lngCnt
For j = (i + 1) To lngCnt
If Val(X(i)) > Val(X(j)) Then
strBuffer1 = X(j)
X(j) = X(i)
X(i) = strBuffer1
End If
Next
Next
MsgBox Join(X, ";")
End Sub
See here有关使用的更多信息FileSystemObject
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)