Jump to content

How to install a font via the command line?


Th3_uN1Qu3

Recommended Posts

Yes, i've searched. But none of the solutions worked.

I have an application that requires a font installed. A single font, with only one version, and i need that font installed via the command line, without having to reboot the system. I have tried restarting the explorer.exe process, when i do that the font does appear in Explorer but is still unavailable in my application. Any clues on how to fix this? Fontinst.exe does not work either, as it is an OTF font.

I find it stupid that you can install a font without rebooting by simply dropping it into the fonts folder, but you can't mimic this behaviour with a batch file. :blink: I guess this is one of the moments i'd be much happier running Linux.

Link to comment
Share on other sites


If you copy it then do reg add you could probably achieve this

Example:

reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "FontName (TrueType)" /t REG_SZ /d FontName.ttf /f

Edited by ricktendo64
Link to comment
Share on other sites

If you copy it then do reg add you could probably achieve this

Example:

reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "FontName (TrueType)" /t REG_SZ /d FontName.ttf /f

I already did that, not like that but by importing a reg file. It doesn't work, the font doesn't even show up in the Fonts folder (although it is there) if i do that. I need to reboot to get it recognized...

Edited by Th3_uN1Qu3
Link to comment
Share on other sites

I looked into this option when trying to write a program that installs a custom desktop settings and screensaver. The screensaver was 3d text, and I installed a font from a network share. Yes indeed the problem is that you ultimately need to reload explorer so it recognizes the new font. Otherwise it just show up as your system default (ie Arial) font until you reboot. I struggled with this process for a long time before I just made my program reboot at the end.

I looked into making Explorer refresh itself, which is the process that happens after you load a new color scheme, or switch from XP to classic. When that "please wait" box shows up and your desktop turns grayscale, also this happens when you logoff. Here are the things I wrote down in my notes. I don't know exactly what they mean now. I think I was trying to call the system command that triggered the "Theme Change" so it would re-read the font.

SetSysColors() Function

WMSysColorChange() Function

WM_THEMECHANGED

PostMessage() Function

SendMessage() HWND_BROADCAST

SendMessageA ( HWND_BROADCAST, WM_SYSCOLORCHANGE, 0, 0 )

RedrawWindow ( GetDesktopWindow(), NULL, 0, RDW_Invalidate | RDW_Erase | RDW_UPDATENOW | RDW_ALLCHILDREN );

Anyways I was doing this through AUTOit but like I said I couldn't figure it out. So unless you can get Windows to recognize a theme change and redraw the desktop, you'll need to reboot. Not even restarting explorer.exe (the existing process) allowed the change to occur.

Or perhaps there is another way to automate the installation of a font.

Link to comment
Share on other sites

Try this script make sure the font is in the same folder as this script.

Save as AddFont.vbs

Const FONTS = &H14&
'/-> Path And Name To Font
Dim Font :Font = "PATH_FONT_NAME_HERE"
'/-> Objects
Dim Folder, Fso, Shell
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Shell = CreateObject("Shell.Application")
Set Folder = Shell.Namespace(FONTS)
'/-> Check For File Exists
If Fso.FileExists(Font) Then
Folder.CopyHere Font
Else
MsgBox "Missing " & Font,4128,"Missing Font"
End If

Link to comment
Share on other sites

That script did nothing... It didn't even copy the file to the fonts dir.

Edit: This has got to be THE STUPIDEST THING EVER. I just did it with FileCopy in AutoIt and it installs no problem, and my application uses it. No flashing windows, nothing, nice and clean. This proves that XCOPY is different from the actual GUI copy command...

Edited by Th3_uN1Qu3
Link to comment
Share on other sites

How about reading the whole post? I got it done. :P Anyway, the app i am working on is also stupidly weird. If i run it via the shortcut after installing the font, the font doesn't work. If i run it via the autoplay, it works...

We can consider this solved though. The autoplay and the setup program are the same exe, therefore the autoplay must be run to install the app, so the font will work too.

Link to comment
Share on other sites

The reason AutoIT worked is because copying the file over via the shell into the fonts folder automatically tells explorer to call the APIs to install the font (AddFontResourceEx and CreateScalableFontResource to install the font, and SendMessage with the WM_FONTCHANGE message to all top-level windows on the system), whereas using a manual copy outside of the shell doesn't invoke the APIs and thus a reboot / reload of explorer is required.

