(dos)使用批处理

来源:互联网 发布:淘宝 虚拟交易 编辑:程序博客网 时间:2024/05/01 12:03

使用批处理

目录

5  用批处理文件组织命令
6 具有判断功能的批处理文件
7 使用批处理文件
8 系统自动执行autoexec.bat

内容

5 用批处理文件组织命令

建立简单的批处理
用户定义的以BAT为扩展名的文件,由一个或多个DOS命令组成的文件.当在DOS提示符下输入批处理文件名及回车时,DOS按照文件中命令的顺序执行每个命令.

copy con hqx.bat

date

time

^Z

 

关闭命令行显示
echo off

cls

date

time

这样还是会显示echo off

可以在echo off前加上@

中止批处理
ctrl+c

terminate batch job(Y/N)

Y是中止整个批处理

N是中止当前条

批处理文件中常用的三个命令
echo
一是禁止显示命令名称
echo off

@echo off

二如果指定的文本不是OFF或ON,则将文本作为信息显示在屏幕上
@echo off

date

echo  this is what i mean

time

 

三用echo显示空行(win9x,win2000无效)
这是错的
@echo off

echo aaaa0

echo.

echo bbbb

echo

echo

echo done

多种技术
@echo off

echo aaaa

echo alt-255

echo bbbb

echo alt-255

echo alt-255

echo done

rem
加入注释

@echo off

date

rem  this is what i mean

time

pause
暂停后续命令

@echo off

echo place a formatted disk in drive a

pause

copy *.bat a:

 


6 具有判断功能的批处理文件


IF命令
IF   条件表达式   DOS命令

三种条件类型

IF EXIST 测试文件是否存在:IF EXIST AUTOEXEC。BAT TYPE AUTOEXEC。BAT

IF ERRORLEVEL 测试前一个命令的返回值:IF ERRORLEVEL 1 GOTO SUCCESS

IF STR==STR 测试两字符串是否相等:IF ‘%1’==‘A’GOTO ASCENDING

用IF EXIST 命令测试文件存在
IF EXIST c:/h.bat type c:/h.bat

如果存在hqx.bat 那么显示hqx.bat

NOT 操作符
实例显示文件fileexit.bat
@echo off

IF EXIST C:/AUTOEXEC.BAT ECHO Found AUTOEXEC.BAT

IF EXIST C:/CONFIG.SYS ECHO Found CONFIG.SYS

IF NOT EXIST C:/AUTOEXEC.BAT ECHO NOT Found AUTOEXEC.BAT

IF EXIST C:/CONFIG.SYS ECHO NOT Found CONFIG.SYS

实例检查文件
@echo off

IF EXIST C:/SYSTEM.1ST ECHO Found SYSTEM.1ST

IF NOT EXIST C:/SYSTEM.1ST COPY C:/WINDOWS/SYSTEM.DAT C:/system.1st

IF ERRORLEVEL命令检查前一个命令是否成功 
指示前一个命令的出口情况

用IF命令比较两个字符串
字符串序列可以是一个字符一个词一段话或空字符

IF STRING1= =STRING2 COMMAND

向批处理文件传递参数hqxcolor.bat
DOS将批处理文件名传给%0

分配空字符串给%1%2%3%4%5%6%7%8%9

@echo off

if ‘%1’==‘blue’ echo the color is blue

if ‘%1’==‘red’ echo the color is red

if ‘%1’==‘‘ echo the color is empty

批处理实例
判断是否存在hqxexit.bat
@ECHO OFF

IF ‘%1’==‘‘ ECHO YOUR MUST WRITE THE FILE NAME

if NOT ‘%1’==‘‘ IF EXIST %1 TYPE %1

if NOT ‘%1’==‘‘ IF NOT EXIST %1 ECHO %1 DOES NOT EXIST

判断是否有参数hqxnum.bat
@ECHO OFF

IF ‘%1’==‘‘ ECHO %%1 has no value

if NOT ‘%1’==‘‘ echo %%1 is %1

高级概念
可以使用命名参数来读取DOS的环境项hqxv.bat
@ECHO OFF

IF ‘%PATH%’==‘‘ ECHO PATH IS UNDERFINED

IF NOT ‘%PATH%’==‘‘ ECHO CUURENT PATH IS %PATH%

IF NOT ‘%PROMPT%’== ‘‘ ECHO CURRENT PROMPT IS %PROMPT%

用DOS6的CHOICE命令hqxchoic.bat
echo off

:loop

rem do the procedure

cls

 echo       main menu

echo. 

Rem display empty line

echo   a  display directory listing

echo   b  display memory use

echo   c  display config.sys

echo   q  quit to dos

echo.

choice /c:abcq enter choice:

if not errorlevel 1 goto done

if errorlevel 1 if not errorlevel 2 dir /p

if errorlevel 2 if not errorlevel 3 mem /debug /page

if errorlevel 3 if not errorlevel 4 more < /config.sys

if errorlevel 4 goto done

pause

goto loop

:done

choice
choice /c:yn指定是否提示用户用Y或N开关,默认值

上题abcq为choice的四种出口状态

可以用errorlevel来判断见下

如果大于或等于1且不是2那就能确定是1

如果大于或等于2且不是3那就能确定是2

