optimize batch script to run faster, more clear structure and error free but keep all logic:
@ECHO OFF
setlocal enableDelayedExpansion
chcp 65001

rem Set input and output directories
set "_in_dir=%8"
set "_out_dir=%4"
set "gpuid=0"
if "%2" LSS "7" (set "gpuid=%2")
set TGArgs=%1 %2 %3 %4 %5 %6 %7 %8 %9
for /L %%i in (0,1,8) do @shift
set TGArgs=%TGArgs% %1 %2 %3 %4 %5 %6 %7 %8 %9
set "tg8x=%1"
set "extension=jpg"
if /I "%3" NEQ "jpg" (set "extension=png")

rem Verify if input and output directories exist
if not exist "%_in_dir%" (
echo Error: Input directory "%_in_dir%" not found.
goto :exit4
)
if not exist "%_out_dir%" (
echo Error: Output directory "%_out_dir%" not found.
goto :exit4
)

rem Set model and corresponding executable based on tg8x value using string replacement
echo.%tg8x% | find /I "TG8XHD2AF">nul && (set "_model=TecoGAN8X_af1_225K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xhd2AF=TecoGAN8X\tg8x_dml.exe%") && goto :tg8xhd2
echo.%tg8x% | find /I "TG8XHDAF">nul && (set "_model=TecoGAN8X_af1_225K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8XhdAF=TecoGAN8X\tg8x_dml.exe%") && goto :tg8xhd

echo.%tg8x% | find /I "TG8XHD2RAF">nul && (set "_model=TecoGAN8X_af1_225K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xhd2rAF=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft2
echo.%tg8x% | find /I "TG8XHDRAF">nul && (set "_model=TecoGAN8X_af1_225K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8XhdrAF=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft

echo.%tg8x% | find /I "TG8XFT2AF">nul && (set "_model=TecoGAN8X_AF1b_253K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xft2AF=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft2
echo.%tg8x% | find /I "TG8XFTAF">nul && (set "_model=TecoGAN8X_AF1b_253K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8XftAF=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft

echo.%tg8x% | find /I "TG8X2RAF">nul && (set "_model=TecoGAN8X_af4_75K@6t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8X2rAF=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft2
echo.%tg8x% | find /I "TG8XRAF">nul && (set "_model=TecoGAN8X_af4_75K@6t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8XrAF=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft

echo.%tg8x% | find /I "TG8XFT2">nul && (set "_model=TecoGAN8X_GK3c_93K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xft2=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft2
echo.%tg8x% | find /I "TG8XFT">nul && (set "_model=TecoGAN8X_GK3c_93K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xft=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft

echo.%tg8x% | find /I "TG8XHD2">nul && (set "_model=TecoGAN8X_gk3_90K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xhd2=TecoGAN8X\tg8x_dml.exe%") && goto :tg8xhd2
echo.%tg8x% | find /I "TG8XHD">nul && (set "_model=TecoGAN8X_gk3_90K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xhd=TecoGAN8X\tg8x_dml.exe%") && goto :tg8xhd

echo.%tg8x% | find /I "TG8XHD2R">nul && (set "_model=TecoGAN8X_gk3_90K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xhd2r=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft2
echo.%tg8x% | find /I "TG8XHDR">nul && (set "_model=TecoGAN8X_gk3_90K@7t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xhdr=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft

echo.%tg8x% | find /I "TG8X2R">nul && (set "_model=TecoGAN8X_gk4_245K@6t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8X2r=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft2
echo.%tg8x% | find /I "TG8XR">nul && (set "_model=TecoGAN8X_gk4_245K@6t.pth") && (set "_tg8x_dml=%tg8x:TG-MODEL\TG8Xr=TecoGAN8X\tg8xR_dml.exe%") && goto :tg8xft

rem If no TG8X models found, run tecogan.exe
if errorlevel 1 (
%WINDIR%\system32\cmd.exe /c "tecogan.exe %TGArgs%"
goto :exit4
)

:tg8xhd
set /a resume=0
set /a num_in=0
for %%A in (%_in_dir%*.png) do (if exist %%A (set /a num_in+=1))
set /a num_out=0

:run_hd
set /a max_out=%num_in%-9
if %num_out% gtr %max_out% set /a num_out=%max_out%
%WINDIR%\system32\cmd.exe /c "%_tg8x_dml% --in_dir %_in_dir% --out_dir %_out_dir% --start_index %num_out% --model %_model% --gpu_id %gpuid%"

set /a num_out=0
for %%A in (%_out_dir%*.png) do (if exist %%A (set /a num_out+=1))

if %num_in%==%num_out% goto :exit
set /a resume+=1
if %resume% leq 55 (
goto :run_hd
)
:exit
..\offset.exe %_out_dir%
goto :exit4

:tg8xhd2
set /a num_in=0
for %%A in (%_in_dir%*.png) do (if exist %%A (set /a num_in+=1))
set /a num_out=0
set /a resume=0

:run_hd2
set /a max_out=%num_in%-9
if %num_out% gtr %max_out% set /a num_out=%max_out%
%WINDIR%\system32\cmd.exe /c "%_tg8x_dml% --in_dir %_in_dir% --out_dir %_out_dir% --start_index %num_out% --model %_model% --gpu_id %gpuid%"

set /a num_out=0
for %%A in (%_out_dir%*.png) do (if exist %%A (set /a num_out+=1))

if %num_in%==%num_out% goto :exit2
set /a resume+=1
if %resume% leq 55 (
goto :run_hd2
)
:exit2
..\offset.exe %_out_dir%
goto :exit4

:tg8xft2
set /a resume=0
set /a num_in=0
for %%A in (%_in_dir%*.%extension%) do (if exist %%A (set /a num_in+=1))
set /a num_out=0
set order=both

:run_ft2
@echo on
%_tg8x_dml% %_in_dir% %_out_dir% %num_out% %_model% %gpuid% %order% %extension%
@echo off

set /a num_out=0
for %%A in (%_out_dir%*.%extension%) do (if exist %%A (set /a num_out+=1))

if %num_out% geq %num_in% goto :exit3
if exist %_out_dir%tmp set order=reverse

set /a resume+=1
if %resume% leq 999 (
goto :run_ft2
)
:exit3
..\offset.exe %_out_dir%
goto :exit4

:tg8xft
set /a resume=0
set /a num_in=0
for %%A in (%_in_dir%*.%extension%) do (if exist %%A (set /a num_in+=1))
set /a num_out=0
set order=both

:run_ft
@echo on
%_tg8x_dml% %_in_dir% %_out_dir% %num_out% %_model% %gpuid% %order% %extension%
@echo off

set /a num_out=0
for %%A in (%_out_dir%*.%extension%) do (if exist %%A (set /a num_out+=1))

if %num_out% geq %num_in% goto :exit1
if exist %_out_dir%tmp set order=reverse

set /a resume+=1
if %resume% leq 999 (
goto :run_ft
)
:exit1
..\offset.exe %_out_dir%

:exit4
exit /b