File vbscript在所有文件中查找具有特定文件名的最近文件

File vbscript在所有文件中查找具有特定文件名的最近文件,file,vbscript,File,Vbscript,我只有以下文件 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) 除此之外,我还有 Test1.pdf test2.pdf Test3.pdf 我的要求是找到文件名为“New Text Document”的最新文件,以便开始: ' need

我只有以下文件

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)
除此之外,我还有

Test1.pdf

test2.pdf

Test3.pdf
我的要求是找到文件名为“New Text Document”的最新文件,以便开始:

  ' 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
”需要FSO才能访问文件夹
Dim oFS:Set oFS=CreateObject(“Scripting.FileSystemObject”)
'保留找到的文件(如果有)
FIND的尺寸:FIND的集合=无
“尽可能小的数目
尺寸nMax:nMax=0
定义要考虑哪些文件
Dim reFiNa:设置reFiNa=新的RegExp
reFiNa.Pattern=“^New Text Document-Copy\(\d+)$”
我的天哪
'查看文件夹中的所有文件
对于oFS.GetFolder(“..\testdata\17405017”).文件中的每个文件
设置oMTS=reFiNa.Execute(oFile.Name)
如果1=oMTS.Count,则
'文件符合模式
如果nMax
更新wrt注释:

如果RegExp找不到任何文件,则文件夹中不存在类似“New Text Document-Copy(1)”的文件。您可以尝试使用@Ansgar不太严格的过滤器-只需查看文件名的前17个字符-或者修改.Pattern-例如“^New Text Document-Copy((\d+)).doc$”,如果您忘记了.doc扩展名


查看@Ansgar的贡献也可以帮助您澄清您的规格:“最新”是指“最后修改的文件”还是“最高副本(#)”?

列举如下文件夹中的文件:

Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\some\where").Files
  WScript.Echo f.Name
Next
Set mostRecent = Nothing
...
If mostRecent Is Nothing Then
  Set mostRecent = f
ElseIf f.DateLastModified > mostRecent.DateLastModified Then
  Set mostRecent = f
End If
如下所示检查文件名(使用
LCase()
使检查不区分大小写):

请记住最近修改的文件,如下所示:

Set fso = CreateObject("Scripting.FileSystemObject")
For Each f In fso.GetFolder("C:\some\where").Files
  WScript.Echo f.Name
Next
Set mostRecent = Nothing
...
If mostRecent Is Nothing Then
  Set mostRecent = f
ElseIf f.DateLastModified > mostRecent.DateLastModified Then
  Set mostRecent = f
End If

对于@Mgetz No,这可能是一个更好的问题。OP希望使用.wsh脚本或.asp页面来执行此操作。@reporter请阅读相关主题列表,我无法复制示例代码:(我几乎完成了。显示一些错误///然后显示您的代码和错误。非常感谢,我已修改了位置并尝试运行。它显示未找到任何文件。reFiNa.Pattern=“^New Text Document((\d+))$”是的,最新的表示最后修改的文件。我已将正则表达式编辑为reFiNa.Pattern=“^New Text Document^“仍然显示未找到文件…:(一直在拉我所有的头发…@Priyabrata-在.Pattern中的前导“^”表示“在要匹配的字符串的开头”,因此从模式中删除尾随“^”。对于“latest”的定义,Ansgar的方法比我的好。