2017-04-06 81 views
0
@echo off 
if not exist "c:\user_records" md c:\user_records 
if [%1]==[] goto ask 
set name=%~1 
set username=%~2 
if name==[] if username==[] (
@echo Error. I don't understand the perameters. Now switching to 
@echo Interactive mode 
goto int 
) 
goto save 
:ask 
@echo This program allows the user to create a username for them to use. 
@echo When asked, please enter your name and chosen username. The 
@echo program will create a .txt file with the chosen username as the title. 
:int 
set /p name="What is your name?" 
set /p username="What is your username?" 
:save 
if exist c:\user_records\%username%.txt (
@echo This username already exists! 
set /p username="Please enter another username:" 
) 
@echo %username%.txt was created 
@echo %name% >>c:\user_records\%username%.txt 
@echo %username% >>c:\user_records\%username%.txt 
@echo %date% %time% >>c:\user_records\%username%.txt 

如果用戶給出2個參數中的1個或多於2個,那麼它應該顯示一個錯誤消息,指出參數丟失並轉到int。但相反,它需要前兩個或只有一個。我該如何解決?錯誤如果超過2個輸入

回答

0

只是一個基本的解決方案

.... 
if not "%~2"=="" if "%~3"=="" goto :doSomething 
echo Error - Bad arguments 
goto :eof 
:doSomething 
.... 

如果您至少有兩個論點,但不是三個,然後開始工作,否則顯示錯誤和結束。

另外請注意,在你的代碼中,行

if name==[] if username==[] 

是錯誤的。 name只是一個字符串和一個變量的名字。您需要與環境變量關聯的值,即應使用%name%(調用set變量的批處理方式)。當批解析器到達該行時,它將看到%name%參考,並將其展開爲與name變量關聯的值。

+0

我建議你不要用「讀操作」這句話,但「變量擴展」,而不是(這種改變也應該在的username的情況下進行)?恕我直言,一個_read operation_是什麼'設置/ P'命令不... – Aacini

+0

@Aacini,我明白你的意思,也許'retrieve'是一個更加明確的術語,'read',但對我來說'變量expansion'是這樣的解析器處理已編碼的值檢索請求。我選擇將它改爲*'calling' *,因爲它是Microsoft文檔中使用的術語。 –

+0

@Aacini,並提及問題本身,是的,請提出你認爲可能有所幫助的一切。我只有一個頭,通常是不夠的。 –