总有别人不曾走过的路,总有别人不曾目睹的风景......
有关Batch的几个应用
上一篇 /
下一篇 2012-06-08 16:22:27
/ 个人分类:Batch
1. 搜索文件中包含特殊字符的节。
由于没有查找到
Batch对于字符串的操作,故采取echo与find的方法,来判断节里是否包含所查找的特殊字符。
echo %variable% | find string
|
此时通过判定errorlevel的值,可间接判断变量variable中是否包含所要查找的字符string。
2. 获取主机当前时间。
由于在利用time命令获取时间时,上午( 8:30)和下午(20:30)显示不同,因为不满10小时时,数字前面会有一个空格,处理起来会不方便。利用如下方法解决该问题。
if "%time:~0,1%"==" " (echo %date:~12,2%-0%time:~1,1%.txt) else (echo %date:~12,2%-%time:~0,2%.txt) |
3. 获取所输入的字符串长度。
@echo off & setlocal enabledelayedexpansion :loop cls & (set sss=) & set /p sss=请输入一个字符串: (call :strLen len "!sss!") echo "!sss!"&echo\&echo 长度:!len! pause & goto loop exit /b &rem end of main program rem 调用时, 字符串值必须加上双引号对, 以适应串中含有空格的情况; 不能用字符串变量名作为参数 :strLen lenVarName "strValue" (set str=%2) if "!str!" equ """" (set %1=0)&exit /b (set /a lower=0, upper=1) :searchUpper for /f "tokens=1" %%u in ("!upper!") do if "!str:~%%u,1!" equ "" goto binSearch (set /a upper*=2) goto searchUpper :binSearch set /a "t=(lower+upper)/2" if !t! equ !lower! (set /a %1=t-1) & exit /b for /f "tokens=1" %%t in ("!t!") do if "!str:~%%t,1!" equ "" (set /a upper=t) else (set /a lower=t) goto binSearch exit /b
|
收藏
举报
TAG:
Batch