Batch Files
by @jhauga
Expert-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...
bin/ directory on PATH for distributing scripts as commands.BAT;.CMD (default on Windows)1. Always start with @echo off and setlocal β Prevents noisy output and variable leakage to the caller.
2. Validate inputs before processing β Check required arguments and file existence early. Use if not defined and if not exist.
3. Quote paths and variables β Use "%~1" and "%_MY_PATH%" to handle spaces and special characters safely.
4. Use exit /b instead of exit β Avoids closing the parent console window.
5. Return meaningful exit codes β exit /b 0 for success, non-zero for specific failures.
6. Use %~dp0 for script-relative paths β Ensures the script works regardless of the caller's working directory.
7. Prefer ROBOCOPY over XCOPY β More reliable, supports retry, mirroring, and logging.
8. Use EnableDelayedExpansion when modifying variables inside loops or parenthesized blocks.
9. Write errors to stderr β echo ERROR: message 1>&2 keeps stdout clean for piping.
10. Use REM for comments β :: can cause issues inside FOR loop bodies.
Security Considerations
&, |, or > can inject commands. Always quote: "%_USER_INPUT%".SETLOCAL β Prevents variable values from leaking to parent processes.DEL, RD, or ROBOCOPY to prevent unintended deletion.SET /P for sensitive input β Input is visible and stored in console history. Use a dedicated credential tool when possible.clawhub install batch-files