You are not logged in.

#1 02 Nov 2015 19:09

joskuzera
Member
Registered: 05 Oct 2015
Posts: 5

Help with batch files!

Hi everyone, I wrote a very simple batch file, being that I'm new with if statements within batch i wanted to know how to correct this. I want my if statement to check if the user hasn't input anything and then provide a usage output. what did i do wrong?

@echo off
if "%1" == "" (
   echo Usage: %0 arg1
   exit /b
)
set /p option=Enter Username:
echo Your Username is %option%
echo Your secret Number is %random%
pause
cls
echo Thank you for using Joes Program %option% Enjoy your day!
pause

Offline

#2 03 Nov 2015 02:15

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 362

Re: Help with batch files!

You should clear the variable because if you invoke it multiple times from the same CMD session it will contain the value from a previous run. Also check it after user hits Enter.

@echo off
if "%1" == "" (
   echo Usage: %0 arg1
   exit /b
)

set "option="
:Input
set /p option=Enter Username:
if "%option%"=="" goto :Input

echo Your Username is %option%
echo Your secret Number is %random%
pause
cls
echo Thank you for using Joes Program %option% Enjoy your day!
pause

Windows Shell Scripting and InstallShield

Offline

Board footer

Powered by