Advertisement
alexpanoiu

FontList.cmd

May 29th, 2019
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.78 KB | None | 0 0
  1.      @echo off
  2.  
  3. ::  This script generates a list of installed fonts and writes it to a file with
  4. ::  a name derived from the current date and time and a user-supplied tag. The
  5. ::  script counts the fonts, and displays the resulting number. The script
  6. ::  compares the generated list with the most recent older list; if no
  7. ::  differences are encountered, or if the user did not supply a tag, the
  8. ::  generated file is deleted.
  9.  
  10.     setlocal enableextensions disabledelayedexpansion
  11.  
  12.     set "BASENAME=FontList"
  13.     set "PATTERN=%BASENAME% ??????-???? *.txt"
  14.     set "KEYNAME=HKLM\Software\Microsoft\Windows NT\CurrentVersion\Fonts"
  15.  
  16.     set "TIMETAG="
  17.     for /f "usebackq delims=" %%t in (
  18.       `wmic os get localdatetime ^| find "."`
  19.     ) do if not defined TIMETAG set "TIMETAG=%%~t"
  20.     set "TIMETAG=%TIMETAG:~2,6%-%TIMETAG:~8,4%"
  21.  
  22.     set "PREVFILE="
  23.     if exist "%PATTERN%" for /f "usebackq delims=" %%f in (
  24.       `dir /b /o:-d "%PATTERN%" ^| find /i ".txt"`
  25.     ) do if not defined PREVFILE set "PREVFILE=%%~f"
  26.  
  27.     set "FILETAG="
  28. :argloop
  29.     if "%~1" == "" goto :argdone
  30.     if defined FILETAG set "FILETAG=%FILETAG% "
  31.     set "FILETAG=%FILETAG%%~1"
  32.     shift /1
  33.     goto :argloop
  34. :argdone
  35.  
  36.     set "DELETEFILE="
  37.     if not defined FILETAG set "DELETEFILE=1"
  38.     if not defined FILETAG set "FILETAG=Temp"
  39.  
  40.     set "FILETAG=%FILETAG:~0,18%"
  41. :trailingspaceloop
  42.     if not "%FILETAG:~-1%" == " " goto :trailingspacedone
  43.     set "FILETAG=%FILETAG:~0,-1%"
  44.     goto :trailingspaceloop
  45. :trailingspacedone
  46.  
  47.     set "FILENAME=%BASENAME% %TIMETAG% %FILETAG%.txt"
  48.     if not exist "%FILENAME%" goto :usefilename
  49.     echo Cannot use file name "%FILENAME%": file exists.
  50.     exit /b 1
  51. :usefilename
  52.  
  53.     set "FONTCOUNT=0"
  54.  
  55.     for /f "usebackq delims=" %%f in (
  56.       `reg query "%KEYNAME%" ^| find " REG_SZ " ^| sort /locale C`
  57.     ) do call :fontentry "%%~f"
  58.  
  59.     if "%FONTCOUNT%" == "1" (
  60.       echo Found 1 font
  61.     ) else (
  62.       echo Found %FONTCOUNT% fonts
  63.     )
  64.  
  65.     if not defined PREVFILE (
  66.       echo.
  67.       goto :comparedone
  68.     )
  69.     fc "%PREVFILE%" "%FILENAME%" > nul 2> nul
  70.     if not errorlevel 1 (
  71.       echo The font list has not been changed
  72.       echo.
  73.       set "DELETEFILE=1"
  74.       goto :comparedone
  75.     )
  76.     echo The font list has been changed
  77.     echo .
  78.     fc "%PREVFILE%" "%FILENAME%"
  79. :comparedone
  80.  
  81.     dir /o:d "%PATTERN%" 2> nul | find /i ".txt"
  82.  
  83.     if defined DELETEFILE del "%FILENAME%" > nul 2> nul
  84.  
  85.     exit /b
  86.  
  87. ::  ----------------------------------------------------------------------------
  88. ::  call :fontentry "value"
  89. ::  ----------------------------------------------------------------------------
  90.  
  91. ::  The subroutine :fontentry is called with one argument, which is a quoted
  92. ::  string representing a registry entry extracted from %KEYNAME%, in the format
  93. ::  returned by reg query. The subroutine will increase %FONTCOUNT% and write
  94. ::  the entry to %FILENAME%. Leading spaces are deleted, REG_SZ and the
  95. ::  surrounding spaces are replaced with " = ", multiple spaces are replaced
  96. ::  with one space, and some problematic characters are sanitized.
  97.  
  98. :fontentry
  99.  
  100.     set "FONTENTRY=%~1"
  101.     set "FONTENTRY=%FONTENTRY:~4%"
  102.  
  103. :shrinkspacesloop
  104.     set "FONTENTRYTEMP=%FONTENTRY:  = %"
  105.     if "%FONTENTRYTEMP%" == "%FONTENTRY%" goto :shrinkspacesdone
  106.     set "FONTENTRY=%FONTENTRYTEMP%"
  107.     goto :shrinkspacesloop
  108. :shrinkspacesdone
  109.  
  110.     set "FONTENTRY=%FONTENTRY: REG_SZ = = %"
  111.  
  112.     set "FONTENTRY=%FONTENTRY:&=+%"
  113.     set "FONTENTRY=%FONTENTRY:^=\%"
  114.     set "FONTENTRY=%FONTENTRY:>=(%"
  115.     set "FONTENTRY=%FONTENTRY:<=)%"
  116.     set "FONTENTRY=%!!|!%"
  117.  
  118.     set /a "FONTCOUNT+=1"
  119.     >> "%FILENAME%" echo %FONTENTRY%
  120.  
  121.     exit /b
  122.  
  123. ::  ----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement