Get font name in . bat

Asked

Viewed 732 times

2

I’m using the script below to install fonts in Windows, but I wonder if there is a way to get the name of face source. For example, the source file name is BebasNeue.otf, but the name of her face is Bebas Neue (OpenType). How to get the name of face for script to properly register?

The script is typing the Bebasneue font name into the record and I would like it to write the face name in the Bebas Neue case (Opentype).

@setlocal enableextensions enabledelayedexpansion
@echo off

echo instalando fontes....

for /f "delims=" %%f in ('dir /b \\mulinfsv0005\Configs\fonts\') do (
    set variable=%%f
    set variable=!variable:~0,-4!
    echo !variable!
    xcopy \\servidorOrigem\Configs\fonts\%%f c:\Windows\Fonts /y /q
    reg add "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Fonts" /v !variable! /t REG_SZ /d %%f /f
) 
endlocal

echo As fontes foram instaladas!

pause
  • It seems that to do this you can use Powershell.

1 answer

2


A solution, but which does not involve only batch is to use a program to extract the source name directly from the file. This can be done with several languages (C, Python and etc). I don’t know if there is any pure batch form that can solve this.


Example:

You can use the library freetype to load the source and get the name. For this you can use the following code (in C):

#include <stdint.h>

#include <ft2build.h>
#include FT_FREETYPE_H

#include <freetype.h>
#include <ftglyph.h>
#include <ftsnames.h>

int main(int argc, char** argv)
{
    // Verifica se há parâmetros:
    if (argc < 2)
    {
        printf("Nenhum parametro informado.");
        return 0;
    }

    // O arquivo da fonte que se extrairá o nome.
    const char* face_target = argv[1];

    FT_Library  library;

    int error = FT_Init_FreeType(&library);
    if (error)
    {
        // Erro ao carregar biblioteca.
        return -1;
    }

    FT_Face face;
    error = FT_New_Face(library, face_target, 0, &face);

    if (error == FT_Err_Unknown_File_Format)
    {
        // Erro: Provavelmente o arquivo da fonte não é suportado.
        return -1;
    }
    else if (error)
    {
        // Erro: Não foi possível abrir o arquivo ou o arquivo está quebrado.
        return -1;
    }
    else
    {
        // A fonte foi lida com sucesso:

        FT_SfntName name;
        memset(&name, 0, sizeof(FT_SfntName));
        FT_Get_Sfnt_Name(face, 1, &name);

        printf("%.*s", name.string_len, name.string);
    }

    return 0;
}

And generate a program that prints the source name, given her file. Assuming the program is named as "font2name" and is accessible, you can call it Batch like this:

font2name BebasNeue.otf > name.txt
set /p FACE_NAME=< name.txt
echo %FACE_NAME%

With this, FACE_NAME will store the font name the way you want "Bebas Neue".


I’m not sure, but I believe the same can be done with python, using freetype for python. But, I don’t know if, for what you’re doing, it’s possible to replace batch with python.

  • In fact I need a script to install +150 fonts per gpo, I have a Powershell script that installs source by source by automatically logging the fonts.(I haven’t tested it yet in gpo) but, I like your solution, I’ll see if I can find this library for c#.

  • Take a look at https://github.com/Robmaister/SharpFont. It’s an implementation of Freetype for C#, it shouldn’t change much compared to the code I showed you.

  • Um, Interresante, I’ll check it out. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.