【发布时间】:2015-11-25 06:57:13
【问题描述】:
我希望程序处理任意数量的命令行参数,但我不确定如何实现一个 for 循环来执行多个 shift 命令。此外,程序结束时不应关闭命令提示符窗口。
这是我目前所拥有的:
@echo off
if "%1"=="" goto skip
if "%1"=="-ti" time /t
if "%1"=="-da" date /t
if "%1"=="-c" cls
if "%1"=="-ip" ipconfig
if "%1"=="-d" dir
if "%1"=="-p" path
if "%1"=="-v" ver
if "%1"=="-t" type
if "%1"=="-m" md
exit /b
:skip
echo No arguments!
我想让以下命令行参数正常运行:
C:\>batchfile –d –ti –da –ip –v
C:\>batchfile
C:\>batchfile –d –m CSC3323 –d
C:\>batchfile –d –t batchfile.bat –m CSC3323 –d
【问题讨论】:
-
这在 Cmd.exe shell 脚本(批处理文件)代码中很难做到。我强烈推荐 PowerShell,它内置了精美的参数解析。本文将为您介绍 - Windows IT Pro - PowerShell: Why You'll Never Go Back to Cmd.exe Batch Files。
标签: batch-file for-loop command-line-arguments batch-processing