![由于异常“所需对象:‘objFolderItem’”,无法使用 VBScript 安装字体](https://linux22.com/image/1430142/%E7%94%B1%E4%BA%8E%E5%BC%82%E5%B8%B8%E2%80%9C%E6%89%80%E9%9C%80%E5%AF%B9%E8%B1%A1%EF%BC%9A%E2%80%98objFolderItem%E2%80%99%E2%80%9D%EF%BC%8C%E6%97%A0%E6%B3%95%E4%BD%BF%E7%94%A8%20VBScript%20%E5%AE%89%E8%A3%85%E5%AD%97%E4%BD%93.png)
这是我的脚本 如何从 Windows 命令提示符安装字体?
Dim WinFontDir
Dim SrcFontDir
WinFontDir = "C:\Windows\Fonts"
SrcFontDir = "..\fonts.test"
Set objShell = CreateObject("Shell.Application")
Set objFontFolder = objShell.Namespace(WinFontDir)
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSrc = objFSO.GetFolder(SrcFontDir)
Set colFiles = objSrc.Files
For each objFile in colFiles
If objFSO.FileExists(WInFontDir + "\" + objFile.Name) Then
WScript.Echo objFile.Name
else
WScript.Echo "Copying " + objFile.Name
objFSO.CopyFile SrcFontDir + "\" + objFile.Name, WinFontDir + "\" + objFile.Name
Set objFolderItem = objFontFolder.ParseName(objFile.Name)
objFolderItem.InvokeVerb("Install") // <- exception
End If
Next
基本上,我希望能够遍历字体文件夹,并且仅当字体文件不存在于“C:\Windows\Fonts”中时才复制和安装。
但是当谈到这一行时objFolderItem.InvokeVerb("Install")
我收到了这个错误信息:
所需对象:'objFolderItem'
原因何在?
答案1
最后,只有这种方法对我有用:
Dim WinFontDir
Dim SrcFontDir
WinFontDir = "C:\Windows\Fonts"
SrcFontDir = "..\fonts"
Set objShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objSrc = objFSO.GetFolder(SrcFontDir)
Set colFiles = objSrc.Files
For each objFile in colFiles
If Not objFSO.FileExists(WInFontDir + "\" + objFile.Name) Then
WScript.Echo "Copying " + objFile.Name
FONTS = &H14&
Set objFontFolder = objShell.Namespace(FONTS)
objFontFolder.CopyHere objFile.Path
End
Next
注意:仅复制字体文件夹中不存在的文件