Skip to content

Instantly share code, notes, and snippets.

@fwitte
Forked from JohannesDeml/README.md
Last active November 24, 2021 11:15
Show Gist options
  • Save fwitte/f793e453bd4ede8ec0701911da99c6c2 to your computer and use it in GitHub Desktop.
Save fwitte/f793e453bd4ede8ec0701911da99c6c2 to your computer and use it in GitHub Desktop.
Batch converter for windows using inkscape and the command line

Batch convert svg|pdf|eps|emf|wmf to eps|pdf|png|svg

Screenshot Batch converter for Windows using Inkscape with the command line
InkscapeBatchConvert is an easy to use solution to quickly convert all files of a folder to another type without the need to open Inkscape. The program uses Windows Batch scripting and will only work on Windows.
Tested with Inkscape 0.9.x - 1.0.x ✅

Usage

  1. Download _InkscapeBatchConvert.bat
  2. Put it in the folder where you have files you wish to convert.
  3. Then double click the file to start it.

Troubleshooting

Problem Solution
Windows blocks the program Right click the file → Properties → Unblock
Windows protected your PC Disable blocking
I can't convert EPS / PDF files. The program says it runs the conversion, but there are no files in the out folder. Make sure you have ghost script installed and added to your environment path. For more information read the Inkscape FAQ
Add ghostscript to path
Can't find Inkscape installation All common file paths are checked for the installation. If you haven't installed Inkscape download it here, otherwise you can also define your own path in the script by setting inkscapePath1 yourself
Can't find inkscape installation custom inkscape path
I'm missing a setting in the script, e.g. defined output width Have a look at the Commandline Documentation or read the inkscape help file inkscape --help and add the needed properties to the script
Inkscape help output Script change position

Todo

Use shell or pipe to avoid starting inkscape for every file as pointed out by @vaifrax here. I would be very happy to get help for that 🙂

Credits

