75人参与 • 2024-05-19 • Dos/bat
批处理脚本文件具有特殊的扩展名bat或cmd,这种类型的文件通过称为**命令解释器(cmd.exe)**的系统文件提供的接口(shell)来识别和执行。
命令 | 备注 |
---|---|
ver | 显示正在使用的ms-dos的版本 |
assoc | 这是将扩展名与文件类型(ftype)相关联的批处理命令,显示现有关联或删除关联 |
cd | 更改不同的目录,或显示当前目录 |
cls | 清屏 |
copy | 文件复制 |
del | 删除文件 |
dir | 列出目录内容 |
date | 系统日期 |
echo | 显示消息,或打开、关闭命令回显 |
exit | 退出dos控制台 |
md | 创建目录 |
move | 移动文件或目录 |
path | 显示或设置路径变量 |
pause | 等待输入 |
prompt | 用于更改或重置cmd.exe提示符 |
rd | 删除目录(需为空) |
ren | 重命名文件和目录 |
rem | 注释 |
start | 在新窗口中启动程序,或打开文档 |
time | 设置或显示时间 |
type | 将文件或文件的内容打印到输出中 |
vol | 显示卷标 |
attrib | 显示或设置当前目录中的文件的属性 |
chkdsk | 检查磁盘是否有问题 |
choice | 为用户提供了一个选项列表 |
cmd | 调用另一个命令提示符实例 |
comp | 根据文件大小比较2个文件 |
convert | 将卷从fat16或fat32文件系统转换为ntfs文件系统 |
driverquery | 显示所有已安装的设备驱动程序及其属性 |
expand | 从压缩的.cab压缩文件中提取文件 |
find | 在文件或输入中搜索字符串,输出匹配的行 |
format | 将磁盘格式化为使用windows支持的文件系统(如fat,fat32或ntfs),从而覆盖磁盘的先前内容 |
help | 显示windows提供的命令的列表 |
ipconfig | 显示windows ip配置。显示连接的配置和连接的名称 |
label | 添加,设置或删除磁盘标签 |
more | 一次显示一个或多个文件的内容 |
net | 根据使用的命令提供各种网络服务 |
ping | 通过网络将icmp/ip “回显”发送到指定的地址的包 |
shutdown | 关闭计算机,或者注销当前用户 |
sort | 从源文件获取输入,并按字母顺序排序其内容,从a到z或从z到a。它将在控制台上打印输出 |
subst | 将驱动器号分配给本地文件夹,显示当前分配或删除分配 |
systeminfo | 该批处理命令显示计算机及其操作系统的配置 |
taskkill | 该批处理命令结束一个或多个任务 |
tasklist | 列出任务,包括任务名称和进程标识(pid) |
xcopy | 以更高级的方式复制文件和目录 |
tree | 将当前目录的所有子目录的树显示为递归或深度的任何级别 |
fc | 列出两个文件之间的实际差异 |
diskpart | 显示和配置磁盘分区的属性 |
title | 该批处理命令设置控制台窗口中显示的标题 |
set | 显示当前系统上的环境变量列表 |
/l
: for循环移动范围的/l
参数用于迭代数组。/a
:数值
@echo off
rem remarks
程序执行时忽略rem关键字之后的任何内容。
:: remarks
@echo off rem this is for listing down all the files in the directory program files dir "c:\program files" > c:\lists.txt echo "the program has completed"
rem test.bat @echo off echo %1 echo %2 echo %3
运行方式
test.bat 1 2 3
set /a variable-name=value
@echo off set message=hello world echo %message% rem 变量需要包含在%符号中显示
@echo off set /a a=5 set /a b=10 set /a c=%a% + %b% echo %c%
setlocal
命令,使变量局部在脚本的范围内。endlocal
,调用exit
,或者当执行到达脚本中的文件结尾(eof)时都会返回。@echo off set globalvar=5 setlocal set var=13145 set /a var=%var% + 5 echo %var% echo %globalvar% endlocal
@echo off echo %java_home%
set message=hello world
set a= if [%a%]==[] echo "string a is empty"
要检查是否存在空字符串,需要在方括号中包含变量名,并将其与方括号中的值进行比较。
@echo off set a=hello set b=world set /a d=50 set c=%a% and %b% %d% echo %c% :: hello and world 50
@echo off set str=hello world call :strlen str strlen echo string is %strlen% characters long exit /b :strlen setlocal enabledelayedexpansion :strlen_loop if not "!%1:~%len%!"=="" set /a len+=1 & goto :strlen_loop (endlocal & set %2=%len%) goto :eof
使用set变量设置为字符串的变量后,可以使用set变量的**/a开关转换为整数**。
@echo off set var=13145 set /a var=%var% + 5 echo %var%
%variable:~num_chars_to_skip% %variable:~num_chars_to_skip,num_chars_to_keep%
输出:
helloworld
hello
~0,5
用于指定需要显示的字符
@echo off set x=1000 set y=1 set y=%y% echo %x% set y=%y:~-4% echo %y%
输出
1000
1
使用~-4
选项来表示只想显示字符串y的最后4个字符。
@echo off set str=batch scripts is easy. it is really easy. echo %str% :: 删除is set str=%str:is=% echo %str% :: batch scripts easy. it really easy.
@echo off set str=batch scripts is easy. it is really easy echo %str% set str=%str:~1,-1% echo %str% :: atch scripts is easy. it is really eas
:=
@echo off set str=this string has a lot of spaces echo %str% set str=%str:=% echo %str% :: thisstringhasalotofspaces
@echo off set str=this message needs changed. echo %str% set str=%str:needs=has% echo %str% :: this message has changed.
set a[0]=1
@echo off set list=1 2 3 4 (for %%a in (%list%) do ( echo %%a ))
echo %a[0]%
set a[3]=4
@echo off setlocal enabledelayedexpansion set topic[0]=comments set topic[1]=variables set topic[2]=arrays set topic[3]=decision making set topic[4]=time and date set topic[5]=operators for /l %%n in (0,1,5) do ( echo !topic[%%n]! )
for循环移动范围的/l
参数用于迭代数组。
@echo off set arr[0]=1 set arr[1]=2 set arr[2]=3 set arr[3]=4 set "x=0" :symloop if defined arr[%x%] ( call echo %%arr[%x%]%% set /a "x+=1" goto :symloop ) echo "the length of the array is" %x%
@echo off set len=3 set obj[0].name=joe set obj[0].id=1 set obj[1].name=mark set obj[1].id=2 set obj[2].name=mohan set obj[2].id=3 set i=0 :loop if %i% equ %len% goto :eof set cur.name= set cur.id= for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do ( set cur.%%k=%%l ) echo name=%cur.name% echo value=%cur.id% set /a i=%i%+1 goto loop
if %c%==15 (echo "the value of variable c is 15") else (echo "unknown value")
if defined
:变量是否存在if defined str1 echo "variable str1 is defined"
if exists
:文件是否存在if exist c:\set3.txt (echo "file exists") else (echo "file does not exist")
if errorlevel
:测试运行的最后一个命令的退出代码if errorlevel n somecommand
各种命令发出整数退出代码来表示命令的状态。 通常,如果命令成功完成,则命令通过传递0;如果命令失败,命令通过传递1。
@echo off set /a a=5 if %a%==5 goto :labela if %a%==10 goto :labelb :labela echo "the value of a is 5" exit /b 0 :labelb echo "the value of a is 10"
运算符 | 备注 |
---|---|
equ | 相等 |
neq | 不等 |
lss | 左小于右 |
leq | 小于等于 |
gtr | 大于 |
geq | 大于等于 |
echo %date%
@echo off echo/today is: %year%-%month%-%day% goto :eof setlocal enableextensions set t=2&if "%date%z" lss "a" set t=1 for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('echo/^|date') do ( for /f "tokens=%t%-4 delims=.-/ " %%d in ('date/t') do ( set %%a=%%d&set %%b=%%e&set %%c=%%f)) endlocal&set %1=%yy%&set %2=%mm%&set %3=%dd%&goto :eof
echo %time%
dir c:\ > lists.txt 2> &1
:function_name do_something exit /b 0
:display set /a index=2 echo the value of index is %index% exit /b 0
@echo off rem main setlocal call :display 5 , 10 exit /b %errorlevel% rem function :display echo the value of parameter 1 is %~1 echo the value of parameter 2 is %~2 exit /b 0
exit / b%errorlevel%
语句,以便将主程序的代码与函数分开。~1
来访问发送给函数的第一个参数,同样使用~2
来访问第二个参数。:: 获取内存大于40mb的进程 tasklist /fi "memusage gt 40000"
/s system | 指定要连接的远程系统 |
/u [domain]user | 指定命令应在其下执行的用户上下文 |
/p [password] | 指定给定用户上下文的密码。 提示输入,如果省略。 |
/m [module] | 列出当前使用给定的exe / dll名称的所有任务。 如果未指定模块名称,则显示所有已加载的模块。 |
/svc | 显示每个进程中托管的服务。 |
/v | 显示详细的任务信息。 |
/fi filter | 显示一组符合过滤器指定条件的任务。 |
/fo format | 指定输出格式。 有效值:table,list,csv。 |
/nh | 指定“列标题”不应显示在输出中。 仅适用于table和csv格式。 |
:: 获取内存大于40mb的进程 tasklist /fi "memusage gt 40000"
start "title" [/d path] [options] "command" [parameters]
/s system | 指定要连接的远程系统 |
/u [domain]user | 指定命令应在其下执行的用户上下文。 |
/p [password] | 指定给定用户上下文的密码。 提示输入,如果省略。 |
/fi filtername | 应用过滤器来选择一组任务,允许使用*通配符。 |
/pid processid | 指定要终止的进程的pid。使用tasklist来获取pid。 |
/im imagename | 指定要终止的进程的映像名称。 通配符*可用于指定所有任务或图像名称。 |
/t | 终止指定的进程以及由其启动的任何子进程。 |
/f | 指定强制终止进程。 |
:: 杀死(终止)打开的记事本任务 taskkill /f /im notepad.exe :: 杀死了一个id为9901的进程 taskill /pid 9901
start "title" [/d path] [options] "command" [parameters]
title | cmd窗口标题栏的文本(必需) |
path | 起始目录。 |
command | 命令,批处理文件或可执行程序运行。 |
parameters | 传递给命令的参数 |
options:
/min | 启动窗口最小化。 |
/max | 启动窗口最大化。 |
/low | 使用idle优先级。 |
/normal | 使用normal优先级。 |
/abovenormal | 使用abovenormal优先级。 |
/belownormal | 使用belownormal优先级。 |
/high | 使用high优先级。 |
/realtime | 使用realtime优先级。 |
:: 在新窗口中运行批处理脚本test.bat。 窗口将以最小化模式启动,并且指定标题为:“test batch script”。 start "test batch script" /min test.bat :: 在另一个进程中运行microsoft word,然后在ms word中打开文件testa.txt start "" "c:\program files\microsoft office\winword.exe" "d:\test\testa.txt"
dw=dir /w
当要执行dir /w命令时,可以简单地键入dw这个单词。dw这个词现在已经成为命令dir /w的别名。
doskey [options] [macroname=[text]]
@echo off doskey cd=cd/test doskey d=dir
@echo off doskey cd=cd/test doskey d=dir d=
将d的值设置为null,所以宏d将被删除。
管道运算符(|)将一个命令的输出(默认为stdout)引导到另一个命令的输入(默认为stdin)。
:: 两个命令同时启动,但是sort命令暂停,直到它接收到dir命令的输出。 :: sort命令使用dir命令的输出作为输入,然后将其输出发送到句柄1(即stdout)。 dir c:\ | sort
到此这篇关于教你编写bat脚本windows批处理的文章就介绍到这了,更多相关bat脚本windows批处理内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论