Anonymous user
Call a function: Difference between revisions
Improved code example for Batch File
(Improved code example for Batch File) |
|||
Line 451:
<lang dos>
:: http://rosettacode.org/wiki/Call_a_function
:: Demonstrate the different syntax and semantics provided for calling a function.
@echo off
echo Calling myFunction1
::call a function with no arguments▼
call
echo.
echo Calling myFunction2 11 8
call:myFunction2 11 8
echo.
echo Calling myFunction3 /fi and saving the output into %%filecount%%
call:myFunction3 /fi
echo.%filecount%
echo.
echo Calling myFunction4 1 2 3 4 5
call:myFunction4 1 2 3 4 5
echo.
echo Calling myFunction5 "filename=test.file" "filepath=C:\Test Directory\"
call:myFunction5 "filename=test.file" "filepath=C:\Test Directory\"
echo.
echo Calling myFunction5 "filepath=C:\Test Directory\" "filename=test.file"
call:myFunction5 "filepath=C:\Test Directory\" "filename=test.file"
echo.
pause>nul
exit
:myFunction1
echo myFunction1 has been called.
:: Fixed number of arguments (%a% & %b%)
:myFunction2
:: Returns %a% + %b%
setlocal
set /a c=%~1+%~2
endlocal & echo %c%
goto:eof
:: Optional arguments
:myFunction3
:: Returns the amount of folders + files in the current directory
:: /fi Returns only file count
:: /fo Returns only folder count
setlocal
set count=0
if "%~1"=="" set "command=dir /b"
if "%~1"=="/fi" set "command=dir /b /A-d"
if "%~1"=="/fo" set "command=dir /b /Ad"
for /f "usebackq" %%i in (`%command%`) do set /a count+=1
endlocal & set filecount=%count%
goto:eof
::
:myFunction4
:: Returns sum of arguments
setlocal
:myFunction4loop
set sum=0
for %%i in (%*) do set /a sum+=%%i
endlocal & echo %sum%
goto:eof
:: Named Arguments (filepath=[path] & filename=[name])
:myFunction5
:: Returns the complete path based off the 2 arguments
if "%~1"=="" then goto:eof
setlocal enabledelayedexpansion
▲goto :eof
set "param=%~1"
for /l %%i in (1,1,2) do (
for /f "tokens=1,2 delims==" %%j in ("!param!") do (
if "%%j"=="filepath" (
set p=%%k
) else (
set f=%%k
)
)
set "param=%~2"
)
endlocal & echo.%p%%f%
goto:eof
</lang>
|