2017-04-06 46 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,并提及问题本身,是的,请提出你认为可能有所帮助的一切。我只有一个头,通常是不够的。 –

相关问题