Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如果输入超过2个,则出错_Windows_Batch File_Cmd - Fatal编程技术网

Windows 如果输入超过2个,则出错

Windows 如果输入超过2个,则出错,windows,batch-file,cmd,Windows,Batch File,Cmd,如果用户给出了2个参数中的1个,或多于2个,则应显示一条错误消息,说明参数丢失并转到int。但取而代之的是前两个或仅一个参数。如何解决此问题?这只是一个基本解决方案 @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 under

如果用户给出了2个参数中的1个,或多于2个,则应显示一条错误消息,说明参数丢失并转到int。但取而代之的是前两个或仅一个参数。如何解决此问题?

这只是一个基本解决方案

@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
如果您至少有两个参数,但没有三个,则开始工作,否则显示错误并结束

还要注意的是,在代码中

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

这是错误的
name
只是一个字符串和变量名。您需要与环境变量关联的值,也就是说,您应该使用批处理方式调用集合变量
%name%
。当批处理解析器到达该行时,它将看到
%name%
引用,并将其扩展到与
name
变量关联的值。(如果是
用户名
),也应进行此更改)

我可以建议您不要使用“读取操作”短语,而是使用“变量扩展”吗?我知道读取操作是
set/P
命令所做的…@Aacini,我明白你的意思,也许
retrieve
read
的一个不太含糊的术语,但对我来说
变量扩展
是解析器处理值检索编码请求的方式。我选择将其更改为调用,因为这是Microsoft文档中使用的术语。@Aacini,并参考问题本身,是的,请提供您认为有帮助的一切建议。我只有一个脑袋,通常这是不够的。
if name==[] if username==[]