Skip to content

Commit 8726593

Browse files
committed
Non-functional attempt at rectifying wrapper exit behavior
1 parent 9d6c86e commit 8726593

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

batch encoder.bat

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ set "formatend="
1818
)
1919
set "space= "
2020

21+
set "starttime=%TIME%"
22+
2123
for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser))
2224

2325
:ArgParser
26+
set "allargs=%*"
2427
set "FLAG=0"
2528
for %%G in (%*) DO (
2629
set ARG=%%G
@@ -44,25 +47,45 @@ for %%G in (%*) DO (if "%%G"=="--debug" (set "par_debug=true" & goto ArgParser))
4447
set "par_!FLAG!=true"
4548
if "%par_debug%"=="true" (echo %iconyellow%par_!FLAG!=TRUE%formatend%)
4649
)
47-
48-
if not defined par_selfwrapped (
49-
cmd /c "%~f0" %* --selfwrapped || (set "wraperror=true")
50+
51+
if not "%par_selfwrapped%"=="true" (
52+
cmd /c ""%~f0" %* --selfwrapped" || (set "wraperror=true")
53+
echo UNWRAPPED ^(!errorlevel!^)
54+
echo Returning to instance %starttime%
55+
echo Currentargs: %*
56+
echo Currentargs2: !allargs!
5057
if "!wraperror!"=="true" (
58+
<%TEMP%\batch_update.tmp set /p "newfilename="
59+
echo newfilename: !newfilename!
60+
pause
61+
rem If errorlevel is negative, it's a restart command:
62+
rem Standard restart
63+
if "!errorlevel!"=="-1" (
64+
echo ERROR MINUS 1 - RESTART REQUESTED
65+
pause
66+
"%~f0"
67+
)
68+
rem Restart after update
69+
if "!errorlevel!"=="-2" (!newfilename! --updated-from "%~f0")
70+
rem Restart after update (with silent, so passing previous args)
71+
rem if "!errorlevel!"=="-3" ((goto) 2>nul & !newfilename! --updated-from "%~f0" %* --update false --selfwrapped false)
72+
73+
rem If errorlevel starts with 101, it's already been handled
5174
if "!errorlevel:~0,3!"=="101" (
52-
::if errorlevel starts with 101, it's already been handled
53-
set "errorlevel=!errorlevel:~3!
75+
set "errorlevel=!errorlevel:~3!"
5476
) else (
5577
call:CritError "Wrapper critically exited with error code !errorlevel!."
5678
)
5779
)
80+
echo about to exit with code !errorlevel!
5881
exit /b !errorlevel!
5982
)
6083

6184
if "%par_debug%"=="true" (pause)
6285
cls
6386

6487
if defined par_help (
65-
::list help for args, then exit
88+
rem list help for args, then exit
6689
echo %formatend%Help for Batch Encoder %CurrentVersion%
6790
echo Program usage:
6891
echo.
@@ -96,7 +119,7 @@ if defined par_help (
96119
if defined par_updated-from (
97120
echo %icongray% ^^! %formatend% Just updated^^! Running cleanup...
98121
timeout /nobreak 2 > nul
99-
::↓ special format to remove " from string
122+
rem ↓ special format to remove " from string
100123
if exist "%par_updated-from:"=%" (del "%par_updated-from:"=%")
101124
)
102125

@@ -175,7 +198,7 @@ if defined par_updated-from (
175198
if /i "%par_silent%"=="true" (
176199
(goto) 2>nul & "%~f0" %* --update false
177200
) else (
178-
(goto) 2>nul & "%~f0"
201+
exit -1
179202
)
180203
)
181204
)
@@ -186,7 +209,7 @@ if defined par_updated-from (
186209
echo %iconyellow% ^^! %formatend% Differing version found^^! ^(%textred%%CurrentVersion%%formatend% -^> %textgreen%%UpdateVersion%%formatend%^)
187210
echo Proceeding with update in 5 seconds; close window to cancel.
188211
)
189-
:: ... anim
212+
rem ... anim
190213
echo. & echo 
191214
for /l %%x in (1, 1, 6) do (
192215
timeout /nobreak /t 1 > nul
@@ -212,12 +235,17 @@ if defined par_updated-from (
212235
echo You can read the full changelog at: https://github.com/Adam-Kay/Batch-Encoder/releases
213236
echo.
214237
echo %icongray% i %formatend% The program will now clean up and restart.
238+
pause rem REMOVE
215239
call:GrayPause
216240
if exist "%updateFileName%" (del "%updateFileName%")
241+
echo "batch encoder %UpdateVersion%%append%.bat" > %TEMP%\batch_update.tmp
217242
if /i "%par_silent%"=="true" (
218-
(goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false
243+
exit -3
244+
rem (goto) 2>nul & (goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false --selfwrapped false
245+
rem cmd /c ""batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0" %* --update false --selfwrapped false" & exit
219246
) else (
220-
(goto) 2>nul & "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0"
247+
exit -2
248+
rem "batch encoder %UpdateVersion%%append%.bat" --updated-from "%~f0"
221249
)
222250

223251

@@ -392,14 +420,14 @@ if defined par_updated-from (
392420
set "OUTPUTFILE=!INPUTFILE:~0,-8!.ENC.mp4"
393421
) else (
394422
set "outputfiledupe=true"
395-
set "OUTPUTFILE=!INPUTFILE:~0,-8!_!date!-!time::=-!.ENC.mp4"
423+
set "OUTPUTFILE=!INPUTFILE:~0,-8!_!date!-!timerem =-!.ENC.mp4"
396424
)
397425
) else (
398426
if not exist "!INPUTFILE:~0,-4!.ENC.mp4" (
399427
set "OUTPUTFILE=!INPUTFILE:~0,-4!.ENC.mp4"
400428
) else (
401429
set "outputfiledupe=true"
402-
set "OUTPUTFILE=!INPUTFILE:~0,-4!_!date!-!time::=-!.ENC.mp4"
430+
set "OUTPUTFILE=!INPUTFILE:~0,-4!_!date!-!timerem =-!.ENC.mp4"
403431
)
404432
)
405433

@@ -429,7 +457,7 @@ if defined par_updated-from (
429457
) do (set LEN_INP=%%g)
430458
for /F "tokens=*" %%g in ( 'powershell -Command "(^& '%LOCATION_pwsh%' -i '!OUTPUTFILE!' 2>&1 | select-String 'Duration: (.*), s').Matches.Groups[1].Value"'
431459
) do (set LEN_OUT=%%g)
432-
for /F "tokens=*" %%g in ( 'powershell -Command "[Math]::Abs(((Get-Date !LEN_INP!) - (Get-Date !LEN_OUT!)).TotalSeconds)"'
460+
for /F "tokens=*" %%g in ( 'powershell -Command "[Math]rem Abs(((Get-Date !LEN_INP!) - (Get-Date !LEN_OUT!)).TotalSeconds)"'
433461
) do (set LEN_DIFF=%%g)
434462

435463
echo Input file: !LEN_INP! - Output file: !LEN_OUT!
@@ -455,8 +483,8 @@ if defined par_updated-from (
455483
)
456484
echo %icongreen% ^| %formatend% Safely proceeding with input file recycling...
457485
if exist "!INPUTFILE!" (
458-
::delete to recycle bin
459-
powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]::DeleteFile('%CD%\!INPUTFILE!','OnlyErrorDialogs','SendToRecycleBin')"
486+
rem delete to recycle bin
487+
powershell -Command "Add-Type -AssemblyName Microsoft.VisualBasic; [Microsoft.VisualBasic.FileIO.FileSystem]rem DeleteFile('%CD%\!INPUTFILE!','OnlyErrorDialogs','SendToRecycleBin')"
460488
)
461489
)
462490
)
@@ -498,13 +526,13 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%)
498526
if /i "%par_silent%"=="true" (call:CtrlExit 2)
499527
echo The program will now restart.
500528
call:GrayPause
501-
(goto) 2>nul & "%~f0"
529+
exit -1
502530

503531
:CtrlExit
504-
if defined par_selfwrapped (
532+
if "%par_selfwrapped%"=="true" (
505533
exit 101%~1
506534
)
507-
:: go one level up (out of subroutine), then exit
535+
rem go one level up (out of subroutine), then exit
508536
(goto) 2>nul || exit /b %~1
509537

510538
:GrayPause
@@ -535,6 +563,6 @@ if %TOTAL% equ 0 (echo  No files found. %formatend%)
535563
cls
536564
set "message=%~1"
537565
if defined message (set "message=- %message% ")
538-
echo [7m Batch Encoder %CurrentVersion% %message%%formatend%
566+
echo [7m Batch Encoder %CurrentVersion% %message%%formatend% (%starttime%) (%allargs%)
539567
echo.
540568
goto:eof

0 commit comments

Comments
 (0)