$npx -y skills add github/awesome-copilot --skill batch-filesExpert-level Windows batch file (.bat/.cmd) skill for writing, debugging, and maintaining CMD scripts. Use when asked to "create a batch file", "write a .bat script", "automate a Windows task", "CMD scripting", "batch automation", "scheduled task script", "Windows shell script",
| 1 | # Batch Files |
| 2 | |
| 3 | A comprehensive skill for creating, editing, debugging, and maintaining Windows batch files (.bat/.cmd) using cmd.exe. Applies to CLI tool development, system administration automation, scheduled tasks, file operations scripting, and PATH-based executable scripts. |
| 4 | |
| 5 | ## When to Use This Skill |
| 6 | |
| 7 | - Creating or editing `.bat` or `.cmd` files |
| 8 | - Automating Windows tasks (file operations, deployments, backups) |
| 9 | - Building CLI tools intended for a `bin/` folder on PATH |
| 10 | - Writing scheduled task scripts (SCHTASKS, Task Scheduler) |
| 11 | - Debugging batch script issues (variable expansion, error levels, quoting) |
| 12 | - Integrating batch scripts with external tools (curl, git, Node.js, Python) |
| 13 | - Scaffolding new batch-based projects with structured templates |
| 14 | |
| 15 | ## Prerequisites |
| 16 | |
| 17 | - Windows NT-based OS (Windows 7 or later) |
| 18 | - cmd.exe (built-in) |
| 19 | - Optional: a `bin/` directory on PATH for distributing scripts as commands |
| 20 | - Optional: PATHEXT configured to include `.BAT;.CMD` (default on Windows) |
| 21 | |
| 22 | ## Command Interpretation |
| 23 | |
| 24 | cmd.exe processes each line through four stages in order: |
| 25 | |
| 26 | 1. **Variable substitution** — `%VAR%` tokens are replaced with environment variable values. `%0`–`%9` reference batch arguments. `%*` expands to all arguments. |
| 27 | 2. **Quoting and escaping** — Caret `^` escapes special characters (`& | < > ^`). Quotation marks prevent interpretation of enclosed special characters. In batch files, `%%` yields a literal `%`. |
| 28 | 3. **Syntax parsing** — Lines are split into pipelines (`|`), compound commands (`&`, `&&`, `||`), and parenthesized groups `( )`. |
| 29 | 4. **Redirection** — `>` overwrites, `>>` appends, `<` reads input, `2>` redirects stderr, `2>&1` merges stderr into stdout, `>NUL` discards output. |
| 30 | |
| 31 | ## Variables |
| 32 | |
| 33 | ### Environment Variables |
| 34 | |
| 35 | ```bat |
| 36 | set _MY_VAR=Hello World |
| 37 | echo %_MY_VAR% |
| 38 | set _MY_VAR= |
| 39 | ``` |
| 40 | |
| 41 | - `set` with no arguments lists all variables |
| 42 | - `set _PREFIX` lists variables starting with `_PREFIX` |
| 43 | - No spaces around `=` — `set name = val` sets variable `"name "` to `" val"` |
| 44 | |
| 45 | ### Special Variables |
| 46 | |
| 47 | | Variable | Value | |
| 48 | |----------|-------| |
| 49 | | `%CD%` | Current directory | |
| 50 | | `%DATE%` | System date (locale-dependent) | |
| 51 | | `%TIME%` | System time HH:MM:SS.mm | |
| 52 | | `%RANDOM%` | Pseudorandom number 0–32767 | |
| 53 | | `%ERRORLEVEL%` | Exit code of last command | |
| 54 | | `%USERNAME%` | Current user name | |
| 55 | | `%USERPROFILE%` | Current user profile path | |
| 56 | | `%TEMP%` / `%TMP%` | Temporary file directory | |
| 57 | | `%PATHEXT%` | Executable extensions list | |
| 58 | | `%COMSPEC%` | Path to cmd.exe | |
| 59 | |
| 60 | ### Scoping with SETLOCAL / ENDLOCAL |
| 61 | |
| 62 | ```bat |
| 63 | setlocal |
| 64 | set _LOCAL_VAR=scoped value |
| 65 | endlocal |
| 66 | REM _LOCAL_VAR is no longer defined here |
| 67 | ``` |
| 68 | |
| 69 | To return a value from a scoped block: |
| 70 | |
| 71 | ```bat |
| 72 | endlocal & set _RESULT=%_LOCAL_VAR% |
| 73 | ``` |
| 74 | |
| 75 | ### Delayed Expansion |
| 76 | |
| 77 | Variables inside parenthesized blocks are expanded at parse time. Use delayed expansion for runtime evaluation: |
| 78 | |
| 79 | ```bat |
| 80 | setlocal EnableDelayedExpansion |
| 81 | set _COUNT=0 |
| 82 | for /l %%i in (1,1,5) do ( |
| 83 | set /a _COUNT+=1 |
| 84 | echo !_COUNT! |
| 85 | ) |
| 86 | endlocal |
| 87 | ``` |
| 88 | |
| 89 | - `!VAR!` expands at execution time (delayed) |
| 90 | - `%VAR%` expands at parse time (immediate) |
| 91 | |
| 92 | ## Control Flow |
| 93 | |
| 94 | ### Conditional Execution |
| 95 | |
| 96 | ```bat |
| 97 | if exist "output.txt" echo File found |
| 98 | if not defined _MY_VAR echo Variable not set |
| 99 | if "%_STATUS%"=="ready" (echo Go) else (echo Wait) |
| 100 | if %ERRORLEVEL% neq 0 echo Command failed |
| 101 | ``` |
| 102 | |
| 103 | Comparison operators: `equ`, `neq`, `lss`, `leq`, `gtr`, `geq`. Use `/i` for case-insensitive string comparison. |
| 104 | |
| 105 | ### Compound Commands |
| 106 | |
| 107 | ```bat |
| 108 | command1 & command2 & REM Always run both |
| 109 | command1 && command2 & REM Run command2 only if command1 succeeds |
| 110 | command1 || command2 & REM Run command2 only if command1 fails |
| 111 | ``` |
| 112 | |
| 113 | ### FOR Loops |
| 114 | |
| 115 | ```bat |
| 116 | REM Iterate over a set of values |
| 117 | for %%i in (alpha beta gamma) do echo %%i |
| 118 | |
| 119 | REM Numeric range: start, step, end |
| 120 | for /l %%i in (1,1,10) do echo %%i |
| 121 | |
| 122 | REM Files in a directory |
| 123 | for %%f in (*.txt) do echo %%f |
| 124 | |
| 125 | REM Recursive file search |
| 126 | for /r %%f in (*.log) do echo %%f |
| 127 | |
| 128 | REM Directories only |
| 129 | for /d %%d in (*) do echo %%d |
| 130 | |
| 131 | REM Parse command output |
| 132 | for /f "tokens=1,2 delims=:" %%a in ('ipconfig ^| findstr "IPv4"') do echo %%b |
| 133 | |
| 134 | REM Parse file lines |
| 135 | for /f "usebackq tokens=*" %%a in ("data.txt") do echo %%a |
| 136 | ``` |
| 137 | |
| 138 | ### GOTO and Labels |
| 139 | |
| 140 | ```bat |
| 141 | goto :main_logic |
| 142 | :usage |
| 143 | echo Usage: %~nx0 [options] |
| 144 | exit /b 1 |
| 145 | |
| 146 | :main_logic |
| 147 | echo Running main logic... |
| 148 | goto :eof |
| 149 | ``` |
| 150 | |
| 151 | `goto :eof` exits the current batch or subroutine. Labels start with `:`. |
| 152 | |
| 153 | ## Command-Line Arguments |
| 154 | |
| 155 | | Syntax | Value | |
| 156 | |--------|-------| |
| 157 | | `%0` | Script name as invoked |