[关闭]
@cyysu 2017-10-12T10:17:29.000000Z 字数 7121 阅读 792

Bat-实战基础篇(一)

  • 时间:2017年10月11日
  • 作者:Kali
  • 邮箱:cyysu.github.io@gmail.com/2869905223@qq.com/微信lwyx1413
  • 版本:4.0
  • 描述:批处理教程基础篇,主要介绍一些语法

Bat系列教程


项目实战

我们这里的例子采用Makefile系列教程-项目篇(一),这两个脚本具有同样的功能。遇到不会的内容时刻查看help xx

  1. # 这里先补充一下基础知识:
  2. 1. 批处理文件命令行参数
  3. %[1-9]表示参数,参数是指在运行批处理文件时在文件名后加的以空格(或者Tab)分隔的字符串。变量可以从%0到%9,%0表示批处理命令本身,其它参数字符串用%1到%9顺序表示。
  4. 2. 常用的基本命令和语法
  5. @echo off 表示不回显命令执行,只显示执行结果
  6. md 表示建立文件夹 make directory
  7. rd 删除文件夹
  8. ren 重命名
  9. move 移动文件
  10. start 可以启动一个应用程序,打开文件夹或者文件
  11. echo 输出描述信息
  12. pause 程序暂停,等待任意按键按下终止程序
  13. dir 列出当前目录
  14. goto 跳转label
  15. rem 表示注释信息
  16. :: 表示注释信息
  17. %% 行注释信息或者获取变量
  18. set 表示设置变量
  19. doskey 给系统设置别名
  20. call 调用另外一个脚本
  21. type 列出文件内容
  22. ^ 取消该符号定义
  23. shift 移位传入的参数,可以使用shift /? 来查看帮助
  24. CLIP 剪切板 CLIP < README.TXT 表示将 readme.txt 的一份文本放入 Windows 剪贴板。
  25. %~f0 表示运行的文件全路径
  26. %* 代表所有参数
  27. %~nx0 表示文件的名字
  28. xcopy 强大的复制,常用示例:xcopy e:\*.* d: /s /h /d /y ::复制E盘所有内容到D
  29. ver 显示系统版本
  30. # 下面来个例子来学习一下上面的命令
  31. @ echo off
  32. ::设置xx变量
  33. set xx=123
  34. ::设置xx1变量
  35. set xx1=456
  36. set x
  37. echo xx变量为: %xx%
  38. ::调用系统变量 ERRORLEVEL表示上一条执行状态
  39. echo %os%
  40. echo %JAVA_HOME%
  41. echo %ERRORLEVEL%
  42. pause
  43. # 执行结果
  44. xx=123
  45. xx1=456
  46. xx value = 123
  47. Windows_NT
  48. D:\software\jdk1.8
  49. 请按任意键继续. . .
  50. # for循环
  51. @echo off
  52. set /a total = 0
  53. :: 只查看文件夹
  54. for /d %%i in (./*) do (
  55. echo %%i
  56. pause
  57. )
  58. ::查看bat文件
  59. for %%x in (*.bat) do (
  60. echo %%x
  61. )
  62. goto :end
  63. :example1
  64. ::/L表示增量形式
  65. for /L %%i in (1,1,5) do (
  66. echo %%i
  67. )
  68. goto :end
  69. :example2
  70. ::查找pdf的个数
  71. for %%x in (*.pdf) do (
  72. echo %%x
  73. set /a total = total + 1
  74. )
  75. goto :end
  76. :example3
  77. ::输出结果信息
  78. echo %total% pdf files
  79. goto :end
  80. :end
  81. pause
  82. # 语法知识
  83. set /A 进行算术的命令
  84. set /p 表示从终端读取
  85. pause >nul 表示不打印输出信息
  86. wait 等待
  1. @ECHO OFF
  2. REM Command file for Sphinx documentation
  3. if "%SPHINXBUILD%" == "" (
  4. set SPHINXBUILD=sphinx-build
  5. )
  6. set BUILDDIR=build
  7. set ALLSPHINXOPTS=-d %BUILDDIR%/doctrees %SPHINXOPTS% source
  8. set I18NSPHINXOPTS=%SPHINXOPTS% source
  9. if NOT "%PAPER%" == "" (
  10. set ALLSPHINXOPTS=-D latex_paper_size=%PAPER% %ALLSPHINXOPTS%
  11. set I18NSPHINXOPTS=-D latex_paper_size=%PAPER% %I18NSPHINXOPTS%
  12. )
  13. if "%1" == "" goto help
  14. if "%1" == "help" (
  15. :help
  16. echo.Please use `make ^<target^>` where ^<target^> is one of
  17. echo. html to make standalone HTML files
  18. echo. dirhtml to make HTML files named index.html in directories
  19. echo. singlehtml to make a single large HTML file
  20. echo. pickle to make pickle files
  21. echo. json to make JSON files
  22. echo. htmlhelp to make HTML files and a HTML help project
  23. echo. qthelp to make HTML files and a qthelp project
  24. echo. devhelp to make HTML files and a Devhelp project
  25. echo. epub to make an epub
  26. echo. latex to make LaTeX files, you can set PAPER=a4 or PAPER=letter
  27. echo. text to make text files
  28. echo. man to make manual pages
  29. echo. texinfo to make Texinfo files
  30. echo. gettext to make PO message catalogs
  31. echo. changes to make an overview over all changed/added/deprecated items
  32. echo. xml to make Docutils-native XML files
  33. echo. pseudoxml to make pseudoxml-XML files for display purposes
  34. echo. linkcheck to check all external links for integrity
  35. echo. doctest to run all doctests embedded in the documentation if enabled
  36. echo. coverage to run coverage check of the documentation if enabled
  37. goto end
  38. )
  39. # /s 表示删除指定目录和其所有子目录 /q 删除内容不需要确认
  40. if "%1" == "clean" (
  41. for /d %%i in (%BUILDDIR%\*) do rmdir /q /s %%i
  42. del /q /s %BUILDDIR%\*
  43. goto end
  44. )
  45. REM Check if sphinx-build is available and fallback to Python version if any
  46. %SPHINXBUILD% 2> nul
  47. if errorlevel 9009 goto sphinx_python
  48. goto sphinx_ok
  49. :sphinx_python
  50. set SPHINXBUILD=python -m sphinx.__init__
  51. %SPHINXBUILD% 2> nul
  52. if errorlevel 9009 (
  53. echo.
  54. echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
  55. echo.installed, then set the SPHINXBUILD environment variable to point
  56. echo.to the full path of the 'sphinx-build' executable. Alternatively you
  57. echo.may add the Sphinx directory to PATH.
  58. echo.
  59. echo.If you don't have Sphinx installed, grab it from
  60. echo.http://sphinx-doc.org/
  61. exit /b 1
  62. )
  63. :sphinx_ok
  64. if "%1" == "html" (
  65. %SPHINXBUILD% -b html %ALLSPHINXOPTS% %BUILDDIR%/html
  66. if errorlevel 1 exit /b 1
  67. echo.
  68. echo.Build finished. The HTML pages are in %BUILDDIR%/html.
  69. goto end
  70. )
  71. if "%1" == "dirhtml" (
  72. %SPHINXBUILD% -b dirhtml %ALLSPHINXOPTS% %BUILDDIR%/dirhtml
  73. if errorlevel 1 exit /b 1
  74. echo.
  75. echo.Build finished. The HTML pages are in %BUILDDIR%/dirhtml.
  76. goto end
  77. )
  78. if "%1" == "singlehtml" (
  79. %SPHINXBUILD% -b singlehtml %ALLSPHINXOPTS% %BUILDDIR%/singlehtml
  80. if errorlevel 1 exit /b 1
  81. echo.
  82. echo.Build finished. The HTML pages are in %BUILDDIR%/singlehtml.
  83. goto end
  84. )
  85. if "%1" == "pickle" (
  86. %SPHINXBUILD% -b pickle %ALLSPHINXOPTS% %BUILDDIR%/pickle
  87. if errorlevel 1 exit /b 1
  88. echo.
  89. echo.Build finished; now you can process the pickle files.
  90. goto end
  91. )
  92. if "%1" == "json" (
  93. %SPHINXBUILD% -b json %ALLSPHINXOPTS% %BUILDDIR%/json
  94. if errorlevel 1 exit /b 1
  95. echo.
  96. echo.Build finished; now you can process the JSON files.
  97. goto end
  98. )
  99. if "%1" == "htmlhelp" (
  100. %SPHINXBUILD% -b htmlhelp %ALLSPHINXOPTS% %BUILDDIR%/htmlhelp
  101. if errorlevel 1 exit /b 1
  102. echo.
  103. echo.Build finished; now you can run HTML Help Workshop with the ^
  104. .hhp project file in %BUILDDIR%/htmlhelp.
  105. goto end
  106. )
  107. if "%1" == "qthelp" (
  108. %SPHINXBUILD% -b qthelp %ALLSPHINXOPTS% %BUILDDIR%/qthelp
  109. if errorlevel 1 exit /b 1
  110. echo.
  111. echo.Build finished; now you can run "qcollectiongenerator" with the ^
  112. .qhcp project file in %BUILDDIR%/qthelp, like this:
  113. echo.^> qcollectiongenerator %BUILDDIR%\qthelp\Python-OperSource-Project-Developer-Guide.qhcp
  114. echo.To view the help file:
  115. echo.^> assistant -collectionFile %BUILDDIR%\qthelp\Python-OperSource-Project-Developer-Guide.ghc
  116. goto end
  117. )
  118. if "%1" == "devhelp" (
  119. %SPHINXBUILD% -b devhelp %ALLSPHINXOPTS% %BUILDDIR%/devhelp
  120. if errorlevel 1 exit /b 1
  121. echo.
  122. echo.Build finished.
  123. goto end
  124. )
  125. if "%1" == "epub" (
  126. %SPHINXBUILD% -b epub %ALLSPHINXOPTS% %BUILDDIR%/epub
  127. if errorlevel 1 exit /b 1
  128. echo.
  129. echo.Build finished. The epub file is in %BUILDDIR%/epub.
  130. goto end
  131. )
  132. if "%1" == "latex" (
  133. %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
  134. if errorlevel 1 exit /b 1
  135. echo.
  136. echo.Build finished; the LaTeX files are in %BUILDDIR%/latex.
  137. goto end
  138. )
  139. if "%1" == "latexpdf" (
  140. %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
  141. cd %BUILDDIR%/latex
  142. make all-pdf
  143. cd %~dp0
  144. echo.
  145. echo.Build finished; the PDF files are in %BUILDDIR%/latex.
  146. goto end
  147. )
  148. if "%1" == "latexpdfja" (
  149. %SPHINXBUILD% -b latex %ALLSPHINXOPTS% %BUILDDIR%/latex
  150. cd %BUILDDIR%/latex
  151. make all-pdf-ja
  152. cd %~dp0
  153. echo.
  154. echo.Build finished; the PDF files are in %BUILDDIR%/latex.
  155. goto end
  156. )
  157. if "%1" == "text" (
  158. %SPHINXBUILD% -b text %ALLSPHINXOPTS% %BUILDDIR%/text
  159. if errorlevel 1 exit /b 1
  160. echo.
  161. echo.Build finished. The text files are in %BUILDDIR%/text.
  162. goto end
  163. )
  164. if "%1" == "man" (
  165. %SPHINXBUILD% -b man %ALLSPHINXOPTS% %BUILDDIR%/man
  166. if errorlevel 1 exit /b 1
  167. echo.
  168. echo.Build finished. The manual pages are in %BUILDDIR%/man.
  169. goto end
  170. )
  171. if "%1" == "texinfo" (
  172. %SPHINXBUILD% -b texinfo %ALLSPHINXOPTS% %BUILDDIR%/texinfo
  173. if errorlevel 1 exit /b 1
  174. echo.
  175. echo.Build finished. The Texinfo files are in %BUILDDIR%/texinfo.
  176. goto end
  177. )
  178. if "%1" == "gettext" (
  179. %SPHINXBUILD% -b gettext %I18NSPHINXOPTS% %BUILDDIR%/locale
  180. if errorlevel 1 exit /b 1
  181. echo.
  182. echo.Build finished. The message catalogs are in %BUILDDIR%/locale.
  183. goto end
  184. )
  185. if "%1" == "changes" (
  186. %SPHINXBUILD% -b changes %ALLSPHINXOPTS% %BUILDDIR%/changes
  187. if errorlevel 1 exit /b 1
  188. echo.
  189. echo.The overview file is in %BUILDDIR%/changes.
  190. goto end
  191. )
  192. if "%1" == "linkcheck" (
  193. %SPHINXBUILD% -b linkcheck %ALLSPHINXOPTS% %BUILDDIR%/linkcheck
  194. if errorlevel 1 exit /b 1
  195. echo.
  196. echo.Link check complete; look for any errors in the above output ^
  197. or in %BUILDDIR%/linkcheck/output.txt.
  198. goto end
  199. )
  200. if "%1" == "doctest" (
  201. %SPHINXBUILD% -b doctest %ALLSPHINXOPTS% %BUILDDIR%/doctest
  202. if errorlevel 1 exit /b 1
  203. echo.
  204. echo.Testing of doctests in the sources finished, look at the ^
  205. results in %BUILDDIR%/doctest/output.txt.
  206. goto end
  207. )
  208. if "%1" == "coverage" (
  209. %SPHINXBUILD% -b coverage %ALLSPHINXOPTS% %BUILDDIR%/coverage
  210. if errorlevel 1 exit /b 1
  211. echo.
  212. echo.Testing of coverage in the sources finished, look at the ^
  213. results in %BUILDDIR%/coverage/python.txt.
  214. goto end
  215. )
  216. if "%1" == "xml" (
  217. %SPHINXBUILD% -b xml %ALLSPHINXOPTS% %BUILDDIR%/xml
  218. if errorlevel 1 exit /b 1
  219. echo.
  220. echo.Build finished. The XML files are in %BUILDDIR%/xml.
  221. goto end
  222. )
  223. if "%1" == "pseudoxml" (
  224. %SPHINXBUILD% -b pseudoxml %ALLSPHINXOPTS% %BUILDDIR%/pseudoxml
  225. if errorlevel 1 exit /b 1
  226. echo.
  227. echo.Build finished. The pseudo-XML files are in %BUILDDIR%/pseudoxml.
  228. goto end
  229. )
  230. :end

打赏

                    支付宝                                                         微信

微信与支付宝支付

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注