@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个,那么它应该显示缺少参数和goto int的错误消息。但它需要前两个或只需一个。我该如何解决这个问题?
答案 0 :(得分:0)
只是一个基本的解决方案
....
if not "%~2"=="" if "%~3"=="" goto :doSomething
echo Error - Bad arguments
goto :eof
:doSomething
....
如果您至少有两个参数但不是三个,则开始工作,否则显示错误并结束。
另请注意,在您的代码中,行
if name==[] if username==[]
错了。 name
只是一个字符串和变量的名称。您需要与环境变量关联的值,也就是说,您应该使用%name%
,即调用set变量的批处理方式。当批处理解析器到达该行时,它将看到%name%
引用,并将其扩展为与name
变量关联的值。 (这种变化也应该在username
)