Tag Archives: Batch script

Batch convert SVG to PNG on Windows using Inkscape

Needed to convert several SVGs to PNG, and discovered the free Inkscape easily can be used in batch scripts.

The following .bat file will convert all SVGs dropped on it to a PNG with height 48 placed next to the original SVG. More options can be found in the Inkscape manual.

@echo off
for %%f in (%*) do (
    echo %%~f
    "C:\Program Files\Inkscape\inkscape.exe" ^
      -z ^
      --export-background-opacity=0 ^
      --export-height=48 ^
      --export-png="%%~dpnf.png" ^
      --file="%%~f"

)

BAT: Change working directory to directory of script

Sometimes you want a batch file to always run with the directory of the script as the working directory. Not whatever directory you ran it from.

@echo off
pushd %~dp0
do stuff
popd

Here pushd changes the directory to %~dp0, which is the directory of the running batch script. popd returns to whatever it was when the script started running. Not sure if the last step is necessary, but there you go anyways 🙂

Bat: Some simple things to make batch scripts more helpful

Tried to make a helpful batch script for something at work and learned some new things I thought I’d write down here so I know where to find it later.

Turn off echoing

Should be known by everyone really, but in case you don’t. Prevents the commands in the batch script and the command prompt to show up while the script is running. Beginning a line with @ does this for a single command.

@echo off

Comments

REM Some comment
REM Will still appear as a command if you haven't disabled echoing

Labels and goto

Trivial, but I keep forgetting about this since I never use it in other programming.

goto end
REM Will be skipped
:end

Set title of command prompt

title MyScript: Doing x now

Check command line arguments

if "%~1"=="" (echo Usage: test.bat path && goto end)
if not exist %~1 (echo %~1 does not exist && goto end)

Simpe choices

choice /c ny /n /d y /t 10 /m "Do you really want to do this? [y=10s,n]
if ERRORLEVEL 2 (
    echo Ok, I'll do it
)

Run other batch scripts

If you don’t use the call command, the other script will actually take over and your script will end.

call other.bat arg1 arg2 ...

Do the same thing with various subjects

for %%x in (
    a
    b
    c
) do (
    echo %%x
)