rem convert_html_folders_to_png.bat
rem v6.5
rem This bat is working and does not delete the server log
rem =======
@echo off
setlocal enabledelayedexpansion
set "main_folder=C:\aHTML\My Notes"
set "wkhtmltoimage_path=C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage.exe"
set "python_path=C:\xxx?xxx\xxx?xxx\06___Programming\Python\python.exe"
set "log_file=C:\aHTML\My Notes\conversion_log.txt"
set "server_log_file=C:\aHTML\My Notes\server_log.txt"
rem Delete old conversion log file if it exists
if exist "%log_file%" del "%log_file%"
pushd "%main_folder%"
echo Starting local server in %main_folder% >> "%log_file%" 2>&1
start "" cmd /c "%python_path% -m http.server 8000 > "%server_log_file%" 2>&1"
echo Waiting for server to start... >> "%log_file%" 2>&1
timeout /t 10 >> "%log_file%" 2>&1
echo Checking if server started... >> "%log_file%" 2>&1
set server_started=false
for /f "tokens=5" %%a in ('netstat -aon ^| findstr :8000') do (
echo Server PID: %%a >> "%log_file%" 2>&1
set server_pid=%%a
set server_started=true
)
if "%server_started%"=="false" (
echo Failed to start local server. Check Python installation, Python path, and server_log.txt. Exiting. >> "%log_file%" 2>&1
popd
exit /b 1
)
echo ******** Ready to loop through HTML files ********** >> "%log_file%" 2>&1
for %%f in (*.html) do (
set "filename=%%~nf"
echo Original filename: !filename! >> "%log_file%" 2>&1
rem More targeted sanitization
set "sanitized_filename=!filename: =_!"
set "sanitized_filename=!sanitized_filename:~0,50!"
rem set "sanitized_filename=!sanitized_filename:?=_!"
rem set "sanitized_filename=!sanitized_filename:*=_!"
rem set "sanitized_filename=!sanitized_filename:/=_!"
rem set "sanitized_filename=!sanitized_filename:\=_!"
rem set "sanitized_filename=!sanitized_filename:^=_!"
rem set "sanitized_filename=!sanitized_filename:"=_!"
rem set "sanitized_filename=!sanitized_filename:'=_!"
echo Processing file: %%f >> "%log_file%" 2>&1
echo Final sanitized filename: !sanitized_filename! >> "%log_file%" 2>&1
"%wkhtmltoimage_path%" "http://localhost:8000/%%f" "!sanitized_filename!.png" >> "%log_file%" 2>&1
if exist "!sanitized_filename!.png" (
echo Successfully created: !sanitized_filename!.png >> "%log_file%" 2>&1
) else (
echo Failed to create: !sanitized_filename!.png >> "%log_file%" 2>&1
)
)
echo Note any errors >> "%log_file%" 2>&1
taskkill /pid %server_pid% /f >> "%log_file%" 2>&1
popd
rem Kill the CMD window running the server
for /f "tokens=2 delims==;" %%i in ('wmic process where "name='cmd.exe' and CommandLine like '%%python.exe -m http.server%%'" get processid /value ^| find "="') do (
taskkill /pid %%i /f >> "%log_file%" 2>&1
)
echo Conversion complete. >> "%log_file%" 2>&1
rem Close the CMD window
exit