使用Inkscape从批处理文件将SVG转换为PNG许多文件 [英] Svg to png many files conversion using Inkscape from batch file

查看:152
本文介绍了使用Inkscape从批处理文件将SVG转换为PNG许多文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含许多svg文件的文件夹,我想使用inkscape和批处理脚本(Windows 10 *.bat 文件)将它们转换为png文件.以下脚本对于单个文件正确运行,但是,如何对目录中的数百个文件重复相同的命令?

I have a folder containing many svg files and I would like to convert them to png files using inkscape with a batch script (Windows 10 *.bat file). the following script works correctly for a single file but, how to iterate the same command for hundreds of files inside a directory?

PATH = "C:\Program Files\Inkscape"

inkscape myfile.svg --export-png=myfile.png

推荐答案

@echo off
for %%i in ("%~dp0*.svg") do (
    echo %%i to %%~ni.png
    "C:\Program Files\Inkscape\bin\inkscape.com" --export-type="png" "%%i"
)

要通过利用所有逻辑内核来加快处理速度,可以使用以下多任务处理方法:
并行执行Shell进程

To speed things up by utilizing all of your logic cores, you can use this multitasking approach:
Parallel execution of shell processes

@echo off
setlocal enableDelayedExpansion

:: Define the command that will be run to obtain the list of files to process
set listCmd=dir /b /a-d "%~dp0*.svg"

:: Define the command to run for each file, where "%%F" is an iterated file name from the list
::   something like YOUR_COMMAND -in "%%F" -out "%%~nF.ext"
set runCmd=""C:\Program Files\Inkscape\bin\inkscape.com" --export-type="png" "%%F""

:: Define the maximum number of parallel processes to run.
set "maxProc=%NUMBER_OF_PROCESSORS%"

::---------------------------------------------------------------------------------
:: The remainder of the code should remain constant
::

:: Get a unique base lock name for this particular instantiation.
:: Incorporate a timestamp from WMIC if possible, but does not fail if
:: WMIC not available. Also incorporate a random number.
  set "lock="
  for /f "skip=1 delims=-+ " %%T in ('2^>nul wmic os get localdatetime') do (
    set "lock=%%T"
    goto :break
  )
  :break
  set "lock=%temp%\lock%lock%_%random%_"

:: Initialize the counters
  set /a "startCount=0, endCount=0"

:: Clear any existing end flags
  for /l %%N in (1 1 %maxProc%) do set "endProc%%N="

:: Launch the commands in a loop
  set launch=1
  for /f "tokens=* delims=:" %%F in ('%listCmd%') do (
    if !startCount! lss %maxProc% (
      set /a "startCount+=1, nextProc=startCount"
    ) else (
      call :wait
    )
    set cmd!nextProc!=%runCmd%
    echo -------------------------------------------------------------------------------
    echo !time! - proc!nextProc!: starting %runCmd%
    2>nul del %lock%!nextProc!
    %= Redirect the lock handle to the lock file. The CMD process will     =%
    %= maintain an exclusive lock on the lock file until the process ends. =%
    start /b "" cmd /c >"%lock%!nextProc!" 2^>^&1 %runCmd%
  )
  set "launch="

:wait
:: Wait for procs to finish in a loop
:: If still launching then return as soon as a proc ends
:: else wait for all procs to finish
  :: redirect stderr to null to suppress any error message if redirection
  :: within the loop fails.
  for /l %%N in (1 1 %startCount%) do 2>nul (
    %= Redirect an unused file handle to the lock file. If the process is    =%
    %= still running then redirection will fail and the IF body will not run =%
    if not defined endProc%%N if exist "%lock%%%N" 9>>"%lock%%%N" (
      %= Made it inside the IF body so the process must have finished =%
      echo ===============================================================================
      echo !time! - proc%%N: finished !cmd%%N!
      type "%lock%%%N"
      if defined launch (
        set nextProc=%%N
        exit /b
      )
      set /a "endCount+=1, endProc%%N=1"
    )
  )
  if %endCount% lss %startCount% (
    timeout /t 1 /nobreak >nul
    goto :wait
  )

2>nul del %lock%*
echo ===============================================================================

这篇关于使用Inkscape从批处理文件将SVG转换为PNG许多文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