bat批处理的基本命令和使用方法总结
批处理命令是一种用于在 Windows 操作系统中自动执行任务的工具。批处理命令通常以 .bat 或 .cmd 为扩展名。一些常用的批处理命令如下: echo:用于在控制台显示文本信息。 set:用于创建或修改环境变量。 if:用于在批处理文件中进行条件判断。 for:用于循环读取文件或列表中的数据。 goto:用于跳转到批处理文件中的指定标签。 call:用于调用另一个批处理文件。 md:用于创建新的目录。 cd:用于切换到当前目录。 del:用于删除文件。 copy:用于复制文件。 xcopy:用于复制目录。 start:用于启动应用程序或文件。 taskkill:用于终止运行的任务。 ping:用于测试网络连接。 netstat:用于显示网络状态。 如果你感兴趣,可以私信提问
echo:使用 echo 命令可以在控制台显示文本信息,例如:
echo Hello World!
set:使用 set 命令可以创建或修改环境变量,例如:
set name=John echo My name is %name%
if:使用 if 命令可以在批处理文件中进行条件判断,例如:
set num=10 if %num% lss 20 ( echo %num% is less than 20 ) else ( echo %num% is greater than or equal to 20 )
for:使用 for 命令可以循环读取文件或列表中的数据,例如:
for /f "tokens=1" %i in (file.txt) do ( echo %i )
goto:使用 goto 命令可以跳转到批处理文件中的指定标签,例如:
goto start echo This line will not be executed :start echo This line will be executed
call:使用 call 命令可以调用另一个批处理文件,例如:
call sub.bat echo This line will be executed after sub.bat
md:使用 md 命令可以创建新的目录,例如:
md 123456
cd:使用 cd 命令可以切换到当前目录,例如:
cd newfolder
del:使用 del 命令可以删除文件,例如:
del file.txt
copy 命令可以复制文件,例如:
copy source.txt destination.txt
实例:复制007.txt到当前目录的所有子目录里
在复制 “007.txt” 到子目录之前,使用 if exist 命令判断子目录中是否已经存在 “1.txt”。如果存在,则复制该文件。否则,不复制。并且做出相应的提示在运行该脚本之前,请确保 “1.txt” 在当前目录中。
@echo off for /d %%d in (*) do ( if exist "%%d 07.txt" ( echo %%d"...........exist..............." ) else ( copy 007.txt "%%d" echo %%d "...........ok..............." ) ) pause
xcopy:使用 xcopy 命令可以复制目录,例如:
xcopy sourcefolder destinationfolder /s
pause:使用 pause 命令可以暂停批处理文件的执行,等待用户按下任意键,例如:
pause
复制多个 txt 文件中固定两行位置的数据,并把读取的数据一起放到同一个新的 txt 文件。
@echo off set "outputfile=output.txt" REM 清空输出文件 echo. > %outputfile% REM 遍历所有 txt 文件 for /f "delims=" %%i in (dir /b *.txt) do ( REM 读取每个文件的第二行和第三行 for /f "skip=1 tokens=*" %%j in (type "%%i") do ( echo %%j >> %outputfile% goto :next ) :next for /f "skip=2 tokens=*" %%j in (type "%%i") do ( echo %%j >> %outputfile% ) ) echo 数据已经提取到文件 %outputfile% 中。
用批处理文件自动连接网络大打印机,win7,win10通用那种
@echo off set printer=\network-printerprinter-name rundll32 printui.dll,PrintUIEntry /in /n "%printer%"
该脚本使用 rundll32 命令连接网络打印机。您只需要修改 printer 变量的值,替换为网络打印机的名称和路径即可。在运行该脚本之前,请确保您已经有了访问网络打印机的权限。
把文件名末尾 -数字 消去,并重新命名。例如ab-cd-0102.jpg修改成ab-cd.jpg ,jui-eh-dj-02088.jpg修改成jui-eh-dj.jpg
下一篇:
Linux系统下搭建maven环境