batch file function parameter is received as empty variable

Viewed 42

Below is a batch file.

set ORIGDIR=%CD%
set OUTPATH=d:\out
set PNGPATH=d:\result.png
set BMPPATH=e:\result.bmp

:Run
setlocal EnableDelayedExpansion
echo data is %~1
set API=%~1
set GRID=%~2
set APPOUTPATH=%OUTPATH%\!API!\!GRID!X!GRID!
md !APPOUTPATH!
echo !APPOUTPATH!
cd /D D:\App!API!\Release
app!API!.exe  --input=%~3 --output=!APPOUTPATH!/out_ --grid=!GRID!
EXIT /B 0

CALL :Run dx12, 1, %YUVPATH%
CALL :Run dx11, 2, %YUVPATH%
CALL :Run dx9, 4, %YUVPATH%
   
cd /D ORIGDIR

When it is executed, the parameter passed to Run are all empty. How to pass parameter to the function correctly

1 Answers

A label does NOT define a function, like in most programming languages, it's just a label.

The result is, after the line set BMPPATH=e:\result.bmp, the next executed line will be setlocal EnableDelayedExpansion.

To fix it, just move your CALL statements up.

set ORIGDIR=%CD%
set OUTPATH=d:\out
set PNGPATH=d:\result.png
set BMPPATH=e:\result.bmp


CALL :Run dx12 1 %YUVPATH%
CALL :Run dx11 2 %YUVPATH%
CALL :Run dx9 4 %YUVPATH%
   
cd /D ORIGDIR
exit /b

:Run
setlocal EnableDelayedExpansion
echo data is %~1
set API=%~1
set GRID=%~2
set APPOUTPATH=%OUTPATH%\!API!\!GRID!X!GRID!
md !APPOUTPATH!
echo !APPOUTPATH!
cd /D D:\App!API!\Release
app!API!.exe  --input=%~3 --output=!APPOUTPATH!/out_ --grid=!GRID!
endlocal
EXIT /B 0
Related