Windows Batch Functions


Place these functions at the top of your batch file. Before the functions begin, use a goto, to jump over the functions and get to your main program.

GOTO :MAIN 


Inside your main program, to call a function, just say...

set PARAMETER1=test1
set PARAMETER1=test2
call :FUNCTIONNAME $PARAMETER1% %PARAMETER2% RETURN_VAR_NAME
echo %RETURN_VAR_NAME% 


Inside your function, you can return a value if you want to

set %~3=my_output
goto :eof  


Inside your function, you can throw an error if you want to

exit /b 1



After the function returns, you can test if the function threw an error

if %ERRORLEVEL% GTR 0 ( 

Functions

Delay Program

:DelayMe
SET delaytime=%1
:: Wait XXX miliseconds before continuing
echo Delay %delaytime%ms
PING 1.1.1.1 -n 1 -w %delaytime% >NUL
GOTO :EOF

To Call the Function

:: Delay for 1.5 seconds
call :DelayMe 1500

Check if File Exists

:Exists
set testfilename=%1
if not exist %testfilename% (
  echo ERROR:  %testfilename% not found.
  exit /b 1 
)
GOTO :EOF

To use it...

call :Exists testfile.hex
if %ERRORLEVEL% GTR 0 goto :ABORTPROGRAM    

Remove quotes from a string.

:DeQuote
SET _DeQuoteVar=%1
CALL SET _DeQuoteString=%%!_DeQuoteVar!%%
IF [!_DeQuoteString:~0^,1!]==[^"] (
IF [!_DeQuoteString:~-1!]==[^"] (
SET _DeQuoteString=!_DeQuoteString:~1,-1!
) ELSE (GOTO :EOF)
) ELSE (GOTO :EOF)
SET !_DeQuoteVar!=!_DeQuoteString!
SET _DeQuoteVar=
SET _DeQuoteString=
GOTO :EOF

To call it

:: Note absense of % symbols on parameter to dequote
:: Note for this to work, you must EnableDelayedExpansion
SETLOCAL EnableDelayedExpansion
SET QueryString="This is a test"
echo %QueryString%
CALL :dequote QueryString 
echo %QueryString%

Output

"This is a test"
This is a test

Return a Value from a Function

You can return a value from a function by passing the variable by reference in your call

call :FunctionName ReturnVarName
echo Function Returned: %ReturnVarName%

In the function, you just say

set %~1=PopcornFiesta
goto :EOF

Example Program showing passing value back

@echo off

GOTO :MAIN

rem ************ Functions *************

:TestFunc
set orig=%1
set %~2=%orig%_PopcornFiesta
goto :eof

rem ************ Main Program *************
:MAIN
set param_to_function=LetItBeA
echo %param_to_function%
call :TestFunc %param_to_function% return_value
echo %return_value%

This would output

LetItBeA
LetItBeA_PopcornFiesta

Note the absense of % symbols on the return_value parameter when we call the function. We pass the name of the return variable to the function.


Execute an external Program and put output into variable

:ExecuteCommand
:: Run an external program and pass back the output of that program in a variable
:: this would probably be some specialized command,but for this example, we'll use 
:: the directory command.
:: This will pass back the last line of output, so it discards all but last line
:: If the command throws an error, no output will be given

set param=error
for /f "tokens=*" %%a in ( 
'dir' 
) do ( 
set param=%%a 
)

set %~1=%param%
goto :eof

To Call it

call :ExecuteCommand return_value
echo %return_value%