Skip to content

How to implement function in Windows batch .bat

June 21, 2012

I’ve found this bat in my backups: BatchTutoFunc5.bat.

Cannot find the source url. I’m posting because canbe useful: How to implement function in Windows batch .bat

@echo off

set “aStr=Expect no changed, even if used in function”
set “var1=Expect changed”
echo.aStr before: %aStr%
echo.var1 before: %var1%
call:myGetFunc var1
echo.aStr after : %aStr%
echo.var1 after : %var1%

echo.&pause&goto:eof

::——————————————————–
::– Function section starts below here
::——————————————————–

:myGetFunc    – passing a variable by reference
SETLOCAL
set “aStr=DosTips”
( ENDLOCAL
set “%~1=%aStr%”
)
goto:eof

:myGetFunc2    – passing a variable by reference
SETLOCAL
set “aStr=DosTips”
ENDLOCAL&set “%~1=%aStr%”       &rem THIS ALSO WORKS FINE
goto:eof

From → Technology

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: