|
楼主 |
发表于 2019-10-2 18:24
|
显示全部楼层
获取根目录下所有指定类型文件的随机文件名。
参数说明如下:
1、aPth为初始路径,可为根目录;
2、Lxg为获取文件的指定类型,
可为“ysp”-音视频;“yp”-音频;“sp”-视频;“wb”-文本;“bg”-表格;“tp”-图片等六类。
3、Opt为1和2,。1是短文件名;2为长文件名。
Public Function 随机文件(aPth As String, Lxg As String, Optional Opt As Variant)
Dim arr(), file() As String
Dim i, k, x
sr1 = ".mp3.wma.wav.mid.ogg.ape.acc.mp4.mp5.wkv.avi.wmv.flv.f4v.rm.rmvb.rmv.dat.asf.mov.vob.3gp.ts.swf.tp.ifo.nsv.tta.as3."
sr2 = ".mp3.wma.wav.mid.ogg.ape.acc."
sr3 = ".mp4.mp5.wkv.avi.wmv.flv.f4v.rm.rmvb.rmv.dat.asf.mov.vob.3gp.ts.swf.tp.ifo.nsv.tta.as3."
sr4 = ".txt.doc.docx.docm.pdf.wps."
sr5 = ".xls.xlsx.xlsm."
sr6 = ".TIFF.png.SWF.jpeg.tif"
Select Case Lxg
Case "ysp": sr = sr1
Case "yp": sr = sr2
Case "sp": sr = sr3
Case "wb": sr = sr4
Case "bg": sr = sr5
Case "tp": sr = sr6
End Select
On Error Resume Next
fd = aPth
x = 1: i = 1: k = 1
ReDim file(1 To i)
file(1) = fd
Do Until i > k
f = Dir(file(i), vbDirectory)
Do Until f = ""
If InStr(f, ".") = 0 Then
k = k + 1
ReDim Preserve file(1 To k)
file(k) = file(i) & f & "\"
End If
f = Dir
Loop
i = i + 1
Loop
For i = 1 To k
f = Dir(file(i) & "*.*")
Do Until f = ""
If InStr(sr, Split(f, ".")(UBound(Split(f, ".")))) > 0 Then
n = n + 1
ReDim Preserve arr(1 To n)
Select Case Opt
Case 1: arr(n) = file(i) & f '不带后缀的文件名
Case 2: arr(n) = Split(f, ".")(0) '带路径、带后缀的文件名,即fullname
' arr(n, 2) = IIf(InStr(s2, Split(f, ".")(UBound(Split(f, ".")))) > 0, "音频", "视频")
End Select
End If
x = x + 1
f = Dir
Loop
Next
随机文件 = arr(Int(Rnd * UBound(arr)) + 1)
End Function |
|