Batch Automation : DGN to DWG

I often get this request to batch automate converting DGN files to DWG files with minimal overhead, we can leverage either APS Automation service or local instance of AcCoreConsole that is part of AutoCAD.


@echo off
setlocal enabledelayedexpansion
REM ============================================================
REM dgn2dwg_batch.bat
REM Batch-convert all .DGN files in a folder to .DWG using AutoCAD
REM Requires: AutoCAD executable path, write access to output folder
REM ============================================================
REM ---- CONFIG: Update path to AutoCAD executable if needed ----
set ACAD="D:\ACAD\AutoCAD 2026\accoreconsole.exe"
REM ---- INPUT ARGUMENTS ----
REM %1 = source folder containing .DGN files (defaults to current directory)
if "%~1"=="" (
    set SRC=%CD%
    echo [INFO] No folder specified, using current directory: %CD%
) else (
    set SRC=%~1
)
if not exist "%SRC%" (
    echo [ERROR] Source folder not found: %SRC%
    exit /b 2
)
REM ---- TEMP working folder for scripts ----
set TMP=%TEMP%\dgn2dwg_%RANDOM%
mkdir "%TMP%" >nul 2>&1
echo ============================================================
echo DGN to DWG Batch Conversion
echo Source Folder: %SRC%
echo ============================================================
echo.
REM ---- Iterate over all DGN files ----
set COUNT=0
for %%F in ("%SRC%\*.dgn") do (
    set /a COUNT+=1
    set "BASENAME=%%~nF"
    set "DGNPATH=%%~fF"
    set "DGNFOLDER=%%~dpF"
    set "DWGPATH=!DGNFOLDER!!BASENAME!.dwg"
    echo [!COUNT!] Processing: %%~nxF
    echo     Input:  !DGNPATH!
    echo     Output: !DWGPATH!
    REM Create a temporary script file with embedded file paths
    set SCRIPT=%TMP%\convert_!BASENAME!.scr
    (
        echo -DGNIMPORT
        echo !DGNPATH!
        echo.
        echo.
        echo.
        echo _SAVEAS
        echo 2018
        echo "!DWGPATH!"
        echo _QUIT
        echo Y
    ) > "!SCRIPT!"
    REM Run accoreconsole with the script
    %ACAD% /s "!SCRIPT!" /l en-US
    REM Check if conversion succeeded
    if exist "!DWGPATH!" (
        echo     [OK] Successfully created: %%~nF.dwg
    ) else (
        echo     [ERROR] Failed to create: %%~nF.dwg
    )
    echo.
)
REM ---- Cleanup temp folder ----
rd /s /q "%TMP%" >nul 2>&1
echo ============================================================
if %COUNT%==0 (
    echo [DONE] No DGN files found in "%SRC%"
) else (
    echo [DONE] Processed %COUNT% DGN file(s) from "%SRC%"
    echo       DWG files saved in same folder as source DGN files
)
echo ============================================================
endlocal

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading