批处理函数库

以下是我写的一些常用的批处理函数:
@Echo off&Color a&SetLocal EnableDelayedExpansion
Call :Array test a b c d e f "g h" i j k l "34"
For /l %%a IN (0 1 %test%) DO Echo !test[%%a]!
Pause

:Array
Rem 将函数调用时的所有参数(除第一个外)处理成一个参数数组
Rem 参数ArrayName:要得到的数组的数组名
Rem 返回值%_Name%[i]:由第一个之后的参数组成的数组
Rem 返回值%_Name%:得到的数组的最大下标
Set _Name=%~1
Set _Index=-1
:BeginArray
For %%a IN (%*) DO (
Set %_Name%[!_Index!]=%%a
Set /a _Index+=1
)
Set /a %_Name%=%_Index%-1
:EndArray
Set %_Name%[-1]=
Set _Name=
Set _Index=
Goto :EOF


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansion
Call :GetArgs a b c d e f "g h" i j k l "34"
For /l %%a IN (0 1 %Args%) DO Echo !Args[%%a]!
Pause
Exit

:GetArgs
Rem 将函数调用的所有参数处理成一个参数数组
Rem 返回值Args[i]:参数数组,此数组总是以Args命名
Rem 返回值Args:参数数组的最大下标
Set _Index=0
:BeginGetArgs
For %%a IN (%*) DO (
Set Args[!_Index!]=%%a
Set /a _Index+=1
)
Set /a Args=%_Index%-1
:EndGetArgs
Set _Index=
Goto :EOF


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansion
Call :IsLeap 2000
Echo %IsLeap%
Pause

:IsLeap
Rem 判断某一年是不是润年
Rem 参数_Year:要判断的年份
Rem 返回值IsLeap:润年则为True,平年则为False
Set _Year=%~1
:BeginIsLeap
Rem 预设为False
Set IsLeap=False
Set /a _Mod1=%_Year%%%4
Set /a _Mod2=%_year%%%100
Set /a _Mod3=%_Year%%%400
Set /a _Mod4=%_Year%%%128
Rem 能被四整除为润年
If %_Mod1% equ 0 (
Set IsLeap=True
Rem 排除能被100整除但不能被400整除的年份
If %_Mod2% equ 0 (
If %_Mod3% neq 0 Set IsLeap=False
)
Rem 每128年废一润年(来自百科)
If %_Mod4% equ 0 Set IsLeap=False
)
:EndIsLeap
Set _Year=
Set _Mod1=
Set _Mod2=
Set _Mod3=
Set _Mod4=
Goto :Eof


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansion
Call :Length "Hello world"
Echo %Length%
Pause

:Length
Rem 计算字符串长度
Rem 参数_String:要进行测宽的字符串
Rem 返回值Length:得到的字符串长度
Set _String=%~1
Set _Index=0
:BeginLength
If NOT "!_String:~%_Index%,1!"=="" (
Set /a _Index+=1
Goto :BeginLength
)
Set /a Length=%_Index%
:EndLength
Set _String=
Set _Index=
Goto :Eof


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansion
For /l %%a IN (1 1 30) DO (
Call :Random 1 20
Echo !Rnd!
)
Pause

:Random
Rem 返回某个范围内的随机数,返回的结果总是以Rnd命名
Rem 参数_Lower:范围的下限
Rem 参数_Upper:范围的上限
Rem 返回值Rnd:_Lower-_Upper范围内的随机数
Set _Lower=%~1
Set _Upper=%~2
:BeginRandom
Set /a _Max=(1024*1024*1024*2-1)/32767
Set /a _Len=%_Upper%-%_Lower%+1
If %_Len% gtr %_Max% (
Set /a Rnd=%_Lower%-1            
Goto :EOF
)
Set /a Rnd=%_Lower%+%Random%*%_Len%/32767
:EndRandom
Set _Lower=
Set _Upper=
Goto :EOF


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansion
Call :Range test 3 18
For /l %%a IN (0 1 %test%) DO Echo !test[%%a]!
Pause

:Range
Rem 返回一个整数序列的数组
Rem 参数_Name:整数序列数组的数组名
Rem 参数_From:整数序列的起始值
Rem 参数_To:整数序列的终止值
Rem 参数_Step:整数序列中返回的数值之间的公差,此参数可选
Set _Name=%~1
Set _From=%~2
Set _To=%~3
If "%~4"=="" (
Set _Step=1
) Else (
Set _Step=%~4
)
Set _Index=0
:BeginRange
For /l %%a IN (%_From% %_Step% %_To%) DO (
Set %_Name%[!_Index!]=%%a
Set /a _Index+=1
)
Set /a %_Name%=%_Index%-1
:EndRange
Set _Name=
Set _From=
Set _To=
Set _Step=
Set _Index=
Goto :EOF


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansion
Call :Reverse "Hello world"
Echo %Reverse%
Pause

:Reverse
Rem 反转字符串,结果总是保存在Reverse变量中
Rem 参数_String:要进行反转的字符串
Rem 返回值Reverse:反转后的字符串值
Set _String=%~1
Set _Index=0
:BeginReverse
If NOT "!_String:~%_Index%,1!"=="" (
Set Reverse=!_String:~%_Index%,1!%Reverse%
Set /a _Index+=1
Goto :BeginReverse
)
:EndReverse
Set _String=
Set _Index=
Goto :Eof


============================================
@ Echo off&Color a&SetLocal EnableDelayedExpansion
Set /p Items=请输入要进行解析的项的字符串:
Set /p Delimter=请输入分隔符(必须是单字符):
Set /p ArrayName=请输入字符串分解后得到的数组的数组名:
Call :Split "%Items%" "%Delimter%" "%ArrayName%"
For /l %%a IN (0 1 !%ArrayName%!) DO Echo !%ArrayName%[%%a]!
Pause
Exit

:Split
Rem 对字符串用指定的分隔符分隔,计算分隔得到的项数
Rem 参数_ItemsStr:要进行解析的字符串
Rem 参数_DelimterChar:单字符的分隔符
Rem 参数_Name:字符串分解后得到的数组的数组名
Rem 返回值%_Name%[i]:保存分解得到的数组的第i项,索引从零开始
Rem 返回值%_Name%:保存数组的最大索引
Set _ItemsStr=%~1
Set _DelimterChar=%~2
Set _Name=%~3
Set _n=1
Set _Index=0
:BeginSplit
For /f "tokens=%_n% delims=%_DelimterChar%" %%a IN ("%_ItemsStr%") DO (
If NOT "%%a"=="" (
Set %_Name%[%_Index%]=%%a
Set /a _n+=1
Set /a _Index+=1
Goto :BeginSplit
)
)
:EndSplit
Set /a %_Name%=%_Index%-1
Set _n=
Set _Index=
Set _ItemsStr=
Set _DelimterChar=
Set _Name=
Goto :EOF


============================================
@ Echo off&Color a&SetLocal EnableDelayedExpansion
Call :ToChars "hello world天龙八部" "test"
For /l %%a IN (0 1 %test%) DO Echo "!test[%%a]!"
Pause
Exit

:ToChars
Rem 将字符串分解成单个字符的数组
Rem 参数_String:要进行分解的字符串
Rem 参数_Name:生成的字符数组的数组名
Rem 返回值%_Name%[i]:分解得到的字符数组
Rem 返回值%_Name%:分解得到的字符数组的最大下标
Set _String=%~1
Set _Name=%~2
Set _Index=0
:BeginToChars
If "!_String:~%_Index%,1!"=="" (
Goto :EndToChars
) Else (
Set %_Name%[%_Index%]=!_String:~%_Index%,1!
Set /a _Index+=1
Goto :BeginToChars
)
:EndToChars
Set /a %_Name%=%_Index%-1
Set _String=
Set _Name=
Set _Index=
Goto :EOF




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值