有好事者为windows批处理添加函数

这篇博客介绍了一种方法,通过批处理脚本来实现类似函数的功能,包括字符串操作如判断包含、字符串比较、计算长度和定位子字符串等。示例代码展示了如何使用这些自定义功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

::hello bath
@echo off
setlocal enabledelayedexpansion
:main
set var=123
if %var% lss 130 echo %var% ^< 130
set count=1
for /l %%i in (0,1,3) do (
set /a count+=%%i
echo !count!
)


call :iscontained jjjkkjj  kk
if %errorlevel% == 10 (
echo no contained 
) else (
echo contained
)


set  a=12
set  b=11
call :cmpstr %a% %b%
echo %errorlevel%


set a=123456
call :strlen %a%
echo %errorlevel%




set a=123456
set b=345
call :strstr %a% %b%
echo locate = %errorlevel%


pause






::-------------------------------判断包含-------------------------------------
goto :eof
:iscontained
set source=%1
set str=%2
set tmp=!source:%str%= !
if "%tmp%"=="%source%" (
   exit /b 10 
) else (
   exit /b 11
)
::------------------------------字符串比较--------------------------------------
goto :eof
:cmpstr
if %1 == %2 (
exit /b 0
) else if %1 lss %2 (
exit /b -1
) else if %1 gtr %2 (
exit /b 1
)
::-----------------------------字符串长度---------------------------------------
::最多可以检测长度为1024的字符串,传入包含空格字符串使用双引号包含
goto :eof
:strlen
set source=%1
set count=0
for /l %%i in (0,1,7) do (
set tmp=!source:~%%i!
if  defined tmp (
set /a count+=1
) else (
goto :strlen_end
)
)
:strlen_end
exit /b %count%
::---------------------------定位子字符串-----------------------------------------
goto :eof
:strstr
call :strlen %2
set mtch=%errorlevel%
call :iscontained %1 %2
if %errorlevel% == 10 (
exit /b -1
) else (
set mstr1=%1
set mstr2=%2
set index=0
for /l %%i in (0,1,8) do (
set m1=!mstr1:~%%i,%mtch%!
if !m1! == !mstr2! (
set index=%%i
goto :strstr_end
)
)
)
:strstr_end
exit /b %index%
::--------------------------------------------------------------------


::定位字符串
::比较 *
::复制 set a=%b%
::包含判断
::链接 %var1%%var2%
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值