- 批处理总结
echo 1 > test.txt ;清空写入 echo 2 >> test.txt ;末尾追加 @echo 不显示该句命令 @echo off 关闭回显状态 @echo on 打开回显状态 cd /d 目录 ;切换任意目录 执行bat文件:假如在桌面 C:\Users\10590\Desktop 执行bat文件 echo %~d0 ;C: echo 当前盘符和路径:%~dp0 ;C:\Users\10590\Desktop\ echo 当前批处理全路径:%~f0 ;C:\Users\10590\Desktop\test.bat echo 当前盘符和路径的短文件名格式:%~sdp0 ;C:\Users\10590\Desktop\ echo 当前CMD默认目录:%cd% ;双击bat文件结果为C:\windows\system32 cmd进入目录执行bat文件结果为C:\Users\10590\Desktop set /p test=<test.txt ;读取文件第一行到test变量中 注册表操作 HKCR: HKEY_CLASSES_ROOT HKCU: HKEY_CURRENT_USER HKLM: HKEY_LOCAL_MACHINE HKU: HKEY_USERS HKCC: HKEY_CURRENT_CONFIG 查询 reg query "HKCU\Console\Git Bash" /s ;返回该项中所有子项和项 结果 HKEY_CURRENT_USER\Console\Git Bash FaceName REG_SZ Lucida Console FontFamily REG_DWORD 0x36 FontSize REG_DWORD 0xe0000 FontWeight REG_DWORD 0x190 reg query "HKCU\Console\Git Bash" /ve ;返回为空的项 结果 HKEY_CURRENT_USER\Console\Git Bash (默认) REG_SZ (数值未设置) reg query "HKCU\Console\Git Bash" /v "FaceName" 结果 HKEY_CURRENT_USER\Console\Git Bash FaceName REG_SZ Lucida Console 添加 reg add "HKCU\Console\Git Bash" /v TestItem /t REG_SZ /d "NumberOne" /f /v 项名称 /t 项值类型 /d 项值 /f 不用询问信息而直接添加子项或项 删除 reg delete "HKCU\Console\Git Bash" /v TestItem /f 应用 reg query "HKCU\Console\Git Bash" /v TestItem 1>nu1 2>nul ;失败不中断提示,程序继续往下走 echo %errorlevel% ;0 成功 1 失败 字符串处理 set "teststr=123456789" set "teststr=%teststr:~1%" ;去掉第一个字符 23456789 set "teststr=%teststr:~0,5%" ;截取前五个字符 set "teststr=%teststr:~-5%" ;截取最后五个字符 set "teststr=%teststr:~0,-5%" ;截取第一个到倒数第五个字符 set "teststr=%teststr:~0,-1%" ;去掉最后一个字符 拼接和替换字符串 %变量1%%变量2% 测试bat文件 @echo off set aa=hello set bb=world echo %aa%%bb% ;结果为helloworld set "aa=%aa:l=m%" ;结果为hemmo echo %aa% pause 求取字符串长度 set str=Thisisateststring set num=0 ;初始化计数 :next1 if not "%str%"=="" ( set /a num+=1 set "str=%str:~1%" ;去掉第一个字符 goto next1 ) echo %num% ; 结果为17 pause 查找字符在字符串中出现的位置 @echo off Setlocal ENABLEDELAYEDEXPANSION ;开启变量延迟 用!变量! 替换%变量% set str1=This is a test string set ch1=t ;区分大小写 set str=%str1% :next if not "%str%"=="" ( set /a num+=1 if "!str:~0,1!"=="%ch1%" goto last set "str=%str:~1%" goto next ) set /a num=0 :last echo %num% ;结果为11 echo !num! ;结果为11 pause 开启变量延迟: 简单来说,在读取了一条完整的语句之后, 不立即对该行的变量赋值,而会在某个单条语句执行之前再进行赋值, 也就是说“延迟”了对变量的赋值。 set a=4 set a=5 & echo %a% ;结果4 echo %a% ; 结果5 setlocal enabledelayedexpansion set a=4 set a=5 & echo !a! ; 结果5 echo %a% ; 结果5 交换两个变量 set var1=abc set var2=123 echo var1=%var1% var2=%var2% set var1=%var2%& set var2=%var1% echo var1=%var1% var2=%var2% pause 例子: for /l %%i in (1,1,5) do ( set a=%%i echo %a% ;5 5 5 5 5 ) setlocal enabledelayedexpansion for /l %%i in (1,1,5) do ( set a=%%i echo %a% ;1 2 3 4 5 ) 例子: setlocal endlocal 改变系统变量只影响该文件使用 setlocal path=d:\ echo path set path ; 结果 ;path ;Path=d:\ ;PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC endlocal echo path set path 结果: path Path=C:\Program Files (x86)\Common Files\Oracle\Java\javapath; C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\iCLS\; C:\Program Files\Intel\Intel(R) Management Engine Components\iCLS\; C:\windows\system32;C:\windows;C:\windows\System32\Wbem; C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\System32\OpenSSH\; .... PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC 截取命令行参数: test.bat Setlocal :loop if %1a==a goto :end Call Set "Params=%Params% %1%" shift goto :loop :end set "Params=%Params:~1%" Echo %Params% EndLocal 执行test.bat C:\Users\10590\Pictures\Saved Pictures\my save 结果: C:\Users\10590\Pictures\Saved Pictures\my save ;判断系统 if "%PROCESSOR_ARCHITECTURE%"=="x86" goto x86 if "%PROCESSOR_ARCHITECTURE%"=="AMD64" goto x64 :x64 echo "x64" exit :x86 echo "x86" ;命令不区分大小写 ;if else 使用创建文件 ;批处理 %date:~0,4% 从零开始四个字符 ;获取桌面路径 reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') ;上述命令结果 Desktop REG_SZ C:\Users\10590\Desktop set deskpath="" for /f "tokens=2,*" %%i in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') do ( set deskpath=%%j ) set "ymd=%date:~0,4%%date:~5,2%%date:~8,2%" if not exist "%windir%\SysWOW64\test.dll" ( ;创建文件夹 rd /s /q 删除整个目录 /s 递归 /q 不需要确认 md "%deskpath%\%ymd%\mytest" ;创建空文件 del 删除文件 del /s test* 删除该目录下以test开头文件 type nul>"%deskpath%\%ymd%\mytest\test.txt" ; 或 copy nul "%deskpath%\%ymd%\mytest\test.txt" ) else ( echo "file exist" ) ;for 的使用 在CMD里面的% 等价于批处理里面的 %% for /d 针对目录操作 cmd 中 for /d %i in (C:\Windows\System32\zh-CN\*) do echo %i ;将echo改为@echo 将不显示echo %i语句 bat 中 for /d %%i in (C:\Windows\System32\zh-CN\*) do echo %%i 结果 C:\Windows\System32\zh-CN\Licenses for /r 目录下递归文件操作 /r 带盘符 省略时默认为当前目录 cmd 中 for /r C:\Windows\System32 %i in (*.dll) do @echo %i ;打印该目录下所有的dll文件 bat 中 for /r C:\Windows\System32 %%i in (*.dll) do @echo %%i 结果: ..... C:\Windows\System32\AccountsRt.dll C:\Windows\System32\AcGenral.dll C:\Windows\System32\AcLayers.dll C:\Windows\System32\acledit.dll C:\Windows\System32\aclui.dll C:\Windows\System32\acmigration.dll ... for /l 用于整数等差操作 cmd 中 for /l %i in (1,2,5) do @echo %i ;范围1到5,每次加2 bat 中 for /l %%i in (1,2,5) do @echo %%i 结果 1 3 5 for /f 用于文本文件增删改等操作 格式skip=2,tokens=1,4,delims=* 跳过文件开始两行 取每行第一和第四端 每段间用* 分割(默认空格) 测试文件: test.txt name sex age class Zhang male 20 A-1 Li male 25 A-2 Wang female 30 A-3 cmd 中 for /f %c in (test.txt) do @echo %c ;默认以空格区分每段 bat 中 for /f %c in (test.txt) do @echo %c 结果 name Zhang Li Wang cmd 中 for /f "skip=1 tokens=1,4 delims=- " %i in (test.txt) do @echo %i %j ;取第一和第四段,以-或空格作为分割符,变量按照字母顺序改变 bat 中 for /f "skip=1 tokens=1,4 delims=- " %%i in (test.txt) do @echo %%i %%j 结果 Zhang A Li A Wang A cmd for /f "skip=1 tokens=1,4 delims=-" %i in (test.txt) do @echo %i %j ;仅以-作为分割 结果 Zhang male 20 A Li male 25 A Wang female 30 A 测试文件test.txt name sex age class Zhang male 20 A-1 Li male 25 A-2 Wang female 30 A-3 cmd 中 for /f "skip=1 tokens=2-4 delims= " %i in (test.txt) do @echo %i %j %k ;取第2,3,4段显示 bat 中 for /f "skip=1 tokens=2-4 delims= " %%i in (test.txt) do @echo %%i %%j %%k 结果 male 20 A-1 male 25 A-2 female 30 A-3 cmd 中 for /f "skip=1 tokens=1* delims= " %i in (test.txt) do @echo %i %j ;取第一段到最后一段,但将整段分为第一段和剩余下的 bat 中 for /f "skip=1 tokens=1* delims= " %%i in (test.txt) do @echo %%i %%j 结果 Zhang male 20 A-1 Li male 25 A-2 Wang female 30 A-3 cmd 中 for /f "skip=1 tokens=1,2* delims= " %i in (test.txt) do @echo %i %j %k ;将整段分为第一、二和剩余下 bat 中 for /f "skip=1 tokens=1,2* delims= " %%i in (test.txt) do @echo %%i %%j %%k 结果 Zhang male 20 A-1 Li male 25 A-2 Wang female 30 A-3 请求使用管理员权限执行批处理 %1 %2 ver|find "5.">nul&&goto :Admin mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :Admin","","runas",1)(window.close)&goto :eof :Admin 常用批处理命令 命令 /? 命令提示符帮助 shift主要用于命令行参数超过9个 命令使用例子 :round if "%1"=="" goto cmd1 echo %1 shift goto round :cmd1 pause other.bat 1 2 3 4 5 6 7 8 9 a 结果 1 2 3 4 5 6 7 8 9 a rem 或:: 注释提示 title terminal ;标题栏为terminal color 0F ;两位十六进制的值 第一个表示背景 第二个表示字体 mode con cols=100 lines=40 ;调整窗口大小 find /name "echo" test.txt ;查找test.txt文件中的含echo行 findstr 命令更智能 findstr /? 查询用法 start 或 call命令调用程序 start test.bat ; 其中start会终止父进程 choose命令 :begin choice /c:1234 /m:"please select" if %errorlevel%==4 goto end if %errorlevel%==3 goto three if %errorlevel%==2 goto two if %errorlevel%==1 goto one :one echo you choose one goto begin :two echo you choose two goto begin :three echo you choose three goto begin :end echo I want to quit type test.txt 显示文件内容 ren oldtest.txt newtest.txt 重命名文件 copy 复制命令 move 移动文件或 net user Guest 123 /add ;添加用户 net user Guest /delete ;删除用户 arp -a 查看 arp -d 删除 arp -s IP MAC 绑定 ping localhost netstat -an 统计IP TCP UDP ICMP 相关数据 tracert IP 本地到目标IP所经过的路由 nslookup baidu.com 域名解析 telnet IP port 远程连接,需要开启telnet客户端----控制面板----程序---程序功能---启用或关闭windows功能 tasklist /fi "imagename eq cmd.exe" ;查看cmd.exe进程 taskkill /F /IM "imagename eq cmd.exe" taskkill /F /IM "pid eq 1080"
常用批处理命令
最新推荐文章于 2023-11-30 13:46:07 发布