Skip to content

Instantly share code, notes, and snippets.

@bjulius
Created October 8, 2024 06:05
Show Gist options
  • Save bjulius/3aa8a47813582fd62f5949ce5dff7a0f to your computer and use it in GitHub Desktop.
Save bjulius/3aa8a47813582fd62f5949ce5dff7a0f to your computer and use it in GitHub Desktop.
Batch file to extract BIM files from every PBIX file in a directory
REM Uses pbi-tools to extract the data model info and create the BIM file associated with each PBIX file in the directory
REM Batch file code written by Brian Julius, October 8, 2024
REM @echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ============================================================
REM Batch Script to Extract BIM Files from PBIX Files
REM All PBIX files are in C:\test
REM All BIM files will be written to C:\test\bim_files
REM ============================================================
REM Set the directory where your PBIX files are located
set "PBIX_DIR=C:\test"
REM Set the output directory where you want all the BIM files to be saved
set "OUTPUT_DIR=C:\test\bim_files"
REM Create the output directory if it doesn't exist
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"
REM Change to the PBIX directory
cd /d "%PBIX_DIR%"
REM Loop through all .pbix files in the directory
for %%f in (*.pbix) do (
echo -------------------------------------------
echo Processing %%f
echo -------------------------------------------
REM Create a subdirectory for extraction within the output directory
set "EXTRACT_SUBDIR=%OUTPUT_DIR%\%%~nf"
REM Ensure the extraction subdirectory is clean
if exist "!EXTRACT_SUBDIR!" rd /s /q "!EXTRACT_SUBDIR!"
REM Extract the PBIX file into the subdirectory
pbi-tools extract "%%f" -extractFolder "!EXTRACT_SUBDIR!"
REM Generate the BIM file from the extracted folder
pbi-tools generate-bim "!EXTRACT_SUBDIR!"
REM Move the BIM file to the OUTPUT_DIR with the PBIX filename
move "!EXTRACT_SUBDIR!\Model.bim" "%OUTPUT_DIR%\%%~nf.bim" /Y
REM Clean up the extraction subdirectory
rd /s /q "!EXTRACT_SUBDIR!"
)
echo.
echo All BIM files have been extracted successfully to %OUTPUT_DIR%!
pause
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment