以下程序是 ms dos 的批处理文件,@echo off & setlocal enabledelayedexpansion::%1 -- String::%2 -- Filename::%3 -- Environment variable::%4 -- Start address::%5 -- get length::%6 -- which line that include string to get,default = 1 (first lin
来源:学生作业帮助网 编辑:六六作业网 时间:2025/01/24 21:23:42
以下程序是 ms dos 的批处理文件,@echo off & setlocal enabledelayedexpansion::%1 -- String::%2 -- Filename::%3 -- Environment variable::%4 -- Start address::%5 -- get length::%6 -- which line that include string to get,default = 1 (first lin
以下程序是 ms dos 的批处理文件,
@echo off & setlocal enabledelayedexpansion::%1 -- String::%2 -- Filename::%3 -- Environment variable::%4 -- Start address::%5 -- get length::%6 -- which line that include string to get,default = 1 (first line include %1) ,-1 means last one if %5@==@ goto usageSet %3=Set Number_Of_Found_Word=-1if "%6"=="" ( set Expect_Found_Time=1 ) ELSE ( set Expect_Found_Time=%6 )For /F "skip=1 usebackq delims=" %%a IN (`find "%~1" %2`) Do call :set_env "%%a" %*endlocalcall out.batif exist out.bat del out.batgoto end:set_envshift::skip first line ,it is not content of target fileset /A Number_Of_Found_Word=%Number_Of_Found_Word%+1if not %Number_Of_Found_Word%==%Expect_Found_Time% if not %Expect_Found_Time%==-1 goto :eofset %3=%~0echo set %3=!%3:%4,%5!>out.batgoto :eof:usageEcho -Usage:Echo - Wjenv string filename environment_variable start_addr Length [times]Echo -Echo - Search "string" in "filename" ,Echo - and get "Length" chars from the line add offset "Start_addr"Echo - Then set to environment "environment_variable" .Echo -Echo - [times] is option argument to specified that get from which line that Echo - include "string" .Echo - 1 means first time ,-1 means last time.goto :eof:end
以下程序是 ms dos 的批处理文件,@echo off & setlocal enabledelayedexpansion::%1 -- String::%2 -- Filename::%3 -- Environment variable::%4 -- Start address::%5 -- get length::%6 -- which line that include string to get,default = 1 (first lin
@echo off & setlocal enabledelayedexpansion //关闭显示并启用变量延迟,启用本地变量
:: %1 -- String //字符串 //::标注 %1~%6是调用程序外部的参数
:: %2 -- Filename //文件名
:: %3 -- Environment variable //环境变量
:: %4 -- Start address //开始地址
:: %5 -- get length //获取长度
:: %6 -- which line that include string to get, default = 1 (first line include %1) ,-1 means last one //获取字符串的行,默认为1,-1表示最后一行
if %5@==@ goto usage //如果%5参数为空,就跳转到:usage处运行
Set %3= //设置%3为空字符
Set Number_Of_Found_Word=-1 //设置Number_Of_Found_Word为-1
if "%6"=="" ( set Expect_Found_Time=1 ) ELSE ( set Expect_Found_Time=%6 )
//如果%6为空,则Expect_Found_Time=1,否则Expect_Found_Time为%6的值
For /F "skip=1 usebackq delims=" %%a IN (`find "%~1" %2`) Do call :set_env "%%a" %*
//for语句你需要好好了解一下,功能很强,需要多练.skip=1—跳过第一行进行操作,usebackq是增强型参数,没有它的话find语句只需用单引号括起来,用了之后需用`;delims是分隔符,默认是空格什么的.find "%~1" %2是遍历的数据集,在文件名为%2的文件中查找"%~1"这个字符串,如果查到了就执行do语句,这里是调用本程序的:set_env语句块,%*指从第一个参数到最后一个参数
endlocal //结束本地变量,这个概念意思比如前面我定义了t这个变量,后来我再setlocal和endlocal里面重新定义了t,最后我还想使用先前定义的t,那么endlocal之后用的就是原来的了
call out.bat //调用out.bat批处理
if exist out.bat del out.bat //如果存在out.bat就删除它
goto end //跳转到程序尾部,结束程序
:set_env //名为set_env的语句块
shift //这个命令还没用过,只知道一点概念,就不说了
:: skip first line , it is not content of target file //注释
set /A Number_Of_Found_Word=%Number_Of_Found_Word%+1 //set /a表示进行数值运算,所以后面能用Number_Of_Found_Word=%Number_Of_Found_Word%+1,将变量Number_Of_Found_Word加1赋予自身,类似与其他语言的i++之类的
if not %Number_Of_Found_Word%==%Expect_Found_Time% if not %Expect_Found_Time%==-1
goto :eof
//如果变量不等于Expect_Found_Time,而且Expect_Found_Time不等于-1,就结束程序
set %3=%~0 //将本程序名赋予参数%3,0表示本程序名称
echo set %3=!%3:%4,%5! >out.bat //这里用到了变量延迟,!%3:%4,%5!的意思是截取字符串,重定向传给out.bat
goto :eof
//下面的echo就不说了,就是显示功能
:usage
Echo -Usage:
Echo - Wjenv string filename environment_variable start_addr Length [times]
Echo -
Echo - Search "string" in "filename" ,
Echo - and get "Length" chars from the line add offset "Start_addr"
Echo - Then set to environment "environment_variable" .
Echo -
Echo - [times] is option argument to specified that get from which line that
Echo - include "string" .
Echo - 1 means first time , -1 means last time.
goto :eof
:end