在批处理文件中获取用户输入,验证和更新

时间:2015-02-13 10:40:16

标签: batch-file dos batch-processing

我在获取用户输入并验证相同内容并将其传递给其他命令时遇到了一些问题。请找到以下代码

:: user input for renaming the war file 
@set /P filename=Enter the war file name for deployment
:: Validating the user input 
IF "%filename%"=="" GOTO Error
:: rename the war file before deploying
@rename "hsa.war" "%filename%.war"
@pause
:Error
ECHO You have not entered a file name Bye bye!!
:End

在上面的代码中,我请求用户指定用于修改war文件的名称,并在后续步骤中验证该文件。当我执行上面的代码,即使我输入名称,执行转到代码的错误部分并打印消息。你能否提供一些关于如何修复它的输入,并避免弹出错误信息。提前致谢

1 个答案:

答案 0 :(得分:0)

您应该跳过:Error标签:

:: user input for renaming the war file 
@set /P filename=Enter the war file name for deployment
:: Validating the user input 
IF "%filename%"=="" GOTO Error

:: check whether file exist (if so, rename will fail) 
if exist "%filename%.war" goto :label_file_exist

:: rename the war file before deploying
@rename "hsa.war" "%filename%.war"
@pause

:: skip :Error label
goto :anyLabelOfYourChoice
:Error
ECHO You have not entered a file name Bye bye!!
:End

添加if exist "%filename%.war"检查以避免出现下一条错误消息:A duplicate file name exists, or the file cannot be found.

考虑检查用户的输入是否构成valid file name