Loading...
Loading...
Compare original and translation side by side
.bat.cmdbin/.bat.cmdbin/bin/.BAT;.CMDbin/.BAT;.CMD%VAR%%0%9%*^& | < > ^%%%|&&&||( )>>><2>2>&1>NUL%VAR%%0%9%*^& | < > ^%%%|&&&||( )>>><2>2>&1>NULset _MY_VAR=Hello World
echo %_MY_VAR%
set _MY_VAR=setset _PREFIX_PREFIX=set name = val"name "" val"set _MY_VAR=Hello World
echo %_MY_VAR%
set _MY_VAR=setset _PREFIX_PREFIX=set name = val"name "" val"| Variable | Value |
|---|---|
| Current directory |
| System date (locale-dependent) |
| System time HH:MM:SS.mm |
| Pseudorandom number 0–32767 |
| Exit code of last command |
| Current user name |
| Current user profile path |
| Temporary file directory |
| Executable extensions list |
| Path to cmd.exe |
| 变量 | 说明 |
|---|---|
| 当前目录 |
| 系统日期(依赖区域设置) |
| 系统时间 HH:MM:SS.mm |
| 伪随机数 0–32767 |
| 上一条命令的退出码 |
| 当前用户名 |
| 当前用户配置文件路径 |
| 临时文件目录 |
| 可执行文件扩展名列表 |
| cmd.exe的路径 |
setlocal
set _LOCAL_VAR=scoped value
endlocal
REM _LOCAL_VAR is no longer defined hereendlocal & set _RESULT=%_LOCAL_VAR%setlocal
set _LOCAL_VAR=scoped value
endlocal
REM _LOCAL_VAR在此处不再定义endlocal & set _RESULT=%_LOCAL_VAR%setlocal EnableDelayedExpansion
set _COUNT=0
for /l %%i in (1,1,5) do (
set /a _COUNT+=1
echo !_COUNT!
)
endlocal!VAR!%VAR%setlocal EnableDelayedExpansion
set _COUNT=0
for /l %%i in (1,1,5) do (
set /a _COUNT+=1
echo !_COUNT!
)
endlocal!VAR!%VAR%if exist "output.txt" echo File found
if not defined _MY_VAR echo Variable not set
if "%_STATUS%"=="ready" (echo Go) else (echo Wait)
if %ERRORLEVEL% neq 0 echo Command failedequneqlssleqgtrgeq/iif exist "output.txt" echo File found
if not defined _MY_VAR echo Variable not set
if "%_STATUS%"=="ready" (echo Go) else (echo Wait)
if %ERRORLEVEL% neq 0 echo Command failedequneqlssleqgtrgeq/icommand1 & command2 & REM Always run both
command1 && command2 & REM Run command2 only if command1 succeeds
command1 || command2 & REM Run command2 only if command1 failscommand1 & command2 & REM 始终执行两条命令
command1 && command2 & REM 仅当command1成功时才执行command2
command1 || command2 & REM 仅当command1失败时才执行command2REM Iterate over a set of values
for %%i in (alpha beta gamma) do echo %%i
REM Numeric range: start, step, end
for /l %%i in (1,1,10) do echo %%i
REM Files in a directory
for %%f in (*.txt) do echo %%f
REM Recursive file search
for /r %%f in (*.log) do echo %%f
REM Directories only
for /d %%d in (*) do echo %%d
REM Parse command output
for /f "tokens=1,2 delims=:" %%a in ('ipconfig ^| findstr "IPv4"') do echo %%b
REM Parse file lines
for /f "usebackq tokens=*" %%a in ("data.txt") do echo %%aREM 遍历一组值
for %%i in (alpha beta gamma) do echo %%i
REM 数值范围:起始值, 步长, 结束值
for /l %%i in (1,1,10) do echo %%i
REM 目录中的文件
for %%f in (*.txt) do echo %%f
REM 递归搜索文件
for /r %%f in (*.log) do echo %%f
REM 仅遍历目录
for /d %%d in (*) do echo %%d
REM 解析命令输出
for /f "tokens=1,2 delims=:" %%a in ('ipconfig ^| findstr "IPv4"') do echo %%b
REM 解析文件行
for /f "usebackq tokens=*" %%a in ("data.txt") do echo %%agoto :main_logic
:usage
echo Usage: %~nx0 [options]
exit /b 1
:main_logic
echo Running main logic...
goto :eofgoto :eof:goto :main_logic
:usage
echo Usage: %~nx0 [options]
exit /b 1
:main_logic
echo Running main logic...
goto :eofgoto :eof:| Syntax | Value |
|---|---|
| Script name as invoked |
| Positional arguments |
| All arguments (unaffected by SHIFT) |
| Argument 1 with enclosing quotes removed |
| Full path of argument 1 |
| Drive letter of argument 1 |
| Path (without drive) of argument 1 |
| File name (no extension) of argument 1 |
| Extension of argument 1 |
| Drive and path of the batch file itself |
| File name with extension of the batch file |
| File size of argument 1 |
| Search PATH for argument 1 |
| 语法 | 说明 |
|---|---|
| 调用时的脚本名称 |
| 位置参数 |
| 所有参数(不受SHIFT影响) |
| 移除引号后的参数1 |
| 参数1的完整路径 |
| 参数1的驱动器号 |
| 参数1的路径(不含驱动器) |
| 参数1的文件名(不含扩展名) |
| 参数1的扩展名 |
| 批处理文件自身的驱动器和路径 |
| 批处理文件的文件名(含扩展名) |
| 参数1的文件大小 |
| 在PATH中搜索参数1 |
:parse_args
if "%~1"=="" goto :args_done
if /i "%~1"=="--help" goto :usage
if /i "%~1"=="--output" (
set "_OUTPUT_DIR=%~2"
shift
)
shift
goto :parse_args
:args_done:parse_args
if "%~1"=="" goto :args_done
if /i "%~1"=="--help" goto :usage
if /i "%~1"=="--output" (
set "_OUTPUT_DIR=%~2"
shift
)
shift
goto :parse_args
:args_doneset _STR=Hello World
echo %_STR:~0,5% & REM "Hello"
echo %_STR:~6% & REM "World"
echo %_STR:~-5% & REM "World"
echo %_STR:~0,-6% & REM "Hello"set _STR=Hello World
echo %_STR:~0,5% & REM "Hello"
echo %_STR:~6% & REM "World"
echo %_STR:~-5% & REM "World"
echo %_STR:~0,-6% & REM "Hello"set _STR=Hello World
echo %_STR:World=Earth% & REM "Hello Earth"
echo %_STR:Hello=% & REM " World" (remove "Hello")set _STR=Hello World
echo %_STR:World=Earth% & REM "Hello Earth"
echo %_STR:Hello=% & REM " World"(移除"Hello")if not "%_STR:World=%"=="%_STR%" echo Contains "World"if not "%_STR:World=%"=="%_STR%" echo Contains "World"@echo off
call :greet "Jane Doe"
echo Result: %_GREETING%
exit /b 0
:greet
setlocal
set "_MSG=Hello, %~1"
endlocal & set "_GREETING=%_MSG%"
exit /b 0call :label argsexit /bendlocal & set@echo off
call :greet "Jane Doe"
echo Result: %_GREETING%
exit /b 0
:greet
setlocal
set "_MSG=Hello, %~1"
endlocal & set "_GREETING=%_MSG%"
exit /b 0call :label argsexit /bendlocal & setset /aset /a _RESULT=10 * 5 + 3
set /a _COUNTER+=1
set /a _REMAINDER=14 %% 3 & REM Use %% for modulo in batch files
set /a _BITS="255 & 0x0F" & REM Bitwise AND+ - * / %% ( )& | ^ ~ << >>0xFF077set /aset /a _RESULT=10 * 5 + 3
set /a _COUNTER+=1
set /a _REMAINDER=14 %% 3 & REM 在批处理文件中使用%%表示取模
set /a _BITS="255 & 0x0F" & REM 按位与+ - * / %% ( )& | ^ ~ << >>0xFF07701mycommand.exe
if %ERRORLEVEL% neq 0 (
echo ERROR: mycommand failed with code %ERRORLEVEL%
exit /b %ERRORLEVEL%
)01mycommand.exe
if %ERRORLEVEL% neq 0 (
echo ERROR: mycommand failed with code %ERRORLEVEL%
exit /b %ERRORLEVEL%
)command1 || (echo command1 failed & exit /b 1)
command2 || (echo command2 failed & exit /b 1)command1 || (echo command1 failed & exit /b 1)
command2 || (echo command2 failed & exit /b 1)exit /b 0 & REM Return success from a batch/function
exit /b 1 & REM Return failure
cmd /c "exit /b 42" & REM Set ERRORLEVEL to 42 inlineexit /b 0 & REM 从批处理/函数返回成功
exit /b 1 & REM 返回失败
cmd /c "exit /b 42" & REM 内联设置ERRORLEVEL为42| Command | Purpose |
|---|---|
| List directory contents |
| Copy files |
| Extended copy with subdirectories (legacy) |
| Robust copy with retry, mirror, logging |
| Move or rename files |
| Delete files |
| Rename files |
| Create directories |
| Remove directories |
| Create symbolic or hard links |
| View or set file attributes |
| Print file contents |
| Paginated file display |
| Display directory structure |
| Replace files in destination with source |
| Show or set NTFS compression |
| Extract from .cab files |
| Create .cab archives |
| Create or extract tar archives |
| 命令 | 用途 |
|---|---|
| 列出目录内容 |
| 复制文件 |
| 支持子目录的扩展复制(传统工具) |
| 具备重试、镜像、日志功能的可靠复制工具 |
| 移动或重命名文件 |
| 删除文件 |
| 重命名文件 |
| 创建目录 |
| 删除目录 |
| 创建符号链接或硬链接 |
| 查看或设置文件属性 |
| 打印文件内容 |
| 分页显示文件内容 |
| 显示目录结构 |
| 用源文件替换目标目录中的文件 |
| 查看或设置NTFS压缩 |
| 从.cab文件中提取内容 |
| 创建.cab归档文件 |
| 创建或提取tar归档文件 |
| Command | Purpose |
|---|---|
| Search for literal strings |
| Search with limited regular expressions |
| Sort lines alphabetically |
| Copy piped input to clipboard |
| Compare two files |
| Binary file comparison |
| Encode/decode Base64, compute hashes |
| 命令 | 用途 |
|---|---|
| 搜索字面字符串 |
| 使用有限正则表达式搜索 |
| 按字母顺序排序行 |
| 将管道输入复制到剪贴板 |
| 比较两个文件 |
| 二进制文件比较 |
| Base64编码/解码、计算哈希值 |
| Command | Purpose |
|---|---|
| Full system configuration |
| Display computer name |
| Windows version |
| Current user and group info |
| List running processes |
| Terminate processes |
| WMI queries (drives, OS, memory) |
| Service control (query, start, stop) |
| List installed drivers |
| Registry operations (query, add, delete) |
| Set persistent environment variables |
| 命令 | 用途 |
|---|---|
| 完整系统配置信息 |
| 显示计算机名称 |
| Windows版本 |
| 当前用户和组信息 |
| 列出运行中的进程 |
| 终止进程 |
| WMI查询(驱动器、操作系统、内存等) |
| 服务控制(查询、启动、停止) |
| 列出已安装的驱动程序 |
| 注册表操作(查询、添加、删除) |
| 设置持久化环境变量 |
| Command | Purpose |
|---|---|
| Test network connectivity |
| IP configuration |
| DNS lookup |
| Network connections and ports |
| Trace route to host |
| Map/disconnect network drives |
| Manage user accounts |
| Network configuration utility |
| ARP cache management |
| Routing table management |
| HTTP requests (Windows 10+) |
| Secure shell (Windows 10+) |
| 命令 | 用途 |
|---|---|
| 测试网络连通性 |
| IP配置信息 |
| DNS查询 |
| 网络连接和端口信息 |
| 追踪到主机的路由 |
| 映射/断开网络驱动器 |
| 管理用户账户 |
| 网络配置工具 |
| ARP缓存管理 |
| 路由表管理 |
| HTTP请求(Windows 10+) |
| 安全Shell(Windows 10+) |
| Command | Purpose |
|---|---|
| Create and manage scheduled tasks |
| Wait N seconds (Vista+) |
| Launch programs asynchronously |
| Run as different user |
| Shutdown or restart |
| Find files by date and execute commands |
| 命令 | 用途 |
|---|---|
| 创建和管理计划任务 |
| 等待N秒(Vista+) |
| 异步启动程序 |
| 以其他用户身份运行 |
| 关机或重启 |
| 按日期查找文件并执行命令 |
| Command | Purpose |
|---|---|
| Locate executables in PATH |
| Create command macros |
| Prompt for single-key input |
| Configure console size and ports |
| Map folder to drive letter |
| Get or set console code page |
| Set console colors |
| Set console window title |
| File type associations |
| 命令 | 用途 |
|---|---|
| 在PATH中查找可执行文件 |
| 创建命令宏 |
| 提示单键输入 |
| 配置控制台大小和端口 |
| 将文件夹映射为驱动器号 |
| 获取或设置控制台代码页 |
| 设置控制台颜色 |
| 设置控制台窗口标题 |
| 文件类型关联 |
(echo Line 1 & echo Line 2) > output.txt
if exist "data.csv" (
echo Processing...
call :process "data.csv"
) else (
echo No data found.
)(echo Line 1 & echo Line 2) > output.txt
if exist "data.csv" (
echo Processing...
call :process "data.csv"
) else (
echo No data found.
)^echo Total ^& Summary & REM Outputs: Total & Summary
echo 100%% complete & REM Outputs: 100% complete (in batch)
echo Line one^
Line two & REM Caret escapes the newlineecho x ^^^& y | findstr x^echo Total ^& Summary & REM 输出:Total & Summary
echo 100%% complete & REM 输出:100% complete(批处理文件中)
echo Line one^
Line two & REM 脱字符转义换行符echo x ^^^& y | findstr x*?dir *.txt & REM All .txt files
ren *.jpeg *.jpg & REM Bulk rename*?dir *.txt & REM 所有.txt文件
ren *.jpeg *.jpg & REM 批量重命名command > file.txt & REM Overwrite stdout to file
command >> file.txt & REM Append stdout to file
command 2> errors.log & REM Redirect stderr
command > all.log 2>&1 & REM Merge stderr into stdout
command < input.txt & REM Read stdin from file
command > NUL 2>&1 & REM Discard all outputcommand > file.txt & REM 将stdout覆盖写入文件
command >> file.txt & REM 将stdout追加写入文件
command 2> errors.log & REM 重定向stderr
command > all.log 2>&1 & REM 将stderr合并到stdout
command < input.txt & REM 从文件读取stdin
command > NUL 2>&1 & REM 丢弃所有输出@echo off
setlocal EnableDelayedExpansion
REM ============================================================
REM Script: example.bat
REM Purpose: Describe what this script does
REM ============================================================
call :main %*
exit /b %ERRORLEVEL%
:main
call :parse_args %*
if not defined _TARGET (
echo ERROR: --target is required. 1>&2
call :usage
exit /b 1
)
echo Processing: %_TARGET%
exit /b 0
:parse_args
if "%~1"=="" exit /b 0
if /i "%~1"=="--target" set "_TARGET=%~2" & shift
if /i "%~1"=="--help" call :usage & exit /b 0
shift
goto :parse_args
:usage
echo Usage: %~nx0 --target ^<path^> [--help]
echo.
echo Options:
echo --target Path to process (required)
echo --help Show this help message
exit /b 0@echo off
setlocal EnableDelayedExpansion
REM ============================================================
REM Script: example.bat
REM Purpose: Describe what this script does
REM ============================================================
call :main %*
exit /b %ERRORLEVEL%
:main
call :parse_args %*
if not defined _TARGET (
echo ERROR: --target is required. 1>&2
call :usage
exit /b 1
)
echo Processing: %_TARGET%
exit /b 0
:parse_args
if "%~1"=="" exit /b 0
if /i "%~1"=="--target" set "_TARGET=%~2" & shift
if /i "%~1"=="--help" call :usage & exit /b 0
shift
goto :parse_args
:usage
echo Usage: %~nx0 --target ^<path^> [--help]
echo.
echo Options:
echo --target Path to process (required)
echo --help Show this help message
exit /b 0@echo offsetlocalif not definedif not exist"%~1""%_MY_PATH%"exit /bexitexit /b 0%~dp0ROBOCOPYXCOPYEnableDelayedExpansionecho ERROR: message 1>&2REM::FOR@echo offsetlocalif not definedif not exist"%~1""%_MY_PATH%"exit /bexitexit /b 0%~dp0ROBOCOPYXCOPYEnableDelayedExpansionecho ERROR: message 1>&2REM::&|>"%_USER_INPUT%"SETLOCALDELRDROBOCOPYSET /P&|>"%_USER_INPUT%"SETLOCALDELRDROBOCOPYSET /P| Technique | How |
|---|---|
| Trace execution | Remove |
| Step through | Add |
| Check error level | |
| Inspect variables | |
| Delayed expansion issues | Variable inside |
FOR loop | Use |
| Spaces in SET | |
| Caret in pipes | After a pipe, use |
| Parentheses in SET /A | Escape with |
| Double percent for modulo | |
| 技巧 | 操作方法 |
|---|---|
| 跟踪执行过程 | 临时移除 |
| 逐步执行 | 在各部分之间添加 |
| 检查错误级别 | 每条命令后执行 |
| 检查变量 | 使用 |
| 延迟扩展问题 | 括号块内的变量未更新?启用 |
FOR循环中的 | 批处理文件中使用 |
| SET命令中的空格 | 使用 |
| 管道中的脱字符 | 管道后使用 |
| SET /A中的括号 | 在if块内使用 |
| 取模使用双百分号 | 批处理文件中使用 |
| Tool | Purpose |
|---|---|
| Cygwin | Full POSIX environment on Windows (grep, sed, awk, ssh) |
| MSYS2 | Lightweight Unix tools and package manager (pacman) |
| WSL | Windows Subsystem for Linux — run native Linux binaries |
| GnuWin32 | Individual GNU utilities as native Windows executables |
| PowerShell | Modern Windows scripting with .NET integration |
| 工具 | 用途 |
|---|---|
| Cygwin | Windows上的完整POSIX环境(grep、sed、awk、ssh) |
| MSYS2 | 轻量级Unix工具和包管理器(pacman) |
| WSL | Windows Subsystem for Linux — 运行原生Linux二进制文件 |
| GnuWin32 | 作为原生Windows可执行文件的单个GNU工具 |
| PowerShell | 集成.NET的现代Windows脚本工具 |
| Shortcut | Action |
|---|---|
| Auto-complete file/folder names |
| Navigate command history |
| Show command history popup |
| Repeat last command |
| Clear current line |
| Cancel running command |
| Clear command history |
| 快捷键 | 操作 |
|---|---|
| 自动补全文件/文件夹名称 |
| 浏览命令历史 |
| 显示命令历史弹窗 |
| 重复上一条命令 |
| 清除当前行 |
| 取消正在运行的命令 |
| 清除命令历史 |
references/| File | Contents |
|---|---|
| Windows tools, utilities, package managers, terminals |
| Example scripts, techniques, best practices links |
| Comprehensive A-Z Windows command reference |
| Cygwin user guide and FAQ |
| MSYS2 installation, packages, and environments |
| WSL setup, commands, and documentation |
references/| 文件 | 内容 |
|---|---|
| Windows工具、实用程序、包管理器、终端 |
| 示例脚本、技巧、最佳实践链接 |
| 全面的A-Z Windows命令参考 |
| Cygwin用户指南和常见问题 |
| MSYS2安装、包和环境配置 |
| WSL设置、命令和文档 |
assets/| Template | Purpose |
|---|---|
| Standalone CLI tool with argument parsing |
| Reusable function library with CALL-able labels |
| Scheduled task / automation script |
assets/| 模板 | 用途 |
|---|---|
| 带参数解析的独立CLI工具 |
| 可通过CALL调用标签的可重用函数库 |
| 计划任务/自动化脚本 |