我相信你知道%1
, %2
.bat 中的 等表示在命令行上传递给 .bat 的编号参数。
如果您将它们用作%~1
, %~2
等等。如果存在的话,所有周围的引号都会被自动删除。
考虑一下这个space-in-args.bat
供测试用:
@echo off
echo. %1
echo. (original first argument echo'd)
echo.
echo. "%1"
echo. (original first argument with additional surrounding quotes)
echo.
echo. %~1
echo. (use %%~1 instead of %%1 to remove surrounding quotes, should there be)
echo.
echo. "%~1"
echo. (we better use "%%~1" instead of "%%1" in this case:
echo. 1. ensure, that argument is quoted when used inside the batch;
echo. 2. avoid quote doubling should the user have already passed quotes.
echo.
Run it:
space-in-args.bat "a b c d e"
输出是:
"a b c d e"
(original first argument echo'd)
""a b c d e""
(original first argument with additional surrounding quotes)
a b c d e
(using %~1 instead of %1 removes surrounding quotes, should there be some)
"a b c d e"
(we better use "%~1" instead of "%1" in this case:
1. ensure, that argument is quoted when used inside the batch;
2. avoid quote doubling should the user have already passed quotes.
也可以看看for /?
(向下滚动到最后)了解更多这些转换。