0

I have this batch file:

for %%f in (*.svg) do (

"C:\Program Files\Inkscape\inkscape.com" ^
  -z ^
  --export-background-opacity=0 ^
  --export-height=256 ^
  --export-png="%%~nf_256x256.png" ^
  --file="{%%~f}"  

"C:\Program Files\Inkscape\inkscape.com" ^
  -z ^
  --export-background-opacity=0 ^
  --export-height=128 ^
  --export-png="%%~nf_128x128.png" ^
  --file="%%~f"  

"C:\Program Files\Inkscape\inkscape.com" ^
  -z ^
  --export-background-opacity=0 ^
  --export-height=64 ^
  --export-png="%%~nf_64x64.png" ^
  --file="%%~f" 

"C:\Program Files\Inkscape\inkscape.com" ^
  -z ^
  --export-background-opacity=0 ^
  --export-height=48 ^
  --export-png="%%~nf_48x48.png" ^
  --file="%%~f" 

"C:\Program Files\Inkscape\inkscape.com" ^
  -z ^
  --export-background-opacity=0 ^
  --export-height=32 ^
  --export-png="%%~nf_32x32.png" ^
  --file="%%~f" 

"C:\Program Files\Inkscape\inkscape.com" ^
  -z ^
  --export-background-opacity=0 ^
  --export-height=24 ^
  --export-png="%%~nf_24x24.png" ^
  --file="%%~f"  

"C:\Program Files\Inkscape\inkscape.com" ^
  -z ^
  --export-background-opacity=0 ^
  --export-height=16 ^
  --export-png="%%~nf_16x16.png" ^
  --file="%%~f" 

"C:\Program Files\ImageMagick-7.0.7-Q16\magick.exe" convert %%~nf_16x16.png %%~nf_24x24.png %%~nf_32x32.png %%~nf_64x64.png %%~nf_128x128.png %%~nf_256x256.png %%~nf.ico)

What it does:

  1. Convert every single .svg in the folder to multiple .png's in different sizes
  2. Combine these .png's into a single .ico

Software used: ImageMagick and Inkscape.

Now there is another 'command' I want to add:

mogrify -units "PixelsPerInch" -density 96 *.png

This script converts the ppi of every png to 96. This needs to happen before the conversion to .ico

I also have this batch file:

@echo off
Setlocal enabledelayedexpansion

Set "Pattern= "
Set "Replace=_"

For %%a in (*.svg) Do (
    Set "File=%%~a"
    Ren "%%a" "!File:%Pattern%=%Replace%!"
)

Pause&Exit

This replaces file name spaces with an underscore. Which needs to happen at first before conversion to .png.

How do I add/combine this into a single batch file?

1 Answer 1

1

Batch files are intended to avoid manual repititious tasks,
all the different resolutions can be put in another nested for.

The renaming should be first to allow access to the ~ modifiers later

Untested:

:: Q:\Test\2019\01\15\SO_1394446.cmd
@Echo off&SetLocal EnableDelayedExpansion

Set inkscape="C:\Program Files\Inkscape\inkscape.com"
Set magick="C:\Program Files\ImageMagick-7.0.7-Q16\magick.exe"

Set "Pattern= "
Set "Replace=_"

For %%a in ("* *.svg") Do (
    Set "File=%%~a"
    Ren "%%a" "!File:%Pattern%=%Replace%!"
)

for %%f in (*.svg) do (
    for %%r in (256 128 64 48 32 24 16) Do (
        %inkscape% ^
          -z ^
          --export-background-opacity=0 ^
          --export-height=%%r ^
          --export-png="%%~nf_%%rx%%r.png" ^
          --file="%%~f"  
    )
    mogrify -units "PixelsPerInch" -density 96 %%~nf_*.png

    %magick% convert %%~nf_16x16.png ^
                     %%~nf_24x24.png ^
                     %%~nf_32x32.png ^
                     %%~nf_48x48.png ^
                     %%~nf_64x64.png ^
                     %%~nf_128x128.png ^
                     %%~nf_256x256.png %%~nf.ico
)
7
  • Thanks for your anwser! When I run this as a batch file in the folder the svg's are located, CMD opens for a brief second and then immediately closes. Jan 15, 2019 at 14:33
  • To debug a batch always start it in an open cmd window to be able to see error messages. I got one silly error, missing do, corrected
    – LotPings
    Jan 15, 2019 at 14:57
  • Thanks! It's running now but I get loads of errors. I've pasted a part of it here: pastebin.com/te9wkbGp . These are the errors it gives for each file. Jan 15, 2019 at 15:36
  • Sorry I'm curious were thoes {} came from, removed.
    – LotPings
    Jan 15, 2019 at 15:52
  • 1
    It was missing in your template, added it.
    – LotPings
    Jan 16, 2019 at 10:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .