Understanding substitution in variables - solved

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Yanta
Posts: 48
Joined: 01 Sep 2019 07:08

Understanding substitution in variables - solved

#1 Post by Yanta » 28 Jul 2020 06:38

I'm trying to install a bunch of fonts.
srcpath=H:\PostInstall. There are 26 ttf files in the folder.
I have some fonts that are already installed and some that aren't.
I've just set it up with echo atm so I can see what's happening.

original code

Code: Select all

for %%a in ("%SrcPath%\Fonts\*.?tf") do call :installfont %%~na  
Goto L1234

:Installfont
if exist "%windir%\fonts\%%a" echo Font %%a already installed.
if not exist "%windir%\fonts\%%a" (
echo Processing font %%a
Echo copy %%a "%windir%\fonts"
set fext=%%a:~-3%
if /i "%fext%"=="ttf" set ftype=(Truetype)
if /i "%fext%"=="otf" set ftype=(Opentype)
echo reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Fonts" /v %%a %ftype% /d %%a /type REG_SZ /d
)
exit /b

:L1234
...Rest of script
The working code

Code: Select all

@Echo Off
set srcpath="H:\PostInstall"
for %%a in ("%SrcPath%\Fonts\*.?tf") do call :installfont %%~nxa
Goto L1234

:Installfont
Set font=%~1
if exist "%windir%\fonts\%font%" echo Font %font% already installed.
if not exist "%windir%\fonts\%font%" (
Echo copy %font% to "%windir%\fonts"
set fext=%font:~-3%
echo Processing font %font% Extension: %fext%
if /i "%fext%"=="ttf" set "ftype=(Truetype)"
if /i "%fext%"=="otf" set "ftype=(Opentype)"
echo reg add "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Fonts" /v "%font% %ftype%" /d %font% /t REG_SZ /f
set fext=
)
exit /b

:L1234
Echo Done
What confused me was that I didn't understand that %%na was missing the extension (I always considered an extension part of a filename), and the value after the subroutine name is an argument, or parameter, which is accessed positionally via %~1.

Post Reply