{
  "schemaVersion": "0.11.0",
  "canonical": "https://www.pystone.net/notes/bat-script-reference/",
  "atlas": "https://www.pystone.net/?node=bat-script-reference#knowledge-atlas",
  "markdown": "https://www.pystone.net/notes/bat-script-reference.md",
  "context": "https://www.pystone.net/notes/bat-script-reference.context.json",
  "knowledgeVersion": "224c990773de.5fa8af6e39fa",
  "build": {
    "siteCommit": "224c990773de166d23a886306577dd90379529ce",
    "notesCommit": "5fa8af6e39fa3891d1b9b4832bfa6c4e0ecaaf0a",
    "builtAt": "1970-01-01T00:00:00.000Z",
    "version": "224c990773de.5fa8af6e39fa"
  },
  "id": "note:bat-script-reference",
  "slug": "bat-script-reference",
  "title": "【batshell】bat脚本",
  "type": "note",
  "visibility": "public",
  "idStability": "rename-stable",
  "author": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "publisher": {
    "name": "Perrin Yong",
    "profile": "https://www.pystone.net/profile/"
  },
  "aliases": [],
  "summary": "【batshell】bat脚本",
  "contentRole": "unspecified",
  "isMoc": false,
  "mocRecognition": "none",
  "generated": false,
  "attribution": "unspecified",
  "domain": "10-计算机、信息技术与工程",
  "tags": [],
  "mocs": [],
  "contentHash": "343f433e53a1f1b2d151ab8d7cf07a4438a5c5c1f6d630a078a3c9f7dc28419d",
  "assets": [],
  "headings": [
    {
      "depth": 1,
      "text": "【batshell】bat脚本",
      "anchor": "batshellbat脚本",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#batshellbat%E8%84%9A%E6%9C%AC"
    },
    {
      "depth": 2,
      "text": "特性",
      "anchor": "特性",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E7%89%B9%E6%80%A7"
    },
    {
      "depth": 2,
      "text": "Variable",
      "anchor": "variable",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#variable"
    },
    {
      "depth": 3,
      "text": "Assignment",
      "anchor": "assignment",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#assignment"
    },
    {
      "depth": 3,
      "text": "Reading",
      "anchor": "reading",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#reading"
    },
    {
      "depth": 3,
      "text": "Listing",
      "anchor": "listing",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#listing"
    },
    {
      "depth": 3,
      "text": "Variable Scope (Global vs Local)",
      "anchor": "variable-scope-global-vs-local",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#variable-scope-global-vs-local"
    },
    {
      "depth": 3,
      "text": "一些特殊的变量",
      "anchor": "一些特殊的变量",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E4%B8%80%E4%BA%9B%E7%89%B9%E6%AE%8A%E7%9A%84%E5%8F%98%E9%87%8F"
    },
    {
      "depth": 3,
      "text": "Tips",
      "anchor": "tips",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#tips"
    },
    {
      "depth": 2,
      "text": "常用命令",
      "anchor": "常用命令",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4"
    },
    {
      "depth": 3,
      "text": "echo",
      "anchor": "echo",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#echo"
    },
    {
      "depth": 3,
      "text": "rem ::",
      "anchor": "rem",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#rem"
    },
    {
      "depth": 3,
      "text": "pause",
      "anchor": "pause",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#pause"
    },
    {
      "depth": 3,
      "text": "start & call",
      "anchor": "start-call",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#start-call"
    },
    {
      "depth": 4,
      "text": "start",
      "anchor": "start",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#start"
    },
    {
      "depth": 4,
      "text": "call",
      "anchor": "call",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#call"
    },
    {
      "depth": 3,
      "text": "set",
      "anchor": "set",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#set"
    },
    {
      "depth": 4,
      "text": "字符串操作相关",
      "anchor": "字符串操作相关",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%93%8D%E4%BD%9C%E7%9B%B8%E5%85%B3"
    },
    {
      "depth": 3,
      "text": "for",
      "anchor": "for",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#for"
    },
    {
      "depth": 4,
      "text": "无开关",
      "anchor": "无开关",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E6%97%A0%E5%BC%80%E5%85%B3"
    },
    {
      "depth": 4,
      "text": "开关/L",
      "anchor": "开关l",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E5%BC%80%E5%85%B3l"
    },
    {
      "depth": 3,
      "text": "判断语句",
      "anchor": "判断语句",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E5%88%A4%E6%96%AD%E8%AF%AD%E5%8F%A5"
    },
    {
      "depth": 3,
      "text": "goto",
      "anchor": "goto",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#goto"
    },
    {
      "depth": 3,
      "text": "目录相关（创建，删除，判断存在）",
      "anchor": "目录相关创建删除判断存在",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E7%9B%AE%E5%BD%95%E7%9B%B8%E5%85%B3%E5%88%9B%E5%BB%BA%E5%88%A0%E9%99%A4%E5%88%A4%E6%96%AD%E5%AD%98%E5%9C%A8"
    },
    {
      "depth": 4,
      "text": "删除",
      "anchor": "删除",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E5%88%A0%E9%99%A4"
    },
    {
      "depth": 3,
      "text": "copy && xcopy",
      "anchor": "copy-xcopy",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#copy-xcopy"
    },
    {
      "depth": 4,
      "text": "copy - 复制文件",
      "anchor": "copy---复制文件",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#copy---%E5%A4%8D%E5%88%B6%E6%96%87%E4%BB%B6"
    },
    {
      "depth": 4,
      "text": "Xcopy 支持文件/文件夹新建",
      "anchor": "xcopy-支持文件文件夹新建",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#xcopy-%E6%94%AF%E6%8C%81%E6%96%87%E4%BB%B6%E6%96%87%E4%BB%B6%E5%A4%B9%E6%96%B0%E5%BB%BA"
    },
    {
      "depth": 3,
      "text": "move",
      "anchor": "move",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#move"
    },
    {
      "depth": 3,
      "text": "%dp0",
      "anchor": "dp0",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#dp0"
    },
    {
      "depth": 3,
      "text": "日期和时间",
      "anchor": "日期和时间",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#%E6%97%A5%E6%9C%9F%E5%92%8C%E6%97%B6%E9%97%B4"
    },
    {
      "depth": 2,
      "text": "ref",
      "anchor": "ref",
      "citation": "https://www.pystone.net/notes/bat-script-reference/#ref"
    }
  ],
  "claims": [],
  "outgoing": [],
  "incoming": [
    {
      "id": "note:software-engineering-and-quality",
      "title": "软件工程与质量保障",
      "url": "https://www.pystone.net/notes/software-engineering-and-quality/",
      "atlas": "https://www.pystone.net/?node=software-engineering-and-quality#knowledge-atlas",
      "label": "软件工程与质量保障",
      "origin": "explicit",
      "humanReviewed": true,
      "context": "CI-CD与质量保障/CI-CD工具链中的“【batshell】bat脚本”导航项",
      "citation": "https://www.pystone.net/notes/software-engineering-and-quality/#ci-cd%E4%B8%8E%E8%B4%A8%E9%87%8F%E4%BF%9D%E9%9A%9Cci-cd%E5%B7%A5%E5%85%B7%E9%93%BE"
    }
  ],
  "contentMarkdown": "# 【batshell】bat脚本\n\n> 创建时间：2020/9/23 22:06\n\n  * 【bat/shell】bat脚本\n    * 特性\n    * Variable\n      * Assignment\n      * Reading\n      * Listing\n      * Variable Scope (Global vs Local)\n      * 一些特殊的变量\n      * Tips\n    * 常用命令\n      * echo\n      * rem ::\n      * pause\n      * start & call\n        * start\n        * call\n      * set\n        * 字符串操作相关\n      * for\n        * 无开关\n        * 开关/L\n      * 判断语句\n      * goto\n      * 目录相关（创建，删除，判断存在）\n        * 删除\n      * copy && xcopy\n        * copy - 复制文件\n        * Xcopy 支持文件/文件夹新建\n      * move\n      * %~dp0\n      * 日期和时间\n    * ref\n\n## 特性\n\nDOS is case insensitive.\n\n## Variable\n\n### Assignment\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n### Reading\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n### Listing\n\nSET command with no arguments - list all variables for the current command prompt session\n\n> SET will list all regular (static) variables for the current session. This listing excludes the dynamic environmental variables like %DATE% or %CD%.\n\n### Variable Scope (Global vs Local)\n\nBy default, variables are global to your entire command prompt session.\n\n**SETLOCAL** \\- make variables local to the scope of your script.\nAfter calling SETLOCAL, any variable assignments revert upon calling **ENDLOCAL** , calling **EXIT** , or when execution reaches the end of file ( **EOF** ) in your script.\n\n### 一些特殊的变量\n\n`%~dpn` \\- the full path to the parent folder of the ith command line argument.\n`%~dp0` \\- the path of the folder for the script file\n\n> %CD% - 扩展到当前目录字符串。\n>  %DATE% - 用跟 DATE 命令同样的格式扩展到当前日期。\n>  %TIME% - 用跟 TIME 命令同样的格式扩展到当前时间。\n>  %RANDOM% - 扩展到 0 和 32767 之间的任意十进制数字。\n>  %ERRORLEVEL% - 扩展到当前 ERRORLEVEL 数值。\n>  %CMDEXTVERSION% - 扩展到当前命令处理器扩展版本号。\n>  %CMDCMDLINE% - 扩展到调用命令处理器的原始命令行。\n>  %HIGHESTNUMANODENUMBER% - 扩展到此计算机上的最高 NUMA 节点号。\n\n### Tips\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n`SET /?` \\- the help text for SET\n\n## 常用命令\n\n### echo\n\n打开回显或关闭请求回显功能，或显示消息。如果没有任何参数，echo命令将显示当前回显设置。\n\n语法\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n示例：\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n### rem ::\n\n注释\n当关闭回显时，rem和::后的内容都不会显示；\n当打开回显时，rem后的内容会显示出来，然而::后的内容仍然不会显示。\n\n### pause\n\n暂停命令。运行 Pause 命令时，将显示下面的消息：\nPress any key to continue…（或：请按任意键继续…)\n\n### start & call\n\n#### start\n\n调用外部程序，所有的DOS命令和命令行程序都可以由start命令来调用。\n\n> MIN 开始时窗口最小化\n>  SEPARATE 在分开的空间内开始 16 位 Windows 程序\n>  HIGH 在 HIGH 优先级类别开始应用程序\n>  REALTIME 在 REALTIME 优先级类别开始应用程序\n>  WAIT 启动应用程序并等候它结束\n>  parameters 这些为传送到命令/程序的参数\n\n#### call\n\nCall xxx.bat parameter1 p2 p3\n参数字符串若包含空格，必须使用引号，否则被拆成多个参数\n\n### set\n\n  1. 显示变量：\nset 显示批处理当前已定义的所有变量及其值\nset s 显示所有以s开头的变量及值\n\n  2. 设置和调用变量，set aa=abcd，就是把aa定义为abcd。\n\n  3. 删除变量：set aa=\n\n#### 字符串操作相关\n\n字符串截取\nset 目标字符串=%源字符串:~起始值,截取长度%\n\n### for\n\n#### 无开关\n\n`for %%i in (a,\"b c\",d) do echo %%i`\n%%variable是批处理程序里面的书写格式，在DOS中书写为%variable\n\n#### 开关/L\n\n实现对循环次数的直接控制\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n创建5个文件夹:\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n### 判断语句\n\n实用场景：调用bat时若没有传参则退出\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n/b 表示只退出批处理脚本，不退出命令行\n\nDemo2:\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n### goto\n\ngoto 标签 注释内容（可以用作说明goto的条件和执行内容）\n:标签 注释内容（可以用作标签下方段的执行内容）\n\n### 目录相关（创建，删除，判断存在）\n\n```csharp\nstring a = &quot;test&quot;;\nstring tmp = &quot;est&quot;;\nstring b = &quot;t&quot; + tmp;\nConsole.WriteLine(string .ReferenceEquals(str1, str2));\n\n```\n\n判断是否存在： if not exist %Directory%\n删除目录(文件夹)： rd /s /q D:\\NETDATA\n删除文件： del “%Directory%\\%file1%”\n创建目录： md %Directory2%\\picTmp\n\n#### 删除\n\n删除指定目录所有指定文件\ndel /f /s /q /a 你的文件路径*.删除的后缀\n\n> /F 强制删除只读文件。\n>  /S 从所有子目录删除指定文件。\n>  /Q 安静模式。删除全局通配符时，不要求确认。\n>  /A 根据属性选择要删除的文件。\n\n### copy && xcopy\n\n#### copy - 复制文件\n\nfile1和file2表示文件夹，不会新建文件\ncopy a.txt .\\file1\\ 若file1不存在，则显示：系统找不到指定路径\ncopy file1 .\\file2\\ 会把file1中的文件复制到file2中\n\n#### Xcopy 支持文件/文件夹新建\n\ncopy a.txt .\\file1\\ 若file1不存在，则自动创建。 如果file后面去掉斜杠『\\』，系统要求做出判断是建立文件还是文件夹\n\n> 常用选项\n>  /E 复制目录和子目录，包括空的。\n>  /I 如果目标不存在，又在复制一个以上的文件，则假定目标一定是一个目录。\n>  /Y 禁止提示以确认改写一个现存目标文件。\n>  /F 复制时显示完整的源和目标文件名。\n\nxcopy Dir1 Dir2 /E /Y /I /F\n\n### move\n\n移动文件\nmove “file.txt” “%targetDir%”\n\n### %~dp0\n\n%0代表bat文件本身的完整路径\n~dp 是变量扩展选项（d - driver，代表盘符，p - path代表路径）\n\n其他扩展选项：\n\n> **%~f0** \\- 完整的路径+文件名\n>  **%~n0** \\- 文件名(无扩展名)\n>  **%~x0** \\- 文件扩展名\n>  %~s0 - 扩充的路径只含有短名(“s”为Short，短的)\n>  %~a0 - 将 %0 扩充到文件的文件属性(“a”为attribute，即属性)\n>  %~t0 - 将 %0 扩充到文件的日期/时间(“t”time)\n>  %~z0 - 将 %0 扩充到文件的大小(Size 大小)\n\n### 日期和时间\n\n日期\n格式： %date%\n结果： 2012-07-31\n\n时间呢\n格式： %time%\n结果： 10:21:21.68\n\n格式： %date:~x,y%以及%time:~x,y%\n说明： x是开始位置，y是取得字符数\n\n获取完整的日期和时间\n格式： %date:~0,4%%date:~5,2%%date:~8,2%%time:~0,2%%time:~3,2%%time:~6,2%\n结果： 20120731111039\n\n如果时间早于10点，那么只显示日期不显示时间，如：20120731\n格式： %date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%\n结果： 20120731052539\n\n## ref\n\n<https://www.jianshu.com/p/5a1a882ead95>\n"
}
