有谁知道如何通过 Windows 上的命令提示符安装字体文件(.ttf、.TTF、.otf、.OTF 等)?
据我了解,它需要将文本文件移动到正确的文件夹,然后还创建一个我认为的注册表值?但我还没有找到一个确认工作的。
注意:我使用的是 Windows 8,因此可能会有所作为。
另一个注意事项:我想做的是批量安装我从 MKV 文件中提取的字体。(所以这将是一个较大的 .bat 文件的一部分的函数,如果需要,我可以发布代码)
有谁知道如何通过 Windows 上的命令提示符安装字体文件(.ttf、.TTF、.otf、.OTF 等)?
据我了解,它需要将文本文件移动到正确的文件夹,然后还创建一个我认为的注册表值?但我还没有找到一个确认工作的。
注意:我使用的是 Windows 8,因此可能会有所作为。
另一个注意事项:我想做的是批量安装我从 MKV 文件中提取的字体。(所以这将是一个较大的 .bat 文件的一部分的函数,如果需要,我可以发布代码)
您需要使用 PowerShell 或 VB 脚本。他们基本上重复使用在 Windows 资源管理器中执行相同操作的 shell 组件,并且不需要重新启动。
有关从 Windows 8.1 或更早版本的目录安装所有字体的 PowerShell 脚本,请参见此处: https ://social.technet.microsoft.com/Forums/fr-FR/winserverpowershell/thread/fcc98ba5-6ce4-466b-a927-bb2cc3851b59
这是 Windows 10 (Windows Server 2019) 的类似脚本,它也更新了 Windows 注册表: https ://social.technet.microsoft.com/Forums/en-US/0c94dcf5-b89d-42e5-a499-06313f46f88b/can-不再安装-fonts-via-script-in-windows-10-1809?forum=win10itprogeneral
此外,您需要在管理员模式下运行脚本。因此,如果 PowerShell 脚本是 InstallFonts.ps1,您的批处理文件需要如下所示:
powershell -command "Set-ExecutionPolicy Unrestricted" 2>> err.out
powershell .\InstallFonts.ps1 2>> err.out
任何 powershell 错误都将出现在与脚本相同的文件夹中的“err.out”中。
也许这也是需要的:
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "FontName (TrueType)" /t REG_SZ /d FontName.ttf /f
当您安装字体时,它所做的只是将 .ttf 文件复制到%systemroot%\fonts
并在HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts
. 这可以使用批处理文件自动执行,如下所示
Rem fontinst.bat
copy akbar.ttf %systemroot%\fonts
regedit /s font.reg
font.reg 将包含以下内容:
REGEDIT4
\[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts\]
"Akbar Plain (TrueType)"="akbar.ttf"
资料来源:m.windowsitpro.com
您是否尝试将它们复制到字体的文件夹中?
copy font.ttf %windir%\Fonts
如果你是 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
在根文件夹上运行这个 python 脚本
于是我和一位同事找到了一个不需要管理员权限,并且不显示任何提示的 powershell 解决方案。您可以使用字体文件的名称来安装和卸载。这使得它对脚本特别有用。
安装:
# Install-Font.ps1
param($file)
$signature = @'
[DllImport("gdi32.dll")]
public static extern int AddFontResource(string lpszFilename);
'@
$type = Add-Type -MemberDefinition $signature `
-Name FontUtils -Namespace AddFontResource `
-Using System.Text -PassThru
$type::AddFontResource($file)
卸载:
# Uninstall-Font.ps1
param($file)
$signature = @'
[DllImport("gdi32.dll")]
public static extern bool RemoveFontResource(string lpszFilename);
'@
$type = Add-Type -MemberDefinition $signature `
-Name FontUtils -Namespace RemoveFontResource `
-Using System.Text -PassThru
$type::RemoveFontResource($file)
您可以从 cmd 或 powershell 中像这样使用它们:
> powershell -executionpolicy bypass -File .\Install-Font.ps1 .\myfonts\playfair-display-v22-latin-regular.ttf
> powershell -executionpolicy bypass -File .\Uninstall-Font.ps1 .\myfonts\playfair-display-v22-latin-regular.ttf
该解决方案基于https://www.leeholmes.com/powershell-pinvoke-walkthrough/并使用本机 Win32 函数 ( gdi32.dll
)。https://docs.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-addfontresourcew
批处理文件示例。它在当前目录中工作。
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
我以这种方式解决了任务:
假设您必须递归地在具有以下结构的子文件夹中安装许多字体:
\root_folder
Install_fonts.cmd
\font_folder_1
font_1.ttf
font_2.otf
\font_folder_2
font_3.ttf
font_4.otf
\font_folder_3
font_5.ttf
font_6.otf
为此,我在我的桌面上下载了FontReg.exe工具(如果它位于其他位置,则更改文件中的)并在如下的批处理脚本中使用它,位于(在文件中更改其名称,如果不同):path
Install_fonts.cmd
Install_fonts.cmd
root_folder
Install_fonts.cmd
@echo off
set back=%cd%
for /d %%i in (%USERPROFILE%\Desktop\root_folder\*) do (
cd "%%i"
echo current directory:
cd
start /wait %USERPROFILE%\Desktop\fontreg-2.1.3-redist\bin.x86-64\FontReg.exe /move
timeout /t 1 /nobreak >nul
)
cd %back%
echo Process completed!
pause
因此,您必须以管理员身份运行Install_fonts.cmd
,以root_folder
自动化字体安装过程。
干杯