bat批处理:基础函数,带参数、带return返回值的函数,首字母大写的函数

1、Hello World

::调用函数
call :func
goto :eof

::函数
:func
	echo hello world
goto:eof

2、带参数的函数

::调用函数
call :func 5 , 10
goto :eof

::函数
:func
	echo the value of parameter 1 is %~1
	echo the value of parameter 2 is %~2
goto :eof

3-1、带返回值 、首字母大写的函数

:: 调用函数
set "button=repeatbutton"
set "components=!button!"
call :first_letter_cap components

:: 显示返回值
echo !components!
goto :eof

:: 函数
:first_letter_cap
	set letters=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
	set "str=!%~1!"
	echo !str!

	set "str1=!str:~1!"
	echo !str1!

	set "str2=!str:~,1!"
	echo !str2!

	:: /i:表示不分大小写
	for %%i in (%letters%) do if /i "%%i"=="!str2!" set str2=%%i
	set  "%~1=!str2!!str1!"
goto :eof

3-2、带返回值 、首字母大写的函数

:: 调用函数
set "button=repeatbutton"
set "components=!button!"
call :first_letter_cap components, return

:: 显示返回值
echo !return!
goto :eof

::函数
:first_letter_cap
	set letters=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
	set "str=!%~1!"
	set "str1=!str:~1!"
	set "str2=!str:~,1!"
	for %%i in (%letters%) do if /i "%%i"=="!str2!" set str2=%%i
	set  "return=!str2!!str1!"
goto :eof

函数的参数、返回值就像全局变量, 参数可当返回值使用,

4、代码上下文

@echo off
setlocal enabledelayedexpansion

...code...

endlocal
pause

参考:

如何用批处理将文本英文行首个字母改成大写
bat 脚本之 使用函数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值