我的批处理代码有一些问题。无法找出为什么它会因为括号而抛出错误
Having some problems with my batch code. Can't find out why it's throwing an error because of a parentheses
自己拿代码检查一下,每次它都能问我开始时间,然后是上午还是下午,但只要我点击进入 am/pm,它立即关闭,我在关闭时截图了,它说( was unexpected here
。如果有人可以帮忙,那就太好了。 (此外,如果有人有修剪和优化技巧,那也很棒。谢谢)
@echo off
:questions
set am=am
set pm=pm
set y=y
set n=n
:1
cls
set /p start=[Shutdown time (The hour of shutdown, do not add minutes):]
set /p 1ampm=[am/pm:]
if %1ampm% EQU %am% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :2
) else (
if !1ampm! EQU %pm% (
PING 1.1.1.1 -n 1 -w 100 >NUL
set realstart=%start%+12
goto :2
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :1
)
)
:2
cls
set /p end=[Enter the time you want the computer to be available again for use(The hour of shutdown, do not add minutes):]
set /p 2ampm=[am/pm:]
if %2ampm% EQU %am% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :loading
) else (
if !2ampm! EQU %pm% (
PING 1.1.1.1 -n 1 -w 100 >NUL
set realend=%end%+12
goto :loading
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :2
)
)
:loading
cls
echo Loading....
PING 1.1.1.1 -n 1 -w 2000 >NUL
cls
set /p yesno=[The time you selected the computer to remain off is from %start% - %end% , is this correct, y/n:]
cls
if %yesno% EQU %y% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :Begining
) else (
if %yesno% EQU %n% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :questions
) else (
goto :questions
)
)
:Begining
set mytime=%time:~0,2%
:Start
if %mytime% GEQ %realstart% (
cls
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
if %mytime% LEQ %realend% (
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
cls
echo This program is Opperating correctly
PING 1.1.1.1 -n 1 -w 600000 >NUL
goto :Start
)
)
你的问题出在这几行:
set /p 1ampm=[am/pm:]
和
if %1ampm% EQU %am% (
问题是变量名。 Batch 在调用批处理文件时可以获取参数,例如 %1
或 %2
。这会导致批处理将其视为 %1 ampm % EQU % am%
,但它没有参数,因此 %1 为空。之后它看到第一个百分号,看到它后面没有数字,并将其视为名为 % EQU %
的变量的开头,该变量也是空的。然后,它删除最后一个百分号,只留下 ampmam (
作为行。这就是引发错误的原因。
因此,要解决错误,只需使用数字以外的其他内容作为变量的开头。
自己拿代码检查一下,每次它都能问我开始时间,然后是上午还是下午,但只要我点击进入 am/pm,它立即关闭,我在关闭时截图了,它说( was unexpected here
。如果有人可以帮忙,那就太好了。 (此外,如果有人有修剪和优化技巧,那也很棒。谢谢)
@echo off
:questions
set am=am
set pm=pm
set y=y
set n=n
:1
cls
set /p start=[Shutdown time (The hour of shutdown, do not add minutes):]
set /p 1ampm=[am/pm:]
if %1ampm% EQU %am% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :2
) else (
if !1ampm! EQU %pm% (
PING 1.1.1.1 -n 1 -w 100 >NUL
set realstart=%start%+12
goto :2
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :1
)
)
:2
cls
set /p end=[Enter the time you want the computer to be available again for use(The hour of shutdown, do not add minutes):]
set /p 2ampm=[am/pm:]
if %2ampm% EQU %am% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :loading
) else (
if !2ampm! EQU %pm% (
PING 1.1.1.1 -n 1 -w 100 >NUL
set realend=%end%+12
goto :loading
) else (
cls
echo you did not enter whether or not the start time is am or pm
goto :2
)
)
:loading
cls
echo Loading....
PING 1.1.1.1 -n 1 -w 2000 >NUL
cls
set /p yesno=[The time you selected the computer to remain off is from %start% - %end% , is this correct, y/n:]
cls
if %yesno% EQU %y% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :Begining
) else (
if %yesno% EQU %n% (
PING 1.1.1.1 -n 1 -w 100 >NUL
goto :questions
) else (
goto :questions
)
)
:Begining
set mytime=%time:~0,2%
:Start
if %mytime% GEQ %realstart% (
cls
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
if %mytime% LEQ %realend% (
echo time has expired, time to go to bed.
shutdown -s -f -t 60 -c "Your computer is about to be shut down in 1 minute"
) else (
cls
echo This program is Opperating correctly
PING 1.1.1.1 -n 1 -w 600000 >NUL
goto :Start
)
)
你的问题出在这几行:
set /p 1ampm=[am/pm:]
和
if %1ampm% EQU %am% (
问题是变量名。 Batch 在调用批处理文件时可以获取参数,例如 %1
或 %2
。这会导致批处理将其视为 %1 ampm % EQU % am%
,但它没有参数,因此 %1 为空。之后它看到第一个百分号,看到它后面没有数字,并将其视为名为 % EQU %
的变量的开头,该变量也是空的。然后,它删除最后一个百分号,只留下 ampmam (
作为行。这就是引发错误的原因。
因此,要解决错误,只需使用数字以外的其他内容作为变量的开头。