如果大于或等于3且不是4那就能确定是3

如果大于或等于4且不是5那就能确定是4

choice /c:abcd/n 将不再显示这几个字符

choice /c:abcd/s将显示区分小写字符


7 使用批处理文件


7.1用GOTO命令跳转
解释
@ECHO OFF

VER

GOTO DONE

DIR

:DONE

REM 有标号

当DOS遇到冒号时,认为本行冒号后面的内容为标号而非DOS命令

DOS不区分标号的大小写

DOS只使用标号的前八个字符.如果在文件中有两个标号的前八个字符相同,则DOS无法区别两个标号,只会跳到最近的标号处

例子:hqxcom.bat,注意大小写在标号中是有区别的
@ECHO OFF

IF ‘%1’==‘CLS’ GOTO CLS

IF ‘%1’==‘DIR’ GOTO DIR

IF ‘%1’==‘VER’ GOTO VER

GOTO DONE

:CLS

ECHO CLS COMMAND

ECHO FUNCTION:CLEARS THE SCREEN DISPLAY

ECHO FORMAT:CLS

GOTO DONE

:DIR

ECHO DIR COMMAND

ECHO FUNCTION:displays a directory listing

ECHO FORMAT:dir [file name] [switches]

GOTO DONE

:ver

echo ver command

echo function:displays dos version number

echo format:ver

goto done

:done

7.2对一组文件重复执行某个命令hqxmore.bat
@echo off

for %%f in (*.bat) do type %%f

 

%%f是for命令变量.

如果不是在批处理命令中执行在DOS中用如下格式

for %f in (*.bat) do type %f

7.3接收多于九个的批参数
hshift.dat
@echo off

echo %0 %1 %2 %3 %4 %5 %6 %7 %8 %9

shift

echo %0 %1 %2 %3 %4 %5 %6 %7 %8 %9

可以看出一个shift命令,使得所有的参数往前移动了一个位置

可以显示多个参数的命令hqxshift.bat
@echo off

:loop

if ‘%1’==’’ goto done

echo %1

shift

goto loop

:done

%1如果不为空就echo %1显示参数,并用shift退一个参数,这样可以把多个参数显示出来,不管有多少个,N个

可以显示多个文件的shiftmore.bat
@echo off

:loop

if ‘%1’==’’ goto done

type %1

shift

goto loop

:done

从上方的例子中演变出来

可以用通配符的多文件显示fshift.bat
@echo off

:loop

if ‘%1’==’’ goto done

for %%F in (%1) do type %%f

shift

goto loop

:done

7.4在批处理中启动另一个批处理
first.bat
@echo off

echo about to call the second batch file

call second

echo back from the second batch file

echo done

second.bat
echo in the batch file

echo all done here

解释
如果first.bat不用call呼叫second.bat那么会得到

@echo off

echo about to call the second batch file

echo in the batch file

echo all done here

这样的结果

dos重新显示提示符

使if能识别大小定
@echo off

for %%f in (Blue blue BLUE) do if ‘%1’==’%%f’ echo blue

for %%f in (RED red Red) do if ‘%1’==’%%f’ echo blue

for %%f in (Green GREEN green) do if ‘%1’==’%%f’ echo blue

8 系统自动执行autoexec.bat


常用的autoexec.bat命令
自动恢复文件hqx.bat(for win98)

@echo off

Pause

Echo the operation is warning your system will destory

if exist c:/system.1st attrib –h –r –s system.1st

if exist c:/windows/system.dat attrib –h –r –s system.dat

if not exist c:/system.1st echo the system.1st not found

if not exist c:/windows/system.dat echo the system.dat not found

if not exist c:/system.1st goto done

echo autobackup the system.1st to system!!!

:loop

choice /c:yn enter choice!!!!

If not errorlevel 1 goto loop

If errorlevel 1 if  not  errorleverl 2  goto copy

If errorlevel 2 if  not  errorleverl 3  goto no

:copy

copy c:/system.1st c:/windows/system.dat

goto done

:no

echo you don’t autobackup the system.1st

:done

attrib +h +r +s c:/windows/system.dat

attrib +h +r +s c:/system.1st

PATH
定义DOS命令路径

查找外部命令和批处理文件的目录路径

PROMPT
定义系统提示符

PROMPT $P$G

FASTOPEN
减少访问文件的时间

只有重复打开某些文件时才能节省时间

FASTOPEN C:=50

FASTOPEN C:=50 D:=50

安装高性能打印队列
print /b:4096 /d:prn /m:32 /u:8

通知DOS在哪建立临时文件
SET TEMP=E:

如果没有足够的空间用于建立临时文件,则管道操作将宣告失败

指定DIR命令属性
SET DIRCMD=/O:N/L

启动DOS Shell
提供用户友好命令

一般在最后执行,就不用批处理的CALL

这样才不必担心能否重新获得控制的问题,因为工作都做了

SHARE
如果硬盘分区大于32M,则必须安装share命令

为了更好利用内存,应该使用config.sys文件的install项

高级概念
如果没有autoexec.bat
自动运行date与time

软件安装时保护autoexec.bat
call custom

custom包含原来autoexec.bat的内容

禁止autoexec.bat运行
可以在config.sys中使用shell=

 
原创粉丝点击