2015-05-13 30 views
0

每當我使用它,它說goto是意想不到的。爲什麼?我已經瀏覽了很多這裏回答的問題,似乎我似乎無法做到。另外,無論我輸入什麼內容,無論是否正確,似乎都是這樣:不正確。請幫助。批處理文件一直說goto是意想不到的

@echo off 
:password 
set /p pw=Type password: 
if %pw%=="fdDg*3&[email protected]%jdgf" goto begin 
if not %pw%=="fdDg*3&[email protected]%jdgf" goto incorrect 
:incorrect 
echo Access denied. 
goto password 
:begin 
echo Welcome! 
pause 
exit 
+0

答案試試這個:'如果 「%PW%」 == 「fdDg * 3 d @%jdgf」' –

+0

如果你用'echo on'運行你的腳本,你會注意到它不喜歡你的密碼中的百分號。不需要'如果不是....'行。 – Stephan

+0

我已經刪除了if語句並更改了百分號。但是,它仍將任何輸入顯示爲不正確。我只需要弄清楚如何做到這一點,如果輸入一個值,它就可以工作。如果輸入了其他值,則轉到:不正確。 –

回答

0

你必須使用

@echo off 
:password 
set /p "pw=Type password: " 
if "%pw%"=="fdDg*3&[email protected]%%jdgf" goto begin 
:incorrect 
echo Access denied. 
goto password 
:begin 
echo Welcome! 
pause 
exit 

對於由周圍需要雙引號Kayasax寫道預定義的密碼輸入的密碼的區分大小寫的比較。

百分號具有特殊含義,因爲您可以看到它,因爲您使用%pw%上的特殊含義。因此,密碼字符串中的百分號必須與另一個百分號一起轉義,如提到的Stephan。並且if not線真的不需要。

然而,密碼驗證碼仍需要更多的代碼無效輸入檢查,看到Why can a user set a new password without the need to enter old password correct with my batch code?