If you aren't the code writing sort, and you need it to work without a reboot, AutoIT is really your only option.

Link to comment
Share on other sites

The reason AutoIT worked is because copying the file over via the shell into the fonts folder automatically tells explorer to call the APIs to install the font (AddFontResourceEx and CreateScalableFontResource to install the font, and SendMessage with the WM_FONTCHANGE message to all top-level windows on the system), whereas using a manual copy outside of the shell doesn't invoke the APIs and thus a reboot / reload of explorer is required.

If you aren't the code writing sort, and you need it to work without a reboot, AutoIT is really your only option.

Thanks for clearing that up. I am a bit of a coder but if i can get it done easier then why not. :)

Link to comment
Share on other sites

  • 1 month later...

I thought this was over with but it seems it's not.

I tested it 3 times on fresh installs of XP and Vista under Virtual PC. Everything worked fine. I left that project alone for some time since i had other stuff to do and things were getting more complicated, and now i've returned to it.

Tried it on my dual-PIII running XP. The font is copied, if i try to copy it manually it says it's already installed, BUT IT DIDN'T WORK EVEN AFTER A REBOOT. If i delete the font manually then copy it again, or use the installer, or whatever, it works.

W-T-F. Since it works now i can't recreate the issue either...

Link to comment
Share on other sites

  • 1 year later...

I know this is an old thread, but I have a new twist to this issue.

I do not have write access to %SystemRoot%\Fonts on my WinXP system, nor do I have the rights to alter HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts. I can still install a font using the Fonts Control Panel applet, however, and this installation is valid only for the current login session.

What I'm looking for is a way via the command line to install a font for the current login session, and I don't want to have to make use of any manual interaction via a GUI. This way, I can wrap this in a program or script that automatically runs on login.

In other words, I'm looking for a command-line-specific way to install a font in the same way that the Fonts applet will do it if Copy fonts to Fonts folder is unchecked.

I'm willing to write a program in C, C++, C#, VB, or any other language, if that's is the only way to accomplish this.

Any ideas as to how I can do this?

Thanks in advance.

Edited by HippoMan
Link to comment
Share on other sites

  • 2 years later...

Here is a batch script to install Fonts in any Folder automatically.

Paste the following code in notepad and save it as "ADD_Fonts.cmd"

place the cmd file inside the folder of the fonts you want to install and run it or

add the source folder as a parameter. for example "ADD_fonts.cmd" C:\Folder 1\Folder 2\


@ECHO OFF
TITLE Adding Fonts..
REM Filename: ADD_Fonts.cmd
REM Script to ADD TrueType and OpenType Fonts for Windows
REM By Islam Adel
REM 2012-01-16

REM How to use:
REM Place the batch file inside the folder of the font files OR:
REM Optional Add source folder as parameter with ending backslash and dont use quotes, spaces are allowed
REM example "ADD_fonts.cmd" C:\Folder 1\Folder 2\

IF NOT "%*"=="" SET SRC=%*
ECHO.
ECHO Adding Fonts..
ECHO.
FOR /F %%i in ('dir /b "%SRC%*.*tf"') DO CALL :FONT %%i
REM OPTIONAL REBOOT
REM shutdown -r -f -t 10 -c "Reboot required for Fonts installation"
ECHO.
ECHO Done!
PAUSE
EXIT

:FONT
ECHO.
REM ECHO FILE=%~f1
SET FFILE=%~n1%~x1
SET FNAME=%~n1
SET FNAME=%FNAME:-= %
IF "%~x1"==".otf" SET FTYPE=(OpenType)
IF "%~x1"==".ttf" SET FTYPE=(TrueType)

ECHO FILE=%FFILE%
ECHO NAME=%FNAME%
ECHO TYPE=%FTYPE%

COPY /Y "%SRC%%~n1%~x1" "%SystemRoot%\Fonts\"
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts" /v "%FNAME% %FTYPE%" /t REG_SZ /d "%FFILE%" /f
GOTO :EOF

ADD_Fonts.cmd

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...