2015-09-03 43 views
0

我創建一個批處理文件,如下批處理文件鍵入退出後自動「

@echo off 
echo Type Below Requirements: 
echo. 
:username 
set /p usr= Type Username: 
if %usr%==[%1]==[] goto username 
echo Your username is: %usr% 
pause 

這是工作完美,當我輸入任何文字,但如果我鍵入」批量自動退出。

+0

你想要什麼的'如果USR%==%[%1] == []'線做?? – aschipfl

+0

如果有人沒有輸入任何東西,它應該重複這個問題,爲此,如果我錯了,如果%usr%== [%1] == []糾正了我的錯誤。謝謝 –

+0

謝謝你,如果你可以請指導我通過這個問題 –

回答

0

首先,比較表達式語法if %usr%==[%1]==[]是錯誤的,它應該改爲if [%usr%]==[]

其次,要正確處理雙引號,您需要啓用delayed expansion

下面應該工作:

@echo off 
setlocal EnableDelayedExpansion 
echo Type Below Requirements: 
echo. 
:username 
set usr= 
set /p usr= Type Username: 
if [!usr!]==[] goto username 
echo Your username is: !usr! 
pause 
+0

感謝alottt,它爲我工作..很大 –

+0

是否可以通過鍵入空格和符號進行限制,例如:「測試用戶名」和「測試不應該允許@username「... –

+0

在上面的批處理文件中,是否可以提供警報,例如:您輸入用戶名無效,如果輸入」空格或符號「,並且密碼應該與密碼Policy匹配... –

2

這裏是另一種方式:

@echo off 
echo Type Below Requirements: 
echo. 
:username 
set "usr=" 
set /p "usr= Type Username: " 
if not defined usr goto :username 
echo Your username is: %usr% 
pause