:: From https://gist.github.com/JohannesDeml/779b29128cdd7f216ab5000466404f11
:: Convert vector file formats in the current folder and their subsfolders
@Echo off
setlocal ENABLEDELAYEDEXPANSION
:: Possible paths to check for the installation
set inkscapePath1="C:\Program Files\Inkscape\bin\inkscape.com"
set inkscapePath2="C:\Program Files\Inkscape\inkscape.com"
set inkscapePath3="C:\Program Files (x86)\Inkscape\bin\inkscape.com"
set inkscapePath4="C:\Program Files (x86)\Inkscape\inkscape.com"
if exist %inkscapePath1% (
set inkscapePath=%inkscapePath1%
) else (
if exist %inkscapePath2% (
set inkscapePath=%inkscapePath2%
) else (
if exist %inkscapePath3% (
set inkscapePath=%inkscapePath3%
) else (
if exist %inkscapePath4% (
set inkscapePath=%inkscapePath4%
) else (
echo Can't find Inkscape installation, aborting.
GOTO end
)
)
)
)
set validInput1=svg
set validInput2=pdf
set validInput3=eps
set validInput4=emf
set validInput5=wmf
set validOutput1=eps
set validOutput2=pdf
set validOutput3=png
set validOutput4=svg
set validOutput5=wmf
:: If this can't be run, then the version is an older one
FOR /F "tokens=* USEBACKQ" %%g IN (`%inkscapePath% --version`) do (SET "inkscapeVersion=%%g")
if "%inkscapeVersion%" EQU "" (
set inkscapeVersion=0.x
set /a inkscapeMajorVersion=0
) else (
set /a inkscapeMajorVersion=%inkscapeVersion:~9,1%
)
echo.
echo This script allows you to convert all files in this folder from one file type to another
echo Running with %inkscapeVersion% from %inkscapePath%
echo (type q to quit at any question)
echo.
set valid=0
echo Allowed file types for source: %validInput1%, %validInput2%, %validInput3%, %validInput4%, %validInput5%
:whileInNotCorrect
set /p sourceType=What file type do you want to use as a source?
if "%sourceType%" EQU "%validInput1%" set valid=1
if "%sourceType%" EQU "%validInput2%" set valid=1
if "%sourceType%" EQU "%validInput3%" set valid=1
if "%sourceType%" EQU "%validInput4%" set valid=1
if "%sourceType%" EQU "%validInput5%" set valid=1
if "%sourceType%" EQU "q" exit /b
if %valid% EQU 0 (
echo Invalid input! Please use one of the following: %validInput1%, %validInput2%, %validInput3%, %validInput4%, %validInput5%
goto :whileInNotCorrect
)
echo.
set valid=0
echo Allowed file types for output: %validOutput1%, %validOutput2%, %validOutput3%, %validOutput4%, %validOutput5%
:whileOutNotCorrect
set /p outputType=What file type do you want to convert to?
if "%outputType%" EQU "%validOutput1%" set valid=1
if "%outputType%" EQU "%validOutput2%" set valid=1
if "%outputType%" EQU "%validOutput3%" set valid=1
if "%outputType%" EQU "%validOutput4%" set valid=1
if "%outputType%" EQU "%validOutput5%" set valid=1
if "%outputType%" EQU "q" exit /b
if %valid% EQU 0 (
echo Invalid input! Please use one of the following: %validOutput1%, %validOutput2%, %validOutput3%, %validOutput4%, %validOutput5%
goto :whileOutNotCorrect
)
if "%outputType%" EQU "%sourceType%" (
echo Input and Output are the same, no point in doing anything. Exiting...
exit /b
)
echo.
:: Older inkscape versions need to generate a pdf before generating svgs
if %inkscapeMajorVersion% EQU 0 (
set toDelOrNot=n
if "%sourceType%" NEQ "pdf" (
if "%outputType%" EQU "%validOutput4%" (
set valid=0
:whilePdfDelNotCorrect
set /p toDelOrNot=EPS to SVG also generates pdfs, delete these after conversion? (y/n^)
if "%toDelOrNot%" EQU "y" set valid=1
if "%toDelOrNot%" EQU "n" set valid=1
if "%toDelOrNot%" EQU "q" exit /b
if %valid% EQU 0 (
echo Invalid input! Please type either y or n.
goto :whilePdfDelNotCorrect
)
)
)
)
:: Set DPI for exported file
:whileNotValidDpiNumber
set /p dpi=With what dpi should it be exported (e.g. 300)?
if "%dpi%" EQU "q" exit /b
IF %dpi% NEQ +%dpi% (
echo Invalid input! Please input an actual number.
goto :whilenotValidDpiNumber
)
echo.
:: count how many files we need to convert before converting!
set /a total=0
for /R %%i in (*.%sourceType%) do (
set /a total=total+1
)
echo Conversion started. Will do %total% file(s).
echo.
set /a count=0
:: Running through all files found with the defined ending
if %inkscapeMajorVersion% NEQ 0 (
:: Inkscape 1.0 and newer
for /R %%i in (*.%sourceType%) do (
set /a count=count+1
:: Create out folder if it does not exist
if not exist "%%~di%%~piout" mkdir "%%~di%%~piout"
echo %%i -^> %%~di%%~piout\%%~ni.%outputType% ^[!count!/%total%^]
%inkscapePath% --export-filename="%%~di%%~piout\%%~ni.%outputType%" --export-dpi=%dpi% "%%i"
)
) else (
:: Inkscape 0.9.x and older
for /R %%i in (*.%sourceType%) do (
set /a count=count+1
:: Create out folder if it does not exist
if not exist "%%~di%%~piout" mkdir "%%~di%%~piout"
echo %%i -^> %%~di%%~piout\%%~ni.%outputType% ^[!count!/%total%^]
if "%outputType%" NEQ "%validOutput4%" (
%inkscapePath% --without-gui --file="%%i" --export-%outputType%="%%~di%%~piout\%%~ni.%outputType%" --export-dpi=%dpi%
) else (
if "%sourceType%" NEQ "pdf" (
%inkscapePath% --without-gui --file="%%i" --export-pdf="%%~di%%~piout\%%~ni.pdf" --export-dpi=%dpi%
)
%inkscapePath% --without-gui -z -f "out\%%~ni.pdf" -l "%%~di%%~piout\%%~ni.%validOutput4%"
if "%toDelOrNot%" EQU "y" (
del "%%~ni.pdf" /f /q
)
)
)
)
echo.
echo %count% file(s) converted from %sourceType% to %outputType%! (Saved in out folder)
echo.
:end
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment