批处理函数库

来源:互联网 发布:linux列出文件夹大小 编辑:程序博客网 时间:2024/06/11 08:11
以下是我写的一些常用的批处理函数:
@Echo off&Color a&SetLocal EnableDelayedExpansionCall :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:ArrayRem 将函数调用时的所有参数(除第一个外)处理成一个参数数组Rem 参数ArrayName:要得到的数组的数组名Rem 返回值%_Name%[i]:由第一个之后的参数组成的数组Rem 返回值%_Name%:得到的数组的最大下标Set _Name=%~1Set _Index=-1:BeginArrayFor %%a IN (%*) DO (Set %_Name%[!_Index!]=%%aSet /a _Index+=1)Set /a %_Name%=%_Index%-1:EndArraySet %_Name%[-1]=Set _Name=Set _Index=Goto :EOF


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansionCall :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]!PauseExit:GetArgsRem 将函数调用的所有参数处理成一个参数数组Rem 返回值Args[i]:参数数组,此数组总是以Args命名Rem 返回值Args:参数数组的最大下标Set _Index=0:BeginGetArgsFor %%a IN (%*) DO (Set Args[!_Index!]=%%aSet /a _Index+=1)Set /a Args=%_Index%-1:EndGetArgsSet _Index=Goto :EOF


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


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansionCall :Length "Hello world"Echo %Length%Pause:LengthRem 计算字符串长度Rem 参数_String:要进行测宽的字符串Rem 返回值Length:得到的字符串长度Set _String=%~1Set _Index=0:BeginLengthIf NOT "!_String:~%_Index%,1!"=="" (Set /a _Index+=1Goto :BeginLength)Set /a Length=%_Index%:EndLengthSet _String=Set _Index=Goto :Eof


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansionFor /l %%a IN (1 1 30) DO (Call :Random 1 20Echo !Rnd!)Pause:RandomRem 返回某个范围内的随机数,返回的结果总是以Rnd命名Rem 参数_Lower:范围的下限Rem 参数_Upper:范围的上限Rem 返回值Rnd:_Lower-_Upper范围内的随机数Set _Lower=%~1Set _Upper=%~2:BeginRandomSet /a _Max=(1024*1024*1024*2-1)/32767Set /a _Len=%_Upper%-%_Lower%+1If %_Len% gtr %_Max% (Set /a Rnd=%_Lower%-1            Goto :EOF)Set /a Rnd=%_Lower%+%Random%*%_Len%/32767:EndRandomSet _Lower=Set _Upper=Goto :EOF


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansionCall :Range test 3 18For /l %%a IN (0 1 %test%) DO Echo !test[%%a]!Pause:RangeRem 返回一个整数序列的数组Rem 参数_Name:整数序列数组的数组名Rem 参数_From:整数序列的起始值Rem 参数_To:整数序列的终止值Rem 参数_Step:整数序列中返回的数值之间的公差,此参数可选Set _Name=%~1Set _From=%~2Set _To=%~3If "%~4"=="" (Set _Step=1) Else (Set _Step=%~4)Set _Index=0:BeginRangeFor /l %%a IN (%_From% %_Step% %_To%) DO (Set %_Name%[!_Index!]=%%aSet /a _Index+=1)Set /a %_Name%=%_Index%-1:EndRangeSet _Name=Set _From=Set _To=Set _Step=Set _Index=Goto :EOF


============================================
@Echo off&Color a&SetLocal EnableDelayedExpansionCall :Reverse "Hello world"Echo %Reverse%Pause:ReverseRem 反转字符串,结果总是保存在Reverse变量中Rem 参数_String:要进行反转的字符串Rem 返回值Reverse:反转后的字符串值Set _String=%~1Set _Index=0:BeginReverseIf NOT "!_String:~%_Index%,1!"=="" (Set Reverse=!_String:~%_Index%,1!%Reverse%Set /a _Index+=1Goto :BeginReverse):EndReverseSet _String=Set _Index=Goto :Eof


============================================
@ Echo off&Color a&SetLocal EnableDelayedExpansionSet /p Items=请输入要进行解析的项的字符串:Set /p Delimter=请输入分隔符(必须是单字符):Set /p ArrayName=请输入字符串分解后得到的数组的数组名:Call :Split "%Items%" "%Delimter%" "%ArrayName%"For /l %%a IN (0 1 !%ArrayName%!) DO Echo !%ArrayName%[%%a]!PauseExit:SplitRem 对字符串用指定的分隔符分隔,计算分隔得到的项数Rem 参数_ItemsStr:要进行解析的字符串Rem 参数_DelimterChar:单字符的分隔符Rem 参数_Name:字符串分解后得到的数组的数组名Rem 返回值%_Name%[i]:保存分解得到的数组的第i项,索引从零开始Rem 返回值%_Name%:保存数组的最大索引Set _ItemsStr=%~1Set _DelimterChar=%~2Set _Name=%~3Set _n=1Set _Index=0:BeginSplitFor /f "tokens=%_n% delims=%_DelimterChar%" %%a IN ("%_ItemsStr%") DO (If NOT "%%a"=="" (Set %_Name%[%_Index%]=%%aSet /a _n+=1Set /a _Index+=1Goto :BeginSplit)):EndSplitSet /a %_Name%=%_Index%-1Set _n=Set _Index=Set _ItemsStr=Set _DelimterChar=Set _Name=Goto :EOF


============================================
@ Echo off&Color a&SetLocal EnableDelayedExpansionCall :ToChars "hello world天龙八部" "test"For /l %%a IN (0 1 %test%) DO Echo "!test[%%a]!"PauseExit:ToCharsRem 将字符串分解成单个字符的数组Rem 参数_String:要进行分解的字符串Rem 参数_Name:生成的字符数组的数组名Rem 返回值%_Name%[i]:分解得到的字符数组Rem 返回值%_Name%:分解得到的字符数组的最大下标Set _String=%~1Set _Name=%~2Set _Index=0:BeginToCharsIf "!_String:~%_Index%,1!"=="" (Goto :EndToChars) Else (Set %_Name%[%_Index%]=!_String:~%_Index%,1!Set /a _Index+=1Goto :BeginToChars):EndToCharsSet /a %_Name%=%_Index%-1Set _String=Set _Name=Set _Index=Goto :EOF