vbscript查找所有具有特定文件名的最近文件 [英] vbscript to find the recent file with specific file name among all
问题描述
新文本文档 - 复制(1)
新文本文档 - 复制(2)
新文本文档 - 复制(3)
新文本文档 - 复制(4)
新文本文档 - 复制(5)
除此之外,我还有一些类似于
Test1.pdf
test2.pdf
Test3.pdf
$ c
$ b 我的要求是找到文件名为'New Text Document'的最新文件。
'需要一个FSO来访问文件夹
Dim oFS:Set oFS = CreateObject(Scripting.FileSystemObject)
'保存找到的文件(如果有的话)
Dim oFiFnd:Set oFiFnd = Nothing
'最小可能的数字
Dim nMax:nMax = 0
'定义要考虑的文件
Dim reFiNa:Set reFiNa = New RegExp
reFiNa.Pattern =^ New Text文档 - 复制\((\ d +)\)$
Dim oFile,oMTS
'查看文件夹
中的所有文件For Each oFile In oFS.GetFolder(.. \testdata\17405017)。文件
设置oMTS = reFiNa.Execute(oFile.Name)
如果1 = oMTS.Count然后
'文件确认模式
If nMax< CLng(oMTS(0).SubMatches(0))然后
'最大nMax迄今为止看到
nMax = CLng(oMTS(0).SubMatches(0))
Set oFiFnd = oFile
结束如果
结束如果
下一个
如果oFiFnd没有那么
'搜索失败
WScript.Echo没有找到文件。
else
$ success
WScript.Echofound,oFiFnd.Path
End If
更新wrt评论:
如果RegExp没有找到任何文件,如文件夹中的新文本文档 - 复制(1)。你可以试试@ Ansgar不太严格的过滤器 - 只要看文件名的前17个字符 - 或者修改.Pattern - 例如如果你忘记了.doc的扩展名,那么新文本文件拷贝((\ d +))。doc $。
看@ Ansgar的贡献可能也有帮助你澄清你的规格:最新是指最后修改文件或最高副本(#)?
I do have only the below files.
New Text Document - Copy (1)
New Text Document - Copy (2)
New Text Document - Copy (3)
New Text Document - Copy (4)
New Text Document - Copy (5)
Apart from this I have something like
Test1.pdf
test2.pdf
Test3.pdf
My requirement is to find the latest file with file name 'New Text Document'
解决方案 To get you started:
' need a FSO for folder access
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
' hold the file found (if any)
Dim oFiFnd : Set oFiFnd = Nothing
' smallest possible number
Dim nMax : nMax = 0
' define which files to consider
Dim reFiNa : Set reFiNa = New RegExp
reFiNa.Pattern = "^New Text Document - Copy \((\d+)\)$"
Dim oFile, oMTS
' look at all files in folder
For Each oFile In oFS.GetFolder("..\testdata\17405017").Files
Set oMTS = reFiNa.Execute(oFile.Name)
If 1 = oMTS.Count Then
' file confirms to pattern
If nMax < CLng(oMTS(0).SubMatches(0)) Then
' largest nMax seen so far
nMax = CLng(oMTS(0).SubMatches(0))
Set oFiFnd = oFile
End If
End If
Next
If oFiFnd Is Nothing Then
' search failed
WScript.Echo "No file found."
Else
' success
WScript.Echo "found", oFiFnd.Path
End If
Update wrt comment:
If the RegExp does not find any files, then there are no files like "New Text Document - Copy (1)" in the folder. You could try @Ansgar's less strict filter - just look at the first 17 characters of the file name - or tinker with the .Pattern - e.g. ""^New Text Document - Copy ((\d+)).doc$" if you forgot the .doc extension.
Looking at @Ansgar's contribution may also help you to clarify your specs: does 'latest' mean 'last file modified' or 'highest Copy (#)'?
这篇关于vbscript查找所有具有特定文件名的最近文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文