批处理文件":此时出乎意料"

时间:2017-08-16 17:52:21

标签: windows batch-file command-line scripting batch-processing

以下代码行是较大脚本的一部分。虽然这是在运行时产生问题的特定部分。在初始提示和用户输入之后,脚本出错,提供错误: unexpected at this time。在查看我的代码和一些在线文档后,看起来我所做的一切都应该是正确的。虽然我确信我错过了导致这种情况发生的轻微事情。

::Changes Date/Time
echo The current date and time is %date% %time%
SET /P TIMECHANGE=Would you like to change the time (y:Yes):

if /i "%TIMECHANGE%" == "y" (
    SET /P POSTIME=Please enter new time (HH:MM:SS):
    time "%POSTIME%"
)

SET /P DATECHANGE=Would you like to change the date (y:Yes):

if /i "%DATECHANGE%" == "y" (
    SET /P POSDATE=Please enter new date (MM-DD-YYYY):
    date "%POSDATE%"
)

tzutil /s "Eastern Standard Time"

编辑:
通过更多的讨论,我发现问题就出现了:

if /i "%TIMECHANGE%" == "y" (

2 个答案:

答案 0 :(得分:1)

请参阅SO中的ar = [ [9, 8, 7], [55, 30, 41], [210, 111, 74], [121, 196, 212], [144, 62, 57], [101, 66, 100], [189, 174, 192], [164, 116, 133] ] function toPaddedHexVal(val) { var hexVal = val.toString(16); return hexVal.length < 2 ? "0" + hexVal : hexVal; } res = ar.map(colorArray => "#" + colorArray.map(toPaddedHexVal).join("")) console.log(res);个条目(有很多个),以解释为什么delayedexpansion变量,并在代码块中使用新值(括号内的顺序排列)不会出现这种情况。似乎改变了值。

此处的简单解决方案是将settime - 设置行移到date语句之外,例如

if

答案 1 :(得分:0)

问题是由带有SET /P文字的(...):行引起的。使用引号如下,它将起作用SET /P "VAR=TEXT:"

SET /P "TIMECHANGE=Would you like to change the time (y:Yes):"

SET /P "POSTIME=Please enter new time (HH:MM:SS):"

SET /P "DATECHANGE=Would you like to change the date (y:Yes):"

SET /P "POSDATE=Please enter new date (MM-DD-YYYY):"