windows * .bat文件用户输入和验证

时间:2014-05-04 17:47:56

标签: windows batch-file cmd

我编写了可以检查用户输入的基本脚本。如果用户未填写脚本必须再次询问姓名输入。但事情出了问题,我们错过了吗?

   @echo OFF
    title Enter your name
    color 0a

    set /p Name="Your name: " %=%

    :start

    if "%Name%"=="" (goto try)

    :try
    cls 
    color 0a
    echo You must enter your name!
    color 0c
    set /p Name="Your name: " %=%
    if "%Name%"!="" (goto init) else (goto try)

    :init
    color 0a
    cls
    Echo %Name% is saved...
    reg add "HKEY_CURRENT_USER\Software\example" /v "YourName" /t REG_SZ /d "%Name%"  /f
    Pause&Exit

2 个答案:

答案 0 :(得分:4)

if "%Name%"!="" ...

不是有效的语法。而是使用:

if not "%Name%"=="" ...

if "%Name%" neq "" ...

答案 1 :(得分:0)

我改变了代码,现在它可以了!

@echo OFF
title Name pl0x
color 0a
:start
set /p Name="Your name: " %=%


if "%Name%"=="" (

cls 
color 0a
echo You must enter your name!
color 0c
goto :start
) else (
color 0a
cls
Echo %Name% saved...
reg add "HKEY_CURRENT_USER\example" /v "YourName" /t REG_SZ /d "%Name%"  /f
Pause&Exit
)