繁体   English   中英

Windows:从 cmd/.bat 文件安装字体

[英]Windows: install fonts from cmd/.bat file

有谁知道如何通过 Windows 上的命令提示符安装字体文件(.ttf、.TTF、.otf、.OTF 等)?

据我了解,它需要将文本文件移动到正确的文件夹,然后我认为还需要创建一个注册表值? 但我还没有找到一个可以确认工作的。

注意:我使用的是 Windows 8,因此可能会有所不同。

另一个注意事项:我想要做的是批量安装我从 MKV 文件中提取的字体。 (所以这将是一个更大的 .bat 文件的一部分,如果需要,我可以发布代码)

您需要使用 PowerShell 或 VB 脚本。 它们基本上重复使用在 Windows 资源管理器中执行相同操作的外壳组件,并且不需要重新启动。

有关从 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- no-longer-install-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

批处理文件示例。 它在当前目录中工作。

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

如果你是 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 脚本

我以这种方式解决了任务:

假设您必须以递归方式在具有以下结构的子文件夹中安装许多字体:

\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工具(更改Install_fonts.cmd文件中的path ,如果它位于其他位置),并在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中,以自动执行字体安装过程。

干杯

所以我和一个同事找到了一个不需要管理员权限的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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM