使用BAT文件时出错. -R在这个时候出乎意料 [英] Error when using BAT Files. - R was unexpected at this time
问题描述
这是代码:
call n:\xxx\xxx\variables.bat %1 %2 %3 %4 %5 %6
set yourZipPassword=%xxx%
set yourFolderPath="n:\xxxx\xxxx\%xxxx%"
for /R "%yourFolderPath%" %%I in ("*.zip") do (
"C:\Program Files\7-Zip\7z.exe" x -p%yourZipPassword% -y -o"%%~dpI" "%%~fI"
)
不知道如何处理此错误-" R在当前是意外的."
Do not know what to do with this error - "R was unexpected at this time."
此bat文件停留在此行上,不再处理:
And this bat file stuck on this line and not processing anymore:
for /R "%yourFolderPath%" %%I in ("*.zip") do (
推荐答案
从...更改set
语法:
set yourFolderPath="n:\xxxx\xxxx\%xxxx%"
...到以下内容(通常):
...to the following (in general):
set "yourFolderPath=n:\xxxx\xxxx\%xxxx%"
因此引号不再是值的一部分.当您在问题中遇到问题时,for /R
循环(for /R "%yourFolderPath%"
)会收到像for /R ""n:\xxxx\xxxx\%xxxx%""
这样的根路径,因此它被过度引用".尤其是在变量值中包含空格和/或特殊字符的情况下,这是一个问题.
So the quotes are no longer part of the value. As you have it in your question, the for /R
loop (for /R "%yourFolderPath%"
) receives a root path like for /R ""n:\xxxx\xxxx\%xxxx%""
, so it is "over-quoted". This constitutes a problem particularly in case there are white-spaces and/or special characters in the variable value.
更改后的set
语法还有另一个优点:假设您使用问题的语法,有一个变量包含目录路径,另一个变量具有文件名:
The changed set
syntax has got another advantage: Imagine you have a variable holding a directory path and another one holding a file name, using the syntax from your question:
set PDIR="D:\Data"
set FILE="file.ext"
现在让我们将这些值连接起来:
Now let us concatenate these values:
echo unquoted: %PDIR%\%FILE%
echo quoted: "%PDIR%\%FILE%"
返回的行是:
unquoted: "D:\Data"\"file.ext"
quoted: ""D:\Data"\"file.ext""
您会看到一些令人不安的报价.现在,让我们使用更改后的语法:
You can see there are some disturbing quotes. Now let us use the changed syntax:
set "PDIR=D:\Data"
set "FILE=file.ext"
echo unquoted: %PDIR%\%FILE%
echo quoted: "%PDIR%\%FILE%"
所以返回的行是:
unquoted: D:\Data\file.ext
quoted: "D:\Data\file.ext"
因此不再有令人不安的报价.
So there are no more disturbing quotes.
请注意,更改后的语法仅在命令扩展启用的情况下起作用,但这是默认设置Windows命令提示符cmd
设置.
Note that the changed syntax only works with command extensions enabled, but this is the default setting for the Windows command prompt cmd
anyway.
这篇关于使用BAT文件时出错. -R在这个时候出乎意料的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!