The most commonly used commands for batch files in the BAT scripting tutorial simple introduction:

Some of the most commonly used commands in batch files:

echoIndicates that the characters after this command are displayed 

echo on means that all commands run after this statement display the command line itself 
echo off means that all commands run after this statement do not display the command line itself
@Similar to echo off, but it is added at the beginning of each command line, indicating that the command line of this line is not displayed at runtime (only affects the current line).
call Call another batch file (if you don't call another batch file directly, after executing that batch file, you will not be able to return to the current file and execute subsequent commands of the current file).
pause Running this sentence will pause the execution of the batch and display the prompt Press any key to continue... on the screen, waiting for the user to press any key to continue
rem Indicates that the characters after this command are comments and will not be executed.

title  BAT's title

cls clear screen


Starting example:

[plain]  view plain copy  
  1. <span style="font-family:SimSun;font-size:14px;">@ECHO OFF  
  2. TITLE BAT script example 1  
  3. echo ------------- enumerates all files in the C drive directory -------------  
  4. echo=  
  5. echo=  
  6. dir c:\*.*  
  7. rem output to text file  
  8. dir c:\*.* > example1.txt  
  9. echo=  
  10. echo=  
  11. echo --------------------------------------------  
  12. PAUSE</span>  

echo= means to output blank lines. There are other ways to output blank lines. For details, please refer to the website:

http://blog.sina.com.cn/s/blog_4b466ad00101dfqu.html

If you enter PAUSE>NUL, it means to pause and not prompt "Press any key to continue".


Set font color and form size:

设置字体颜色:COLOR 02 (0代表背景色,2代表前景色)

常用的颜色有以下值:0 黑色,1蓝色,2 绿色,3 浅绿色,4红色,5紫色,6黄色,7白色,8灰色,9浅蓝,A浅绿,B浅蓝色,C浅红色,D浅紫色,E浅黄色,F亮白色)。

设置窗体大小:MODE CON: COLS=宽度 LINES=高度


文件夹简单操作:

[plain]  view plain  copy
  1. <span style="font-family:SimSun;font-size:14px;">@ECHO OFF  
  2. TITLE BAT脚本例子2  
  3. COLOR A  
  4. echo -----------BAT脚本例子2-----------  
  5. echo=  
  6. echo=  
  7. echo  当前工作路径为:%cd%  
  8. rem 输出文件目录的树形目录  
  9. TREE /f >tree_list.txt  
  10. rem CD切换不同盘符时候需要加上/d  
  11. CD /D C:\  
  12. echo  当前工作路径为:%cd%  
  13. DIR  
  14. rem 创建目录bat_example2  
  15. MD bat_example2  
  16. DIR  
  17. rem 拷贝目录 /s /e /y 说明:在复制文件的同时也复制空目录或子目录,如果目标路径已经有相同文件了,使用覆盖方式而不进行提示  
  18. Xcopy C:\bat_example2 D:\bat_example2  /s /e /y  
  19.   
  20. rem 删除目录bat_example2  
  21. rem RD /Q /S bat_example2  
  22. rem DIR  
  23. echo=  
  24. echo=  
  25. echo --------------------------------------------  
  26. PAUSE</span>  
关于文件夹的其他操作,可参考网址:http://www.jb51.net/article/11313.htm

文件操作

[plain]  view plain  copy
  1. <span style="font-family:SimSun;font-size:14px;">@ECHO OFF  
  2. TITLE BAT脚本例子3  
  3. COLOR A  
  4. echo -----------BAT脚本例子3-----------  
  5. echo=  
  6. echo=  
  7. TYPE tree_list1.txt  
  8. rem 复制(合并)文件 /Y 表示目标路径存在该文件则不提示直接覆盖  
  9. COPY /Y tree_list2.txt + tree_list3.txt C:\  
  10.   
  11. DEL tree_list4.txt /f /s /q /a   
  12. rem /f 表示强制删除文件   
  13. rem /s表示子目录都要删除该文件   
  14. rem /q表示无声,不提示   
  15. rem /a根据属性选择要删除的文件   
  16.   
  17. rem 需要特别注意的是:move不能跨分区移动文件夹  
  18. MOVE example3 example3_1  
  19. echo=  
  20. echo=  
  21. echo --------------------------------------------  
  22. PAUSE</span>  

网络命令

[plain]  view plain  copy
  1. <span style="font-size:14px;">@ECHO OFF  
  2. TITLE BAT脚本例子4  
  3. COLOR A  
  4. echo -----------BAT脚本例子4-----------  
  5. echo=   
  6. PING www.baidu.com  
  7. echo=  
  8. echo -----------------------------------  
  9. IPCONFIG  
  10. echo=  
  11. echo -----------------------------------  
  12. ARP   
  13. echo=  
  14. echo -----------------------------------  
  15. PAUSE</span>  

系统相关

[plain]  view plain  copy
  1. <span style="font-size:14px;">@ECHO OFF  
  2. TITLE BAT脚本例子5  
  3. COLOR A  
  4. echo -----------BAT脚本例子5-----------  
  5. echo=   
  6. echo -----------显示计算机用户-----------  
  7. NET USER  
  8. echo=  
  9. echo -----------显示进程列表-----------  
  10. TASKLIST  
  11. echo=  
  12. echo -----------------------------------  
  13. PAUSE</span>  

       To sum up, in fact, BAT mainly uses DOS commands, so as long as you master DOS commands, using BAT is much easier. Of course, the actual application of BAT is not only these simple commands, but also more complex syntax, which will be introduced in the next article.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325387061&siteId=291194637