『楼 主』:
P处理笔记。里面有很多你不知道的。
使用 LLM 解释/回答一下
1 echo 和 @回显控制命令
@ #关闭单行回显echo off #从下一行开始关闭回显@echo off #从本行开始关闭回显。一般批处理第一行都是这个echo on #从下一行开始打开回显echo #显示当前是 echo off 状态还是 echo on 状态echo. #输出一个"回车换行",一般就是指空白行echo hello world #输出hello world
"关闭回显"是指运行批处理文件时,不显示文件里的每条命令,只显示运行结果批处理开始和结束时,系统都会自动打开回显
2 errorlevel程序返回码
echo %errorlevel%每个命令运行结束,可以用这个命令行格式查看返回码用于判断刚才的命令是否执行成功默认值为0,一般命令执行出错会设 errorlevel 为1
3 dir显示目录中的文件和子目录列表
dir #显示当前目录中的文件和子目录dir /a #显示当前目录中的文件和子目录,包括隐藏文件和系统文件dir c: /a:d #显示 C 盘当前目录中的目录dir c:\ /a:-d #显示 C 盘根目录中的文件dir d:\mp3 /b/p #逐屏显示 d:\mp3 目录里的文件,只显示文件名,不显示时间和大小
dir *.exe /s显示当前目录和子目录里所有的.exe文件其中 * 是通配符,代表所有的文件名,还一个通配符 ? 代表一个任意字母或汉字如 c*.* 代表以 c 开头的所有文件?.exe 代表所有文件名是一个字母的.exe文件
如果指定的目录或文件不存在,将返回 errorlevel 为1
每个文件夹的 dir 输出都会有2个子目录 . 和 ... 代表当前目录.. 代表当前目录的上级目录dir . #显示当前目录中的文件和子目录dir .. #显示当前目录的上级目录中的文件和子目录
其它参数可参考 dir /?
4 cd更改当前目录
cd mp3 #进入当前目录中的mp3 目录cd .. #进入当前目录中的上级目录cd\ #进入根目录cd #显示当前目录cd /d d:\mp3 #可以同时更改盘符和目录
cd\"Documents and Settings"\All users文件名带空格,可以不加引号建议加上引号,因为有的时候不加引号会运行出错,如登录脚本
如果更改到的目录不存在,则出错返回 errorlevel=1
5 md创建目录
md abc #在当前目录里建立子目录 abcmd d:\a\b\c #如果 d:\a 不存在,将会自动创建
6 rd删除目录
rd abc #删除当前目录里的 abc 子目录,要求为空目录rd /s/q d:\temp #删除 d:\temp 文件夹及其子文件夹和文件,不需要按 Y 确认
7 del删除文件
del d:\test.txt #删除指定文件,不能是隐藏、系统、只读文件
del *.*删除当前目录里的所有文件,不包括隐藏、系统、只读文件,要求按 Y 确认
del /q/a/f d:\temp\*.*删除 d:\temp 文件夹里面的所有文件,包括隐藏、只读、系统文件,不包括子目录
del /q/a/f/s d:\temp\*.*删除 d:\temp 及子文件夹里面的所有文件,包括隐藏、只读、系统文件,不包括子目录
8 ren文件重命名
ren 1.txt 2.bak #把 1.txt 更名为 2.bakren *.txt *.ini #把当前目录里所有.txt文件改成.ini文件ren d:\temp tmp #支持对文件夹的重命名
9 cls清屏
10 type显示文件内容
type c:\boot.ini #显示指定文件的内容,程序文件一般会显示乱码type *.txt #显示当前目录里所有.txt文件的内容
11 copy拷贝文件
copy c:\test.txt d:\复制 c:\test.txt 文件到 d:\
copy c:\test.txt d:\test.bak复制 c:\test.txt 文件到 d:\ ,并重命名为 test.bak
copy c:\*.*复制 c:\ 所有文件到当前目录,不包括隐藏文件和系统文件不指定目标路径,则默认目标路径为当前目录
copy con test.txt从屏幕上等待输入,按 Ctrl+Z 结束输入,输入内容存为test.txt文件con代表屏幕,prn代表打印机,nul代表空设备
copy 1.txt + 2.txt 3.txt合并 1.txt 和 2.txt 的内容,保存为 3.txt 文件如果不指定 3.txt ,则保存到 1.txt
copy test.txt +复制文件到自己,实际上是修改了文件日期
12 title设置cmd窗口的标题
title 新标题 #可以看到cmd窗口的标题栏变了
13 ver显示系统版本
14 label 和 vol设置卷标
vol #显示卷标label #显示卷标,同时提示输入新卷标label c:system #设置C盘的卷标为 system
15 pause暂停命令
运行该命令时,将显示下面的消息:请按任意键继续 . . .
一般用于看清楚屏幕上显示的内容
16 rem 和 ::注释命令
注释行不执行操作
17 date 和 time日期和时间
date #显示当前日期,并提示输入新日期,按"回车"略过输入date/t #只显示当前日期,不提示输入新日期time #显示当前时间,并提示输入新时间,按"回车"略过输入time/t #只显示当前时间,不提示输入新时间
18 goto 和 :跳转命令
:label #行首为:表示该行是标签行,标签行不执行操作goto label #跳转到指定的标签那一行
19 find (外部命令)查找命令
find "abc" c:\test.txt在 c:\test.txt 文件里查找含 abc 字符串的行如果找不到,将设 errorlevel 返回码为1
find /i "abc" c:\test.txt查找含 abc 的行,忽略大小写
find /c "abc" c:\test.txt显示含 abc 的行的行数
20 more (外部命令)逐屏显示
more c:\test.txt #逐屏显示 c:\test.txt 的文件内容
21 tree显示目录结构
tree d:\ #显示D盘的文件目录结构
22 &顺序执行多条命令,而不管命令是否执行成功
c: & cd\ & dir /w相当于把下面3行命令写到1行去了c:cd\dir /w
23 &&顺序执行多条命令,当碰到执行出错的命令后将不执行后面的命令
f: && cd\ && dir >c:\test.txt注意如果f盘不存在,那么后面2条命令将不会执行
find "ok" c:\test.txt && echo 成功如果找到了"ok"字样,就显示"成功",找不到就不显示
24 ||顺序执行多条命令,当碰到执行正确的命令后将不执行后面的命令
f: || e:如果有f盘就不进入e盘
find "ok" c:\test.txt || echo 不成功如果找不到"ok"字样,就显示"不成功",找到了就不显示
25 |管道命令
前一个命令的执行结果输出到后一个命令
dir *.* /s/a | find /c ".exe"管道命令表示先执行 dir 命令,对其输出的结果执行后面的 find 命令该命令行结果:输出当前文件夹及所有子文件夹里的.exe文件的个数
type c:\test.txt|more这个和 more c:\test.txt 的效果是一样的
26 > 和 >>输出重定向命令
> 清除文件中原有的内容后再写入>> 追加内容到文件末尾,而不会清除原有的内容主要将本来显示在屏幕上的内容输出到指定文件中
指定文件如果不存在,则自动生成该文件
echo hello world>c:\test.txt生成c:\test.txt文件,内容为hello world这个格式在批处理文件里用得很多,可以生成 .reg .bat .vbs 等临时文件
type c:\test.txt >prn屏幕上不显示文件内容,转向输出到打印机
echo hello world>con在屏幕上显示hello world,实际上所有输出都是默认 >con 的
copy c:\test.txt f: >nul拷贝文件,并且不显示"文件复制成功"的提示信息,但如果f盘不存在,还是会显示出错信息
copy c:\test.txt f: >nul 2>nul不显示"文件复制成功"的提示信息,并且f盘不存在的话,也不显示错误提示信息
echo ^^W ^> ^W>c:\test.txt生成的文件内容为 ^W > W^ 和 > 是控制命令,要把它们输出到文件,必须在前面加个 ^ 符号
27 <从文件中获得输入信息,而不是从屏幕上
一般用于 date time label 等需要等待输入的命令@echo offecho 2005-05-01>temp.txtdate <temp.txtdel temp.txt这样就可以不等待输入直接修改当前日期
28 %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %*命令行传递给批处理的参数
%0 批处理文件本身%1 第一个参数%9 第九个参数%* 从第一个参数开始的所有参数
在C盘根目录新建test.bat,内容如下:
@echo offecho %0echo %1echo %2echo %*
运行cmd,输入 c:\test.bat "/a" /b /c /d可以看出每个参数的含意
修改test.bat内容如下
@echo offecho %1echo %~1echo %0echo %~f0echo %~d0echo %~p0echo %~n0echo %~x0echo %~s0echo %~a0echo %~t0echo %~z0
再运行cmd,输入 c:\test.bat "/a" /b /c /d可以参照 call/? 或 for/? 看出每个参数的含意注意这里可以对文件进行日期比较和大小比较
echo load "%%1" "%%2">c:\test.txt生成的文件内容为 load "%1" "%2"批处理文件里,用这个格式把命令行参数输出到文件
29 if判断命令
if "%1"=="/a" echo 第一个参数是/a
if /i "%1" equ "/a" echo 第一个参数是/a/i 表示不区分大小写,equ 和 == 是一样的,其它运算符参见 if/?
if exist c:\test.bat echo 存在c:\test.bat文件
if not exist c:\windows ( echo 不存在c:\windows文件夹 rem 有多条命令可用小括号括起来,称为"复合语句" rem 行前的空格是为了看起来条理清楚 )
if exist c:\test.bat ( echo 存在c:\test.bat ) else ( echo 不存在c:\test.bat rem else表示"否则",判断结果为假时执行其后的命令 )
30 setlocal 和 endlocal设置"命令扩展名"和"延缓环境变量扩充"
SETLOCAL ENABLEEXTENSIONS #启用"命令扩展名"SETLOCAL DISABLEEXTENSIONS #停用"命令扩展名"SETLOCAL ENABLEDELAYEDEXPANSION #启用"延缓环境变量扩充"SETLOCAL DISABLEDELAYEDEXPANSION #停用"延缓环境变量扩充"ENDLOCAL #恢复到使用SETLOCAL语句以前的状态
"命令扩展名"默认为启用"延缓环境变量扩充"默认为停用批处理结束系统会自动恢复默认值
可以修改注册表以禁用"命令扩展名",详见 cmd /? 。所以用到"命令扩展名"的程序,建议在开头和结尾加上 SETLOCAL ENABLEEXTENSIONS 和 ENDLOCAL 语句,以确保程序能在其它系统上正确运行
"延缓环境变量扩充"主要用于 if 和 for 的符合语句,在 set 的说明里有其实用例程
31 set设置变量
引用变量可在变量名前后加 % ,即 %变量名%
set #显示目前所有可用的变量,包括系统变量和自定义的变量echo %SystemDrive% #显示系统盘盘符。系统变量可以直接引用set p #显示所有以p开头的变量,要是一个也没有就设errorlevel=1set p=aa1bb1aa2bb2 #设置变量p,并赋值为 = 后面的字符串,即aa1bb1aa2bb2echo %p% #显示变量p代表的字符串,即aa1bb1aa2bb2echo %p:~6% #显示变量p中第6个字符以后的所有字符,即aa2bb2echo %p:~6,3% #显示第6个字符以后的3个字符,即aa2echo %p:~0,3% #显示前3个字符,即aa1echo %p:~-2% #显示最后面的2个字符,即b2echo %p:~0,-2% #显示除了最后2个字符以外的其它字符,即aa1bb1aa2becho %p:aa=c% #用c替换变量p中所有的aa,即显示c1bb1c2bb2echo %p:aa=% #将变量p中的所有aa字符串置换为空,即显示1bb12bb2echo %p:*bb=c% #第一个bb及其之前的所有字符被替换为c,即显示c1aa2bb2set p=%p:*bb=c% #设置变量p,赋值为 %p:*bb=c% ,即c1aa2bb2set /a p=39 #设置p为数值型变量,值为39set /a p=39/10 #支持运算符,有小数时用去尾法,39/10=3.9,去尾得3,p=3set /a p=p/10 #用 /a 参数时,在 = 后面的变量可以不加%直接引用set /a p="1&0" #"与"运算,要加引号。其它支持的运算符参见set/?set p= #取消p变量
set /p p=请输入屏幕上显示"请输入",并会将输入的字符串赋值给变量p注意这条可以用来取代 choice 命令
注意变量在 if 和 for 的复合语句里是一次性全部替换的,如@echo offset p=aaaif %p%==aaa ( echo %p% set p=bbb echo %p% )结果将显示aaaaaa因为在读取 if 语句时已经将所有 %p% 替换为aaa这里的"替换",在 /? 帮助里就是指"扩充"、"环境变量扩充"可以启用"延缓环境变量扩充",用 ! 来引用变量,即 !变量名!@echo offSETLOCAL ENABLEDELAYEDEXPANSIONset p=aaaif %p%==aaa ( echo %p% set p=bbb echo !p! )ENDLOCAL结果将显示aaabbb
还有几个动态变量,运行 set 看不到%CD% #代表当前目录的字符串%DATE% #当前日期%TIME% #当前时间%RANDOM% #随机整数,介于0~32767%ERRORLEVEL% #当前 ERRORLEVEL 值%CMDEXTVERSION% #当前命令处理器扩展名版本号%CMDCMDLINE% #调用命令处理器的原始命令行可以用echo命令查看每个变量值,如 echo %time%注意 %time% 精确到毫秒,在批处理需要延时处理时可以用到
32 start批处理中调用外部程序的命令,否则等外部程序完成后才继续执行剩下的指令
start explorer d:\调用图形界面打开D盘
@echo offcd /d %~dp0regedit /s 劲舞团.regstart patcher.exe
不加 start 命令的话,"劲舞团"运行时,后面会有个黑乎乎的cmd窗口
33 call批处理中调用另外一个批处理的命令,否则剩下的批处理指令将不会被执行有时有的应用程序用start调用出错的,也可以call调用
34 choice (外部命令)选择命令
让用户输入一个字符,从而选择运行不同的命令,返回码errorlevel为1234……win98里是choice.comwin2000pro里没有,可以从win98里拷过来win2003里是choice.exe
choice /N /C y /T 5 /D y>nul延时5秒
下面是个 choice 语句的例子@echo offrem 以下在win2000pro运行通过,从win98里拷的chioce.com文件
choice /c:abc aaa,bbb,cccif errorlevel 3 goto cccif %errorlevel%==2 goto bbbif errorlevel==1 goto aaa
rem 必须先判断数值高的返回码rem 可以看到 errorlevel 值的判断有3种写法,有时某种写法不好用,可以用另外的写法rem 直接运行chioce相当于运行choice /c:yn
:aaaecho aaagoto end
:bbbecho bbbgoto end
:cccecho cccgoto end
:end
35 assoc 和 ftype文件关联
assoc 设置'文件扩展名'关联,关联到'文件类型'ftype 设置'文件类型'关联,关联到'执行程序和参数'
当你双击一个.txt文件时,windows并不是根据.txt直接判断用 notepad.exe 打开而是先判断.txt属于 txtfile '文件类型'再调用 txtfile 关联的命令行 txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1
可以在"文件夹选项"→"文件类型"里修改这2种关联
assoc #显示所有'文件扩展名'关联assoc .txt #显示.txt代表的'文件类型',结果显示 .txt=txtfileassoc .doc #显示.doc代表的'文件类型',结果显示 .doc=Word.Document.8assoc .exe #显示.exe代表的'文件类型',结果显示 .exe=exefileftype #显示所有'文件类型'关联ftype exefile #显示exefile类型关联的命令行,结果显示 exefile="%1" %*
assoc .txt=Word.Document.8设置.txt为word类型的文档,可以看到.txt文件的图标都变了
assoc .txt=txtfile恢复.txt的正确关联 ftype exefile="%1" %*恢复 exefile 的正确关联如果该关联已经被破坏,可以运行 command.com ,再输入这条命令36 pushd 和 popd切换当前目录
@echo offc: & cd\ & md mp3 #在 C:\ 建立 mp3 文件夹md d:\mp4 #在 D:\ 建立 mp4 文件夹cd /d d:\mp4 #更改当前目录为 d:\mp4pushd c:\mp3 #保存当前目录,并切换当前目录为 c:\mp3popd #恢复当前目录为刚才保存的 d:\mp4
一般用处不大,在当前目录名不确定时,会有点帮助
37 for循环命令
这个比较复杂,请对照 for/? 来看
for %%i in (c: d: e: f:) do echo %%i依次调用小括号里的每个字符串,执行 do 后面的命令注意%%i,在批处理中 for 语句调用参数用2个%默认的字符串分隔符是"空格键","Tab键","回车键"
for %%i in (*.txt) do find "abc" %%i对当前目录里所有的txt文件执行 find 命令
for /r . %%i in (*.txt) do find "abc" %%i在当前目录和子目录里所有的.txt文件中搜索包含 abc 字符串的行
for /r . %%i in (.) do echo %%~pni显示当前目录名和所有子目录名,包括路径,不包括盘符
for /r d:\mp3 %%i in (*.mp3) do echo %%i>>d:\mp3.txt把 d:\mp3 及其子目录里的mp3文件的文件名都存到 d:\mp3.txt 里去
for /l %%i in (2,1,8) do echo %%i生成2345678的一串数字,2是数字序列的开头,8是结尾,1表示每次加1
for /f %%i in ('set') do echo %%i对 set 命令的输出结果循环调用,每行一个
for /f "eol=P" %%i in ('set') do echo %%i取 set 命令的输出结果,忽略以 P 开头的那几行
for /f %%i in (d:\mp3.txt) do echo %%i显示 d:\mp3.txt 里的每个文件名,每行一个,不支持带空格的名称
for /f "delims=" %%i in (d:\mp3.txt) do echo %%i显示 d:\mp3.txt 里的每个文件名,每行一个,支持带空格的名称
for /f "skip=5 tokens=4" %%a in ('dir') do echo %%a对 dir 命令的结果,跳过前面5行,余下的每行取第4列每列之间的分隔符为默认的"空格"可以注意到 dir 命令输出的前5行是没有文件名的
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do ( echo %%a echo %%b echo %%c )对 date /t 的输出结果,每行取1、2、3列第一列对应指定的 %%a ,后面的 %%b 和 %%c 是派生出来的,对应其它列分隔符指定为 - 和"空格",注意 delims=- 后面有个"空格"其中 tokens=1,2,3 若用 tokens=1-3 替换,效果是一样的
for /f "tokens=2* delims=- " %%a in ('date /t') do echo %%b 取第2列给 %%a ,其后的列都给 %%b
32 subst (外部命令)映射磁盘。
subst z: \\server\d #这样输入z:就可以访问\\server\d了subst z: /d #取消该映射subst #显示目前所有的映时38 xcopy (外部命令)文件拷贝
xcopy d:\mp3 e:\mp3 /s/e/i/y复制 d:\mp3 文件夹、所有子文件夹和文件到 e:\ ,覆盖已有文件加 /i 表示如果 e:\ 没有 mp3 文件夹就自动新建一个,否则会有询问
39 一些不常用的内部命令>& 将一个句柄的输出写入到另一个句柄的输入中<& 从一个句柄读取输入并将其写入到另一个句柄输出中shift 命令行传递给批处理的参数不止9个时,用以切换参数color 设置cmd窗口的显示颜色pormpt 更改命令提示符号,默认都是 盘符:\路径\> ,如 c:\>
40 format (外部命令)格式化硬盘
format c: /q/u/autotest/q表示快速格式化,/autotest表示自动格式化,不需要按 Y 确认/u表示每字节用 F6 覆盖硬盘数据,使其不可用软件恢复
format c: /c格式化C盘,并检测坏道
41 fdisk (外部命令)硬盘分区
win2000不带该命令win98里的fdisk不支持80G以上大硬盘,winme里的支持
fdisk/mbr重建硬盘分区表,一般用于清除引导区病毒、还原精灵注意使用该命令不能从硬盘启动,必须软驱或光驱启动后直接运行
42 ping (外部命令)
ping -l 65500 -t 192.168.1.200不停的向192.168.1.200计算机发送大小为65500byte的数据包
ping -n 10 127.0.0.1>nulping自己10次,可用于批处理延时10秒
43 SC (外部命令)服务控制命令
sc create aaa displayname= bbb start= auto binpath= "C:\WINDOWS\System32\alg.exe"创建服务,服务名称aaa,显示名称bbb,启动类型:自动可执行文件的路径"C:\WINDOWS\System32\alg.exe"
sc description aaa "ccc"更改aaa的描述为ccc
sc config aaa start= disabled binpath= "C:\WINDOWS\System32\svchost.exe -k netsvcs"更改aaa的启动类型:已禁用更改aaa的可执行文件的路径"C:\WINDOWS\System32\svchost.exe -k netsvcs"
sc config aaa start= demand displayname= ddd更改aaa的启动类型:手动更改aaa的显示名称ddd
sc start aaa启动aaa服务
sc stop aaa停止aaa服务
sc delete aaa删除aaa服务
### 1 echo and @Echo Control Commands
@ # Turn off single-line echoing
echo off # Turn off echoing from the next line
@echo off # Turn off echoing from this line. Usually the first line of a batch file is this
echo on # Turn on echoing from the next line
echo # Display whether current state is echo off or echo on
echo. # Output a "carriage return line feed", generally refers to a blank line
echo hello world # Output hello world
"Turning off echoing" means that when running a batch file, it does not display each command in the file, only the running results. The system automatically turns on echoing at the beginning and end of the batch processing.
### 2 errorlevel Program Return Code
echo %errorlevel% Each command ends, you can use this command line format to view the return code
Used to judge whether the previous command was executed successfully
Default value is 0, generally if a command execution fails, errorlevel will be set to 1
### 3 dir Display File and Subdirectory List in Directory
dir # Display files and subdirectories in the current directory
dir /a # Display files and subdirectories in the current directory, including hidden files and system files
dir c: /a:d # Display directories in the current directory of drive C
dir c:\ /a:-d # Display files in the root directory of drive C
dir d:\mp3 /b/p # Display files in the d:\mp3 directory one screen at a time, only display file names, not time and size
dir *.exe /s Display all .exe files in the current directory and subdirectories
Where * is a wildcard, representing all file names, and another wildcard ? represents one arbitrary letter or Chinese character
Such as c*.* represents all files starting with c
?.exe represents all .exe files with a file name of one letter
If the specified directory or file does not exist, it will return errorlevel 1
Each folder's dir output will have 2 subdirectories . and ..., representing the current directory
.. represents the parent directory of the current directory
dir . # Display files and subdirectories in the current directory
dir .. # Display files and subdirectories in the parent directory of the current directory
Other parameters can be referred to dir /?
### 4 cd Change Current Directory
cd mp3 # Enter the mp3 directory in the current directory
cd .. # Enter the parent directory in the current directory
cd\ # Enter the root directory
cd # Display the current directory
cd /d d:\mp3 # Can change both the drive letter and the directory at the same time
cd "Documents and Settings"\All users If the file name has spaces, it is recommended to add quotes. Sometimes not adding quotes may cause errors, such as in login scripts
If the directory to be changed does not exist, an error will occur and return errorlevel=1
### 5 md Create Directory
md abc # Create a subdirectory abc in the current directory
md d:\a\b\c # If d:\a does not exist, it will be created automatically
### 6 rd Delete Directory
rd abc # Delete the abc subdirectory in the current directory, requires it to be an empty directory
rd /s/q d:\temp # Delete the d:\temp folder and its subfolders and files, no need to press Y to confirm
### 7 del Delete File
del d:\test.txt # Delete the specified file, cannot be a hidden, system, or read-only file
del *.* Delete all files in the current directory, not including hidden, system, or read-only files, requires pressing Y to confirm
del /q/a/f d:\temp\*.* Delete all files in the d:\temp folder, including hidden, read-only, system files, not including subdirectories
del /q/a/f/s d:\temp\*.* Delete all files in d:\temp and subfolders, including hidden, read-only, system files, not including subdirectories
### 8 ren Rename File
ren 1.txt 2.bak # Rename 1.txt to 2.bak
ren *.txt *.ini # Rename all .txt files in the current directory to .ini files
ren d:\temp tmp # Supports renaming folders
### 9 cls Clear Screen
### 10 type Display File Content
type c:\boot.ini # Display the content of the specified file, program files generally display garbled characters
type *.txt # Display the content of all .txt files in the current directory
### 11 copy Copy File
copy c:\test.txt d:\ Copy the c:\test.txt file to d:\
copy c:\test.txt d:\test.bak Copy the c:\test.txt file to d:\ and rename it to test.bak
copy c:\*.* Copy all files in c:\ to the current directory, not including hidden files and system files
If the target path is not specified, the default target path is the current directory
copy con test.txt Wait for input from the screen, press Ctrl+Z to end input, and the input content is saved as test.txt file
con represents the screen, prn represents the printer, nul represents the empty device
copy 1.txt + 2.txt 3.txt Merge the contents of 1.txt and 2.txt and save it as 3.txt file
If 3.txt is not specified, it is saved to 1.txt
copy test.txt + Copy the file to itself, actually modifies the file date
### 12 title Set the Title of the cmd Window
title New Title # You can see that the title bar of the cmd window has changed
### 13 ver Display System Version
### 14 label and vol Set Volume Label
vol # Display volume label
label # Display volume label and prompt to enter a new volume label
label c:system # Set the volume label of drive C to system
### 15 pause Pause Command
When running this command, the following message will be displayed: Press any key to continue . . .
Generally used to see the content displayed on the screen clearly
### 16 rem and :: Comment Command
Comment lines do not perform operations
### 17 date and time Date and Time
date # Display current date and prompt to enter a new date, press "Enter" to skip input
date/t # Only display current date, do not prompt to enter new date
time # Display current time and prompt to enter new time, press "Enter" to skip input
time/t # Only display current time, do not prompt to enter new time
### 18 goto and : Jump Command
:label # A line starting with : indicates that this line is a label line, and label lines do not perform operations
goto label # Jump to the specified label line
### 19 find (External Command) Find Command
find "abc" c:\test.txt Find lines containing the string abc in the c:\test.txt file
If not found, it will set errorlevel return code to 1
find /i "abc" c:\test.txt Find lines containing abc, ignoring case
find /c "abc" c:\test.txt Display the number of lines containing abc
### 20 more (External Command) Display Screen by Screen
more c:\test.txt # Display the content of c:\test.txt file screen by screen
### 21 tree Display Directory Structure
tree d:\ # Display the file directory structure of drive D
### 22 & Execute Multiple Commands in Sequence, Regardless of Whether the Command is Executed Successfully
c: & cd\ & dir /w Equivalent to writing the following 3 lines of commands into one line
c:
cd\
dir /w
### 23 && Execute Multiple Commands in Sequence, and Do Not Execute Subsequent Commands When Encountering a Command That Fails to Execute
f: && cd\ && dir >c:\test.txt
Note: If drive F does not exist, then the following 2 commands will not be executed
find "ok" c:\test.txt && echo Success If the word "ok" is found, display "Success", otherwise do not display
### 24 || Execute Multiple Commands in Sequence, and Do Not Execute Subsequent Commands When Encountering a Command That Executes Correctly
f: || e: If there is drive F, do not enter drive E
find "ok" c:\test.txt || echo Not successful If the word "ok" is not found, display "Not successful", if found, do not display
### 25 | Pipe Command
The execution result of the previous command is output to the next command
dir *.* /s/a | find /c ".exe"
The pipe command means to first execute the dir command, and execute the subsequent find command on its output result
The result of this command line: output the number of .exe files in the current folder and all subfolders
type c:\test.txt|more This has the same effect as more c:\test.txt
### 26 > and >> Output Redirection Command
> Clear the original content in the file and then write
>> Append content to the end of the file without clearing the original content
Mainly output the content originally displayed on the screen to the specified file
If the specified file does not exist, it will be automatically generated
echo hello world>c:\test.txt Generate the c:\test.txt file with the content hello world
This format is used a lot in batch files, and can generate temporary files such as .reg .bat .vbs
type c:\test.txt >prn Do not display the file content on the screen, redirect to output to the printer
echo hello world>con Display hello world on the screen, in fact, all output is default >con
copy c:\test.txt f: >nul Copy the file and do not display the prompt "File copied successfully", but if drive F does not exist, the error message will still be displayed
copy c:\test.txt f: >nul 2>nul Do not display the prompt "File copied successfully", and if drive F does not exist, do not display the error prompt
echo ^^W ^> ^W>c:\test.txt The content of the generated file is ^W > W^. The > is a control command. To output them to the file, you must add a ^ in front
### 27 < Get Input Information from the File Instead of from the Screen
Generally used for commands that need to wait for input such as date time label
@echo off
echo 2005-05-01>temp.txt
date <temp.txt
del temp.txt
In this way, the current date can be modified without waiting for input
### 28 %0 %1 %2 %3 %4 %5 %6 %7 %8 %9 %* Parameters Passed to the Batch File from the Command Line
%0 Batch file itself
%1 First parameter
%9 Ninth parameter
%* All parameters starting from the first parameter
Create test.bat in the root directory of drive C with the following content:
@echo off
echo %0
echo %1
echo %2
echo %*
Run cmd and enter c:\test.bat "/a" /b /c /d to see the meaning of each parameter
Modify the content of test.bat as follows
@echo off
echo %1
echo %~1
echo %0
echo %~f0
echo %~d0
echo %~p0
echo %~n0
echo %~x0
echo %~s0
echo %~a0
echo %~t0
echo %~z0
Run cmd again and enter c:\test.bat "/a" /b /c /d. You can refer to call/? or for/? to see the meaning of each parameter. Note that date comparison and size comparison can be performed on files here
echo load "%%1" "%%2">c:\test.txt The content of the generated file is load "%1" "%2"
In the batch file, use this format to output command line parameters to the file
### 29 if Judgment Command
if "%1"=="/a" echo The first parameter is /a
if /i "%1" equ "/a" echo The first parameter is /a
/i means case-insensitive, equ is the same as ==, other operators are referred to if/?
if exist c:\test.bat echo There is a c:\test.bat file
if not exist c:\windows (
echo There is no c:\windows folder
rem Multiple commands can be enclosed in parentheses, called "compound statement"
rem The spaces in front of the line are for clarity
)
if exist c:\test.bat (
echo There is a c:\test.bat
) else (
echo There is no c:\test.bat
rem else means "otherwise", execute the subsequent command when the judgment result is false
)
### 30 setlocal and endlocal Set "Command Extensions" and "Delayed Environment Variable Expansion"
SETLOCAL ENABLEEXTENSIONS # Enable "command extensions"
SETLOCAL DISABLEEXTENSIONS # Disable "command extensions"
SETLOCAL ENABLEDELAYEDEXPANSION # Enable "delayed environment variable expansion"
SETLOCAL DISABLEDELAYEDEXPANSION # Disable "delayed environment variable expansion"
ENDLOCAL # Restore to the state before using the SETLOCAL statement
"Command extensions" are enabled by default
"Delayed environment variable expansion" is disabled by default
The system will automatically restore the default value at the end of the batch processing
The "command extensions" can be disabled by modifying the registry, see cmd /? for details. So for programs that use "command extensions", it is recommended to add SETLOCAL ENABLEEXTENSIONS and ENDLOCAL statements at the beginning and end to ensure that the program can run correctly on other systems
"Delayed environment variable expansion" is mainly used in compound statements of if and for, and there are practical examples in the description of set
### 31 set Set Variable
To reference a variable, you can add % before and after the variable name, that is %variable name%
set # Display all currently available variables, including system variables and custom ones
echo %SystemDrive% # Display the system drive letter. System variables can be directly referenced
set p # Display all variables starting with p, if none, set errorlevel=1
set p=aa1bb1aa2bb2 # Set variable p and assign the string after =, that is aa1bb1aa2bb2
echo %p% # Display the string represented by variable p, that is aa1bb1aa2bb2
echo %p:~6% # Display all characters after the 6th character in variable p, that is aa2bb2
echo %p:~6,3% # Display 3 characters after the 6th character, that is aa2
echo %p:~0,3% # Display the first 3 characters, that is aa1
echo %p:~-2% # Display the last 2 characters, that is b2
echo %p:~0,-2% # Display all characters except the last 2 characters, that is aa1bb1aa2b
echo %p:aa=c% # Replace all aa in variable p with c, that is display c1bb1c2bb2
echo %p:aa=% # Replace all aa strings in variable p with empty, that is display 1bb12bb2
echo %p:*bb=c% # Replace all characters before the first bb and the first bb with c, that is display c1aa2bb2
set p=%p:*bb=c% # Set variable p and assign the value of %p:*bb=c%, that is c1aa2bb2
set /a p=39 # Set p as a numeric variable with a value of 39
set /a p=39/10 # Support operators, use the truncation method for decimals, 39/10=3.9, truncate to 3, p=3
set /a p=p/10 # When using the /a parameter, the variable after = can be directly referenced without adding %
set /a p="1&0" #"And" operation, need to add quotes. Other supported operators are referred to set/?
set p= # Cancel variable p
set /p p=Please enter Display "Please enter" on the screen and assign the input string to variable p
Note that this can be used to replace the choice command
Note that variables are all replaced at one time in the compound statements of if and for. For example:
@echo off
set p=aaa
if %p%==aaa (
echo %p%
set p=bbb
echo %p%
)
The result will display aaaaaa because all %p% have been replaced with aaa when reading the if statement
The "replacement" here is referred to as "expansion" and "environment variable expansion" in /? help. You can enable "delayed environment variable expansion" and use ! to reference variables, that is !variable name!
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set p=aaa
if %p%==aaa (
echo %p%
set p=bbb
echo !p!
)
ENDLOCAL
The result will display aaabbb
There are also several dynamic variables, which cannot be seen by running set
%CD% # String representing the current directory
%DATE% # Current date
%TIME% # Current time
%RANDOM% # Random integer, between 0~32767
%ERRORLEVEL% # Current ERRORLEVEL value
%CMDEXTVERSION% # Current command processor extension version number
%CMDCMDLINE% # Original command line that called the command processor
You can use the echo command to view the value of each variable, such as echo %time%
Note that %time% is accurate to milliseconds, and can be used for delay processing in batch processing
### 32 start Command to Call External Programs in Batch Processing, Otherwise Continue to Execute the remaining instructions after the external program is completed
start explorer d:\ Call the graphical interface to open drive D
@echo off
cd /d %~dp0
regedit /s 劲舞团.reg
start patcher.exe
Without the start command, when "劲舞团" runs, there will be a black cmd window behind
### 33 call Command to Call Another Batch File in Batch Processing, Otherwise the remaining batch processing instructions will not be executed
Sometimes some applications call incorrectly with start, and can also be called with call
### 34 choice (External Command) Choice Command
Let the user enter a character to choose to run different commands, the return code errorlevel is 1 2 3 4...
In win98 it is choice.com
In win2000pro it is not available, you can copy it from win98
In win2003 it is choice.exe
choice /N /C y /T 5 /D y>nul Delay for 5 seconds
The following is an example of a choice statement
@echo off
rem The following runs through in win2000pro, the chioce.com file copied from win98
choice /c:abc aaa,bbb,ccc
if errorlevel 3 goto ccc
if %errorlevel%==2 goto bbb
if errorlevel==1 goto aaa
rem Must judge the return code of higher values first
rem You can see that there are 3 ways to judge the errorlevel value. Sometimes one way is not easy to use, you can use another way
rem Directly running chioce is equivalent to running choice /c:yn
:aaa
echo aaa
goto end
:bbb
echo bbb
goto end
:ccc
echo ccc
goto end
:end
### 35 assoc and ftype File Association
assoc Set 'file extension' association, associated with 'file type'
ftype Set 'file type' association, associated with 'executable program and parameters'
When you double-click a .txt file, windows does not directly judge to open it with notepad.exe according to .txt
But first judge that .txt belongs to the txtfile 'file type' and then call the command line txtfile=%SystemRoot%\system32\NOTEPAD.EXE %1 associated with txtfile
You can modify these 2 associations in "Folder Options"→"File Types"
assoc # Display all 'file extension' associations
assoc .txt # Display the 'file type' represented by .txt, the result shows .txt=txtfile
assoc .doc # Display the 'file type' represented by .doc, the result shows .doc=Word.Document.8
assoc .exe # Display the 'file type' represented by .exe, the result shows .exe=exefile
ftype # Display all 'file type' associations
ftype exefile # Display the command line associated with the exefile type, the result shows exefile="%1" %*
assoc .txt=Word.Document.8 Set .txt as a word type document, you can see that the icon of the .txt file has changed
assoc .txt=txtfile Restore the correct association of .txt
ftype exefile="%1" %* Restore the correct association of exefile
If this association is damaged, you can run command.com and then enter this command
### 36 pushd and popd Switch Current Directory
@echo off
c: & cd\ & md mp3 # Create an mp3 folder in C:\
md d:\mp4 # Create an mp4 folder in D:\
cd /d d:\mp4 # Change the current directory to d:\mp4
pushd c:\mp3 # Save the current directory and switch the current directory to c:\mp3
popd # Restore the current directory to the previously saved d:\mp4
Generally not very useful, it will be a little helpful when the current directory name is uncertain
### 37 for Loop Command
This is relatively complex, please refer to for/?
for %%i in (c: d: e: f:) do echo %%i Call each string in the parentheses in turn and execute the command after do
Note %%i, in the batch processing, the for statement calls parameters with 2%
The default string delimiter is "space bar", "Tab key", "Enter key"
for %%i in (*.txt) do find "abc" %%i Execute the find command on all txt files in the current directory
for /r . %%i in (*.txt) do find "abc" %%i Search for lines containing the string abc in all .txt files in the current directory and subdirectories
for /r . %%i in (.) do echo %%~pni Display the current directory name and all subdirectory names, including the path, not including the drive letter
for /r d:\mp3 %%i in (*.mp3) do echo %%i>>d:\mp3.txt Save the file names of mp3 files in d:\mp3 and its subdirectories to d:\mp3.txt
for /l %%i in (2,1,8) do echo %%i Generate a series of numbers 2345678, 2 is the start of the number sequence, 8 is the end, 1 means add 1 each time
for /f %%i in ('set') do echo %%i Loop and call the output result of the set command, one line at a time
for /f "eol=P" %%i in ('set') do echo %%i Take the output result of the set command and ignore the lines starting with P
for /f %%i in (d:\mp3.txt) do echo %%i Display each file name in d:\mp3.txt, one line at a time, does not support names with spaces
for /f "delims=" %%i in (d:\mp3.txt) do echo %%i Display each file name in d:\mp3.txt, one line at a time, supports names with spaces
for /f "skip=5 tokens=4" %%a in ('dir') do echo %%a For the result of the dir command, skip the first 5 lines, and take the 4th column of the remaining lines
The delimiter between columns is the default "space"
You can notice that the first 5 lines of the dir command output have no file names
for /f "tokens=1,2,3 delims=- " %%a in ('date /t') do (
echo %%a
echo %%b
echo %%c
)
For the output result of date /t, take columns 1, 2, 3 of each line
The first column corresponds to the specified %%a, and the subsequent %%b and %%c are derived, corresponding to other columns
The delimiter is specified as - and "space", note that there is a "space" after delims=-
Among them, tokens=1,2,3 can be replaced with tokens=1-3, and the effect is the same
for /f "tokens=2* delims=- " %%a in ('date /t') do echo %%b Take the 2nd column for %%a, and all subsequent columns for %%b
### 32 subst (External Command) Map Disk.
subst z: \\server\d # In this way, entering z: can access \\server\d
subst z: /d # Cancel this mapping
subst # Display all current mappings
### 38 xcopy (External Command) File Copy
xcopy d:\mp3 e:\mp3 /s/e/i/y Copy the d:\mp3 folder, all subfolders and files to e:\, overwrite existing files
Add /i means if e:\ does not have an mp3 folder, it will be created automatically, otherwise there will be a query
### 39 Some Less Commonly Used Internal Commands
>& Write the output of one handle to the input of another handle
<& Read input from one handle and write it to the output of another handle
shift When there are more than 9 parameters passed to the batch file from the command line, use it to switch parameters
color Set the display color of the cmd window
pormpt Change the command prompt symbol, the default is all drive letter:\path\>, such as c:\>
### 40 format (External Command) Format Hard Disk
format c: /q/u/autotest
/q means quick format, /autotest means automatic format, no need to press Y to confirm
/u means overwrite the hard disk data with F6 per byte, making it unavailable for software recovery
format c: /c Format drive C and detect bad sectors
### 41 fdisk (External Command) Hard Disk Partition
win2000 does not bring this command
fdisk in win98 does not support large hard disks above 80G, fdisk in winme supports
fdisk/mbr Rebuild the hard disk partition table, generally used to clear the boot sector virus and restore the spirit
Note that when using this command, you cannot boot from the hard disk, you must start directly from the floppy disk or CD-ROM
### 42 ping (External Command)
ping -l 65500 -t 192.168.1.200 Continuously send packets with a size of 65500byte to the computer 192.168.1.200
ping -n 10 127.0.0.1>nul Ping yourself 10 times, which can be used for batch processing delay of 10 seconds
### 43 SC (External Command) Service Control Command
sc create aaa displayname= bbb start= auto binpath= "C:\WINDOWS\System32\alg.exe"
Create a service, service name aaa, display name bbb, startup type: automatic
Path of executable file "C:\WINDOWS\System32\alg.exe"
sc description aaa "ccc" Change the description of aaa to ccc
sc config aaa start= disabled binpath= "C:\WINDOWS\System32\svchost.exe -k netsvcs"
Change the startup type of aaa: disabled
Change the path of the executable file of aaa "C:\WINDOWS\System32\svchost.exe -k netsvcs"
sc config aaa start= demand displayname= ddd
Change the startup type of aaa: manual
Change the display name of aaa ddd
sc start aaa Start the aaa service
sc stop aaa Stop the aaa service
sc delete aaa Delete the aaa service
|