Windows:从 cmd/.bat 文件安装字体
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12946384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows: install fonts from cmd/.bat file
提问by begna112
anyone know how to install font files (.ttf, .TTF, .otf, .OTF, etc etc) through the command prompt on windows?
有谁知道如何通过 Windows 上的命令提示符安装字体文件(.ttf、.TTF、.otf、.OTF 等)?
as i understand it, it requires moving the text file to the correct folder and then also creating a registry value i think? but I havent been able to find one that is confirmed working.
据我了解,它需要将文本文件移动到正确的文件夹,然后我认为还需要创建一个注册表值?但我还没有找到一个可以确认工作的。
a note: I am using windows 8 so that might make a difference.
注意:我使用的是 Windows 8,因此可能会有所不同。
another note: what I am trying to do is batch install fonts that I ripped from MKV files. (so this will be a function that is part of a larger .bat file, i can post the code if needed)
另一个注意事项:我想要做的是批量安装我从 MKV 文件中提取的字体。(所以这将是一个更大的 .bat 文件的一部分,如果需要,我可以发布代码)
回答by Yoshi
You'll need to use a PowerShell or VB script. They basically re-use the shell components that do the same thing in Windows Explorer, and they don't need a reboot.
您需要使用 PowerShell 或 VB 脚本。它们基本上重复使用在 Windows 资源管理器中执行相同操作的外壳组件,并且不需要重新启动。
See here for a PowerShell script that installs all fonts from a directory for Windows 8.1 or earlier: https://social.technet.microsoft.com/Forums/fr-FR/winserverpowershell/thread/fcc98ba5-6ce4-466b-a927-bb2cc3851b59
有关从 Windows 8.1 或更早版本的目录安装所有字体的 PowerShell 脚本,请参见此处:https: //social.technet.microsoft.com/Forums/fr-FR/winserverpowershell/thread/fcc98ba5-6ce4-466b-a927-bb2cc3851b59
Here is a similar script for Windows 10 (Windows Server 2019) that also updates the Windows Registry: https://social.technet.microsoft.com/Forums/en-US/0c94dcf5-b89d-42e5-a499-06313f46f88b/can-no-longer-install-fonts-via-script-in-windows-10-1809?forum=win10itprogeneral
这是 Windows 10 (Windows Server 2019) 的类似脚本,它也更新 Windows 注册表:https: //social.technet.microsoft.com/Forums/en-US/0c94dcf5-b89d-42e5-a499-06313f46f88b/can- no-longer-install-fonts-via-script-in-windows-10-1809?forum=win10itprogeneral
Also, you'll need to run the script in admin mode. So if the PowerShell script is InstallFonts.ps1, your batch file needs to look like:
此外,您需要在管理员模式下运行脚本。因此,如果 PowerShell 脚本是 InstallFonts.ps1,则您的批处理文件需要如下所示:
powershell -command "Set-ExecutionPolicy Unrestricted" 2>> err.out
powershell .\InstallFonts.ps1 2>> err.out
Any powershell errors will appear in 'err.out' on the same folder as the script.
任何 powershell 错误都将出现在与脚本相同的文件夹中的“err.out”中。
回答by T.Todua
maybe this is needed too:
也许这也需要:
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "FontName (TrueType)" /t REG_SZ /d FontName.ttf /f
回答by Rex Jones
When you install a font all it does is copy the .ttf file to %systemroot%\fonts
and add an entry in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
. This can be automated with a batch file as follows
当您安装字体时,它所做的就是将 .ttf 文件复制到 .ttf 文件%systemroot%\fonts
并在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
. 这可以通过批处理文件自动化,如下所示
Rem fontinst.bat
copy akbar.ttf %systemroot%\fonts
regedit /s font.reg
The font.reg would contain the following:
font.reg 将包含以下内容:
REGEDIT4
\[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts\]
"Akbar Plain (TrueType)"="akbar.ttf"
Source: m.windowsitpro.com
来源:m.windowsitpro.com
回答by Bali C
Have you tried copying them to the font's folder?
您是否尝试将它们复制到字体的文件夹中?
copy font.ttf %windir%\Fonts
回答by pota opt
Batch file sample. It works in the current directory.
批处理文件示例。它在当前目录中工作。
IF "%*" NEQ "" SET FONT=%* (
FOR /F %%i in ('dir /b "%FONT%*.*tf"') DO CALL :DEST %%i
) else (
EXIT
)
:DEST
SET FONTFILE=%~n1%~x1
SET FONTNAME=%~n1
IF "%~x1"==".ttf" SET FONTTYPE=TrueType
IF "%~x1"==".otf" SET FONTTYPE=OpenType
ECHO FILE = %FONTFILE%
ECHO NAME = %FONTNAME:-= %
ECHO TYPE = %FONTTYPE%
fontview %~dp0%FONTFILE%
GOTO :EXIT
回答by Binoy Pilakkat
if you are a python fan, following script does the job. This script generates a vbscript for font installation. Searches all the sub-folders for ttf fonts and installs it. You don't need to move any font files.
如果你是 python 粉丝,下面的脚本可以完成这项工作。此脚本生成用于字体安装的 vbscript。在所有子文件夹中搜索 ttf 字体并安装它。您不需要移动任何字体文件。
import os
import subprocess
import time
# vb script template
_TEMPL = """
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("%s")
Set objFolderItem = objFolder.ParseName("%s")
objFolderItem.InvokeVerb("Install")
"""
vbspath = os.path.join(os.getcwd(), 'fontinst.vbs')
for directory, dirnames, filenames in os.walk(os.getcwd()):
for filename in filenames:
fpath = os.path.join(directory, filename)
if fpath[-4:] == ".ttf": # modify this line for including multiple extension
with open(vbspath, 'w') as _f:
_f.write(_TEMPL%(directory, filename))
subprocess.call(['cscript.exe', vbspath])
time.sleep(3) # can omit this
os.remove(vbspath) # clean
Run this python script on the root folder
在根文件夹上运行此 python 脚本