VBS 在网络驱动器上按名称 运行 的一部分打开文件,但不在本地

VBS to open a file by part of name run on the network drive but not local

你好吗?

我有一个如下所示的 vbs,它可以在网络驱动器上的文件上正常工作,但在我的电脑上无法正常工作。

Set WshShell = CreateObject("WScript.Shell")
strCurDir = WshShell.CurrentDirectory

Set objExcel = CreateObject("Excel.Application")

with objExcel
    .AskToUpdateLinks = False
    .DisplayAlerts = False
    .visible = True
end with

Set objWorkbook = objExcel.Workbooks.Open(strCurDir & "_?????.xlsb")

'Save the workbook
objWorkbook.Save

'close the workbook
objWorkbook.Close

with objExcel
    .AskToUpdateLinks = True
    .DisplayAlerts = True
    .visible = True
end with

'exit the excel program
objExcel.Quit

'release objects
Set objExcel = Nothing
Set objWorkbook = Nothing

哪里是“?????”这是一个时间戳。在这种情况下,我知道该文件以“4_”开头,扩展名为“.xlsb”,我的时间戳长度为 5 个字符。很快“?????”我习惯了"escape"这个不断变化的时间戳

注意:我做了测试,去掉了“??????”并放置今天的时间戳,它可以在我的机器上运行。所以出于某种原因,我的电脑无法识别“??????”模式。

谁能帮我找出为什么这个脚本对本地文件不起作用?

谢谢!!!

(Excel)Workbooks.Open 需要文件路径,而不是模式。获得所需内容的最简单方法是获取文件夹中的文件列表并检查每个名称是否符合您的模式,例如:

Set fs = CreateObject("Scripting.FileSystemObject")
Set dir = fs.GetFolder(strCurDir)
Set ff = dir.Files
For Each f in ff
    if left(f.Name,2)="4_" and right(f.Name,5)=".xlsb" then
           Set objWorkbook = objExcel.Workbooks.Open(f.Path) 
           ...
    end if 
Next

或者,您可以使用 regex