From eeacec4b3df2d3105c24bf32a5ebd0753de22322 Mon Sep 17 00:00:00 2001 From: Adam K Date: Wed, 17 Jan 2024 00:18:22 +0000 Subject: [PATCH 1/4] Non-functional test of self-wrapping logic Has potential to handle all future critical errors cleanly --- batch encoder.bat | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/batch encoder.bat b/batch encoder.bat index fde0dd7..a2e167a 100644 --- a/batch encoder.bat +++ b/batch encoder.bat @@ -1,7 +1,7 @@ @echo off setlocal enabledelayedexpansion -set CurrentVersion=v1.7.2 +set CurrentVersion=v1.7.3 cls set "icongray=" @@ -18,7 +18,6 @@ set "formatend=" ) set "space= " - for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser)) :ArgParser @@ -45,6 +44,14 @@ for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser)) set "par_!FLAG!=true" if "%par_debug%"=="true" (echo %iconyellow%par_!FLAG!=TRUE%formatend%) ) + +if not defined par_selfwrapped ( + cmd /c "%~f0" %* --selfwrapped && (echo !errorlevel!) || (set "wraperror=true") + rem echo EXITED WRAP ^(error code !errorlevel!, wrap error !wraperror!^) + if "!wraperror!"=="true" (call:CritError "Wrapper critically exited with error code !errorlevel!.") + echo !errorlevel! + exit /b !errorlevel! +) if "%par_debug%"=="true" (pause) cls @@ -94,7 +101,7 @@ if defined par_updated-from ( if "%par_silent%"=="true" (goto AskUpdate) echo %icongray% i %formatend% This program will aim to encode all .mp4 files in the folder it's placed in and recycle the originals. set /p "startconfirmation=Do you want to proceed? %textgray%[Y/N]%formatend%: " - if /i "%startconfirmation%"=="n" exit + if /i "%startconfirmation%"=="n" (goto EndPause) if /i "%startconfirmation%"=="y" (goto AskUpdate) goto AskProceed @@ -119,7 +126,7 @@ if defined par_updated-from ( if exist "batch encoder %UpdateVersion%%append%-u.bat" (del "batch encoder %UpdateVersion%%append%-u.bat") echo %icongray% i %formatend% Downloading information... set "updateFileName=batch_update.json" - curl --silent -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version:2022-11-28" -o %updateFileName% https://api.github.com/repos/Adam-Kay/Batch-Encoder/releases/latest + rem curl --silent -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version:2022-11-28" -o %updateFileName% https://api.github.com/repos/Adam-Kay/Batch-Encoder/releases/latest if not exist "%updateFileName%" (goto AutoUpdateError) >%TEMP%\batch_update.tmp findstr "tag_name" %updateFileName% @@ -127,6 +134,8 @@ if defined par_updated-from ( set "ver=%entry_ver:~15,-2%" set "UpdateVersion=v%ver:~1%" + pause + >%TEMP%\batch_update.tmp findstr "body" %updateFileName% set "pwsh_replace=-replace '^!' -replace '^<\/\S*?^>', '#[FORMEND]#' -replace '^<\S*?^>', '#[FORM]#' -replace '^<', '(less)' -replace '^>', '(more)'" for /F "tokens=*" %%g in ('powershell -Command "(Get-Content $env:TEMP\batch_update.tmp) !pwsh_replace! "') do (set entry_body=%%g) From f49186e751e69a5ff219e43c3c4e635789dc7b8a Mon Sep 17 00:00:00 2001 From: Adam K Date: Wed, 17 Jan 2024 23:47:07 +0000 Subject: [PATCH 2/4] Finalized new wrapper error handler. Also babyproofed some delete statements. In rare cases, they would suggest to the user if they wanted to delete the entire directory ('del ""' is dangerous). --- batch encoder.bat | 46 ++++++++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/batch encoder.bat b/batch encoder.bat index a2e167a..2911257 100644 --- a/batch encoder.bat +++ b/batch encoder.bat @@ -46,10 +46,15 @@ for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser)) ) if not defined par_selfwrapped ( - cmd /c "%~f0" %* --selfwrapped && (echo !errorlevel!) || (set "wraperror=true") - rem echo EXITED WRAP ^(error code !errorlevel!, wrap error !wraperror!^) - if "!wraperror!"=="true" (call:CritError "Wrapper critically exited with error code !errorlevel!.") - echo !errorlevel! + cmd /c "%~f0" %* --selfwrapped || (set "wraperror=true") + if "!wraperror!"=="true" ( + if "!errorlevel:~0,3!"=="101" ( + ::if errorlevel starts with 101, it's already been handled + set "errorlevel=!errorlevel:~3! + ) else ( + call:CritError "Wrapper critically exited with error code !errorlevel!." + ) + ) exit /b !errorlevel! ) @@ -134,8 +139,6 @@ if defined par_updated-from ( set "ver=%entry_ver:~15,-2%" set "UpdateVersion=v%ver:~1%" - pause - >%TEMP%\batch_update.tmp findstr "body" %updateFileName% set "pwsh_replace=-replace '^!' -replace '^<\/\S*?^>', '#[FORMEND]#' -replace '^<\S*?^>', '#[FORM]#' -replace '^<', '(less)' -replace '^>', '(more)'" for /F "tokens=*" %%g in ('powershell -Command "(Get-Content $env:TEMP\batch_update.tmp) !pwsh_replace! "') do (set entry_body=%%g) @@ -168,7 +171,7 @@ if defined par_updated-from ( echo. echo The program will now restart. call:GrayPause - del "%updateFileName%" + if exist "%updateFileName%" (del "%updateFileName%") if /i "%par_silent%"=="true" ( (goto) 2>nul & "%~f0" %* --update false ) else ( @@ -210,7 +213,7 @@ if defined par_updated-from ( echo. echo %icongray% i %formatend% The program will now clean up and restart. call:GrayPause - del "%updateFileName%" + if exist "%updateFileName%" (del "%updateFileName%") if /i "%par_silent%"=="true" ( (goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false ) else ( @@ -362,7 +365,7 @@ if defined par_updated-from ( :Conversion if /i not "%par_verbose%"=="true" (set "quietargs=-v quiet -stats ") - for %%f in (.\*) do ( + for %%f in (.\*) do call:ClearAndTitle "%speed_text%%quality_text%" @@ -445,12 +448,16 @@ if defined par_updated-from ( ) else ( if "%par_waste%"=="delete" ( echo %icongreen% ^| %formatend% Safely proceeding with input file deletion... - del "%CD%\!INPUTFILE!" + if exist "!INPUTFILE!" (del "%CD%\!INPUTFILE!") ) else ( - if not "%par_waste%"=="recycle" (echo --waste argument "%par_waste%" not recognized. Defaulting to "recycle". & echo.) + if not "%par_waste%"=="recycle" ( + if defined par_waste (echo --waste argument "%par_waste%" not recognized. Defaulting to "recycle". & echo.) + ) echo %icongreen% ^| %formatend% Safely proceeding with input file recycling... - ::delete to recycle bin - powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('%CD%\!INPUTFILE!','OnlyErrorDialogs','SendToRecycleBin')" + if exist "!INPUTFILE!" ( + ::delete to recycle bin + powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('%CD%\!INPUTFILE!','OnlyErrorDialogs','SendToRecycleBin')" + ) ) ) timeout /nobreak /t 1 > nul @@ -478,21 +485,28 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%) set errmsg=%~1 if defined errmsg (echo Error message provided: %errmsg%) call:GrayPause - (goto) 2>nul || exit /b 3 + call:CtrlExit 3 :AutoUpdateError - del "%updateFileName%" + if exist "%updateFileName%" (del "%updateFileName%") echo. call:ErrorLine echo. echo There was a problem with the auto-updater. You can download the latest version of the program at: echo https://github.com/Adam-Kay/Batch-Encoder/releases echo. - if /i "%par_silent%"=="true" (exit /b 2) + if /i "%par_silent%"=="true" (call:CtrlExit 2) echo The program will now restart. call:GrayPause (goto) 2>nul & "%~f0" +:CtrlExit + if defined par_selfwrapped ( + exit 101%~1 + ) + :: go one level up (out of subroutine), then exit + (goto) 2>nul || exit /b %~1 + :GrayPause echo %textgray% if /i not "%par_silent%"=="true" (pause) From 2fa308ffdaaee389547f9fef95a1876dde009e60 Mon Sep 17 00:00:00 2001 From: Adam K Date: Thu, 18 Jan 2024 16:41:56 +0000 Subject: [PATCH 3/4] Non-functional attempt at rectifying wrapper exit behavior --- batch encoder.bat | 68 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 48 insertions(+), 20 deletions(-) diff --git a/batch encoder.bat b/batch encoder.bat index 2911257..c88570b 100644 --- a/batch encoder.bat +++ b/batch encoder.bat @@ -18,9 +18,12 @@ set "formatend=" ) set "space= " +set "starttime=%TIME%" + for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser)) :ArgParser + set "allargs=%*" set "FLAG=0" for %%G in (%*) DO ( set ARG=%%G @@ -44,17 +47,37 @@ for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser)) set "par_!FLAG!=true" if "%par_debug%"=="true" (echo %iconyellow%par_!FLAG!=TRUE%formatend%) ) - -if not defined par_selfwrapped ( - cmd /c "%~f0" %* --selfwrapped || (set "wraperror=true") + +if not "%par_selfwrapped%"=="true" ( + cmd /c ""%~f0" %* --selfwrapped" || (set "wraperror=true") + echo UNWRAPPED ^(!errorlevel!^) + echo Returning to instance %starttime% + echo Currentargs: %* + echo Currentargs2: !allargs! if "!wraperror!"=="true" ( + <%TEMP%\batch_update.tmp set /p "newfilename=" + echo newfilename: !newfilename! + pause + rem If errorlevel is negative, it's a restart command: + rem Standard restart + if "!errorlevel!"=="-1" ( + echo ERROR MINUS 1 - RESTART REQUESTED + pause + "%~f0" + ) + rem Restart after update + if "!errorlevel!"=="-2" (!newfilename! --updated-from "%~f0") + rem Restart after update (with silent, so passing previous args) + rem if "!errorlevel!"=="-3" ((goto) 2>nul & !newfilename! --updated-from "%~f0" %* --update false --selfwrapped false) + + rem If errorlevel starts with 101, it's already been handled if "!errorlevel:~0,3!"=="101" ( - ::if errorlevel starts with 101, it's already been handled - set "errorlevel=!errorlevel:~3! + set "errorlevel=!errorlevel:~3!" ) else ( call:CritError "Wrapper critically exited with error code !errorlevel!." ) ) + echo about to exit with code !errorlevel! exit /b !errorlevel! ) @@ -62,7 +85,7 @@ if "%par_debug%"=="true" (pause) cls if defined par_help ( - ::list help for args, then exit + rem list help for args, then exit echo %formatend%Help for Batch Encoder %CurrentVersion% echo Program usage: echo. @@ -96,7 +119,7 @@ if defined par_help ( if defined par_updated-from ( echo %icongray% ^^! %formatend% Just updated^^! Running cleanup... timeout /nobreak 2 > nul - ::↓ special format to remove " from string + rem ↓ special format to remove " from string if exist "%par_updated-from:"=%" (del "%par_updated-from:"=%") ) @@ -175,7 +198,7 @@ if defined par_updated-from ( if /i "%par_silent%"=="true" ( (goto) 2>nul & "%~f0" %* --update false ) else ( - (goto) 2>nul & "%~f0" + exit -1 ) ) ) @@ -186,7 +209,7 @@ if defined par_updated-from ( echo %iconyellow% ^^! %formatend% Differing version found^^! ^(%textred%%CurrentVersion%%formatend% -^> %textgreen%%UpdateVersion%%formatend%^) echo Proceeding with update in 5 seconds; close window to cancel. ) - :: ... anim + rem ... anim echo. & echo  for /l %%x in (1, 1, 6) do ( timeout /nobreak /t 1 > nul @@ -212,12 +235,17 @@ if defined par_updated-from ( echo You can read the full changelog at: https://github.com/Adam-Kay/Batch-Encoder/releases echo. echo %icongray% i %formatend% The program will now clean up and restart. + pause rem REMOVE call:GrayPause if exist "%updateFileName%" (del "%updateFileName%") + echo "batch encoder %UpdateVersion%%append%.bat" > %TEMP%\batch_update.tmp if /i "%par_silent%"=="true" ( - (goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false + exit -3 + rem (goto) 2>nul & (goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false --selfwrapped false + rem cmd /c ""batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false --selfwrapped false" & exit ) else ( - (goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" + exit -2 + rem "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" ) @@ -392,14 +420,14 @@ if defined par_updated-from ( set "OUTPUTFILE=!INPUTFILE:~0,-8!.ENC.mp4" ) else ( set "outputfiledupe=true" - set "OUTPUTFILE=!INPUTFILE:~0,-8!_!date!-!time::=-!.ENC.mp4" + set "OUTPUTFILE=!INPUTFILE:~0,-8!_!date!-!timerem =-!.ENC.mp4" ) ) else ( if not exist "!INPUTFILE:~0,-4!.ENC.mp4" ( set "OUTPUTFILE=!INPUTFILE:~0,-4!.ENC.mp4" ) else ( set "outputfiledupe=true" - set "OUTPUTFILE=!INPUTFILE:~0,-4!_!date!-!time::=-!.ENC.mp4" + set "OUTPUTFILE=!INPUTFILE:~0,-4!_!date!-!timerem =-!.ENC.mp4" ) ) @@ -429,7 +457,7 @@ if defined par_updated-from ( ) do (set LEN_INP=%%g) for /F "tokens=*" %%g in ( 'powershell -Command "(^& '%LOCATION_pwsh%' -i '!OUTPUTFILE!' 2>&1 | select-String 'Duration: (.*), s').Matches.Groups[1].Value"' ) do (set LEN_OUT=%%g) - for /F "tokens=*" %%g in ( 'powershell -Command "[Math]::Abs(((Get-Date !LEN_INP!) - (Get-Date !LEN_OUT!)).TotalSeconds)"' + for /F "tokens=*" %%g in ( 'powershell -Command "[Math]rem Abs(((Get-Date !LEN_INP!) - (Get-Date !LEN_OUT!)).TotalSeconds)"' ) do (set LEN_DIFF=%%g) echo Input file: !LEN_INP! - Output file: !LEN_OUT! @@ -455,8 +483,8 @@ if defined par_updated-from ( ) echo %icongreen% ^| %formatend% Safely proceeding with input file recycling... if exist "!INPUTFILE!" ( - ::delete to recycle bin - powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('%CD%\!INPUTFILE!','OnlyErrorDialogs','SendToRecycleBin')" + rem delete to recycle bin + powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]rem DeleteFile('%CD%\!INPUTFILE!','OnlyErrorDialogs','SendToRecycleBin')" ) ) ) @@ -498,13 +526,13 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%) if /i "%par_silent%"=="true" (call:CtrlExit 2) echo The program will now restart. call:GrayPause - (goto) 2>nul & "%~f0" + exit -1 :CtrlExit - if defined par_selfwrapped ( + if "%par_selfwrapped%"=="true" ( exit 101%~1 ) - :: go one level up (out of subroutine), then exit + rem go one level up (out of subroutine), then exit (goto) 2>nul || exit /b %~1 :GrayPause @@ -535,6 +563,6 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%) cls set "message=%~1" if defined message (set "message=- %message% ") - echo  Batch Encoder %CurrentVersion% %message%%formatend% + echo  Batch Encoder %CurrentVersion% %message%%formatend% (%starttime%) (%allargs%) echo. goto:eof From aba92540290750fa42480d88b4e1463a5b2fd62d Mon Sep 17 00:00:00 2001 From: Adam K Date: Mon, 22 Jan 2024 21:33:02 +0000 Subject: [PATCH 4/4] Another non-functional attempt at wrapper code --- batch encoder.bat | 67 +++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/batch encoder.bat b/batch encoder.bat index c88570b..94ca693 100644 --- a/batch encoder.bat +++ b/batch encoder.bat @@ -1,4 +1,4 @@ -@echo off +@echo on setlocal enabledelayedexpansion set CurrentVersion=v1.7.3 @@ -49,35 +49,40 @@ for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser)) ) if not "%par_selfwrapped%"=="true" ( - cmd /c ""%~f0" %* --selfwrapped" || (set "wraperror=true") - echo UNWRAPPED ^(!errorlevel!^) - echo Returning to instance %starttime% + start /wait "" /b "%~f0" %* --selfwrapped + echo UNWRAPPED ^(!wraperror!^) ^(!errorlevel!^) + echo Returning to instance !starttime! echo Currentargs: %* echo Currentargs2: !allargs! - if "!wraperror!"=="true" ( - <%TEMP%\batch_update.tmp set /p "newfilename=" - echo newfilename: !newfilename! + echo Selfwrapped: !par_selfwrapped! + <%TEMP%\batch_update.tmp set /p "newfilename=" + echo newfilename: !newfilename! + pause + rem If errorlevel is negative, it's a restart command: + rem Standard restart + if "!errorlevel!"=="-1" ( + echo ERROR -1 - RESTART REQUESTED + echo restarting "%~f0" pause - rem If errorlevel is negative, it's a restart command: - rem Standard restart - if "!errorlevel!"=="-1" ( - echo ERROR MINUS 1 - RESTART REQUESTED - pause - "%~f0" - ) - rem Restart after update - if "!errorlevel!"=="-2" (!newfilename! --updated-from "%~f0") - rem Restart after update (with silent, so passing previous args) - rem if "!errorlevel!"=="-3" ((goto) 2>nul & !newfilename! --updated-from "%~f0" %* --update false --selfwrapped false) - - rem If errorlevel starts with 101, it's already been handled - if "!errorlevel:~0,3!"=="101" ( - set "errorlevel=!errorlevel:~3!" - ) else ( - call:CritError "Wrapper critically exited with error code !errorlevel!." - ) + cmd /c "%~f0" & exit /b + ) + rem Restart after update + if "!errorlevel!"=="-2" ( + rem !newfilename! --updated-from "%~f0" + echo cmd /c ""!newfilename:"=!"" --updated-from "%~f0" ^& exit /b + cmd /c @"!newfilename:"=!" --updated-from "%~f0" & exit /b ) - echo about to exit with code !errorlevel! + rem Restart after update (with silent, so passing previous args) + rem if "!errorlevel!"=="-3" ((goto) 2>nul & !newfilename! --updated-from "%~f0" %* --update false --selfwrapped false) + + rem If errorlevel starts with 101, it's already been handled + if "!errorlevel:~0,3!"=="101" ( + set "errorlevel=!errorlevel:~3!" + ) else ( + echo ABOUT TO CALL CRIT ERROR + call:CritError "Wrapper critically exited with error code !errorlevel!." + ) + echo instance %starttime% about to exit with code !errorlevel! exit /b !errorlevel! ) @@ -244,7 +249,8 @@ if defined par_updated-from ( rem (goto) 2>nul & (goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false --selfwrapped false rem cmd /c ""batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false --selfwrapped false" & exit ) else ( - exit -2 + rem exit -2 + (goto) 2>nul & cmd /c ""batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0"" rem "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" ) @@ -502,7 +508,7 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%) :EndPause call:GrayPause - exit /b 0 + call:CtrlExit 0 :CritError timeout /t 1 > nul @@ -514,6 +520,7 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%) if defined errmsg (echo Error message provided: %errmsg%) call:GrayPause call:CtrlExit 3 + goto:eof :AutoUpdateError if exist "%updateFileName%" (del "%updateFileName%") @@ -527,13 +534,17 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%) echo The program will now restart. call:GrayPause exit -1 + goto:eof :CtrlExit if "%par_selfwrapped%"=="true" ( + echo SAFE EXIT exit 101%~1 ) + echo UNKNOWN rem go one level up (out of subroutine), then exit (goto) 2>nul || exit /b %~1 + goto:eof :GrayPause echo %textgray%