- 1 year ago
Batch how to pass a parameter with space to function?
When passing a parameter with a space to a function in a batch script, you need to enclose the parameter in double quotes ("
). This ensures that the space is treated as part of the parameter value and not as a separator between multiple parameters. Here's an example:
batch@echo off
rem Define the function
:myFunction
echo Parameter: %~1
exit /b
rem Call the function with a parameter containing a space
call :myFunction "Hello World"
In this example, the myFunction
function is defined with a single parameter %~1
. When calling the function, we pass the parameter "Hello World"
enclosed in double quotes.
By enclosing the parameter in double quotes, the entire "Hello World"
string is treated as a single parameter and passed to the function. Within the function, you can access the value of the parameter using %~1
.
Remember to use the call
command when invoking the function to ensure that the script continues execution after the function call.