Jump to content

need help with batch an if statement


Recommended Posts

ok so iv been searchin on google for awile now ind im no further to figuring out how to do this :(

so i want my batch file to work like this...

press Y or N

if Y

then do blabla

else

do blablabla

how do i make a batch file do that?? any help guys?? thanx :D

Link to comment
Share on other sites


And you could also try this ... the result is quite the same. *hehe*

set /p Variable="Please press Y or N"
if /I "%Variable%"=="Y" (
 echo commands to perform when "y" has been pressed
 echo you can use more than one command
 echo just put them on several lines like in batch files
) ELSE (
 echo commands to perform when "n" has been pressed
 echo hints from above apply here as well
)

That way you avoid using GoTos and you can use as many commands as you like. :) But watch out not to use parentheses within the if-clause!

Link to comment
Share on other sites

it would result in just about the same size of batch file...for my if then clause I only use 1 line, with 2 lines for the gotos...so 3 lines total. in yours, you use 3 lines for your if command...just depends on which of the 2 you will understand...I did try your method for making 2 batch files that run different runonceex and other commands, and it always gave an error...so I switched to the method I use now...I have no idea why it messed up like it did...might have been that I tried doing something youre not supposed to.

Good post though...as I said, depends on what you understand about if then statements.

Link to comment
Share on other sites

If I'm reading these if/then statements correctly, any input other than Y after "Please press Y or N" will execute the else statement. Is there any way to code this so that any input other than Y or N will be ignored?

I thought of something like this,

:YesNo
set /p Variable="Please press Y or N"
if /I "%Variable%"<>"Y" and /I "%Variable%"<>"N" goto:Yesno

but this will cause the "Please press Y or N" to be repeated over and over until the correct input is received. I want all input other than Y or N to be silently disregarded. Also, I'm not sure if this is the correct coding for "does not equal."

Link to comment
Share on other sites

@evilvoice

:D No problem! It doesn't make any difference for me which "if-method" u wanna use. :P I personally prefer clean coding and therefore I avoid gotos. They would complicate things quite much here since I use nested IFs. :}

   IF ERRORLEVEL 1 (
     echo      AKTION NICHT AUSGEFUEHRT, Ordner bereits vorhanden.
     echo 1 - Ordner "%LW_id%\%%d\%Ordner%" schon vorhanden gewesen.
   ) ELSE (
     echo      Aktion erfolgreich abgeschlossen, Ordner erstellt.
     echo 0 - Ordner "%LW_id%\%%d\%Ordner%" erstellt.
     IF "%Ordner%"=="Anwendungsdaten" (
       echo.
       echo Zusatz: Kopieren der schon vorhandenen Anwendungsdaten an den neuen Platz ...
       xcopy "%UserProfile%\%Ordner%\*.*" "%LW_id%\%%d\%Ordner%\" /q /h /s /e /i /y /k
       xcopy "%UserProfile%\Lokale~1\%Ordner%\*.*" "%LW_id%\%%d\%Ordner%\" /q /h /s /e /i /y /k
       echo.
     )
   )

Hhm, as you see it should work out well even with somehow more "complicated" stuff. Don't know what causes the trouble on your PC. :unsure:

Link to comment
Share on other sites

If I'm reading these if/then statements correctly, any input other than Y after "Please press Y or N" will execute the else statement. Is there any way to code this so that any input other than Y or N will be ignored?

Try using the "NOT" parameter.

IF NOT "%V%"="N" IF NOT "%V%"="Y" command

or ...

IF NOT "%V%"="N" IF NOT "%V%"="Y" (
 commands
)

or ...

set /p Variable="Please press Y or N"
if /I "%Variable%"=="Y" (
echo commands to perform when "y" has been pressed
echo you can use more than one command
echo just put them on several lines like in batch files
) ELSE IF "%Variable%"=="N "(
echo commands to perform when "n" has been pressed
echo hints from above apply here as well
) ELSE (
 echo commands to perform when neither "n" nor "y" has been pressed
)

Just adapt this to your own needs, of course. :)

P.S.: In the first example the second command is just another if command. This way you can nest them as well. In the last one there are just new if else branches - according to one's own need.

Link to comment
Share on other sites

this is a lot easier than the above codes

doesnt matter if you use capitals or not both are in there

just add your 'yes' code to where 'echo yes' is and your no code to where 'echo no' is :D

@echo off
:start
cls
set /p userinp=choose a yes/no(Y-N):
set userinp=%userinp:~0,1%
if "%userinp%"=="Y" goto yes
if "%userinp%"=="y" goto yes
if "%userinp%"=="N" goto no
if "%userinp%"=="n" goto no

echo invalid choice
goto start

:yes
echo YES
goto end

:no
echo NO
goto end

:end
pause>nul

Link to comment
Share on other sites

this is a lot easier than the above codes

doesnt matter if you use capitals or not both are in there

just add your 'yes' code to where 'echo yes' is and your no code to where 'echo no' is  :D

@echo off
:start
cls
set /p userinp=choose a yes/no(Y-N):
set userinp=%userinp:~0,1%
if "%userinp%"=="Y" goto yes
if "%userinp%"=="y" goto yes
if "%userinp%"=="N" goto no
if "%userinp%"=="n" goto no

echo invalid choice
goto start

:yes
echo YES
goto end

:no
echo NO
goto end

:end
pause>nul

Doesn't anyone know you can use a /I switch and cut your "IF" statements in half by not having to use one for "Y" and "y"? :)

Link to comment
Share on other sites

Here Mine It Has Yes No And A Quit Part

For Only One Letter Input

ECHO OFF
CLS
COLOR F2
MODE CON: COLS=55 LINES=7
TITLE YES NO QUIT
:MAIN
ECHO. && ECHO   Y FOR YES ® && ECHO   N FOR NO ® && ECHO   Q FOR QUIT ®
SET Choice=
SET /P Choice=Type the letter and press Enter:
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
IF /I '%Choice%'=='Y' GOTO YES
IF /I '%Choice%'=='N' GOTO No
IF /I '%Choice%'=='Q' GOTO QUIT
ECHO "%Choice%" is not valid. Please try again.
GOTO MAIN
:YES
cls && ECHO. && ECHO Press Key to Return && SET /P = YOU SAID YES
GOTO MAIN
:NO
cls && ECHO. && ECHO Press Key to Return && SET /P = YOU SAID NO
GOTO MAIN
:QUIT
cls && ECHO. && SET /P = YOU SAID QUIT
EXIT

For Multiple Letter Input

ECHO OFF
CLS
COLOR F2
MODE CON: COLS=55 LINES=7
TITLE YES NO QUIT
:MAIN
cls
ECHO. && ECHO   Yes FOR YES ® && ECHO   No FOR NO ® && ECHO   Quit FOR QUIT ®
SET Choice=
SET /P Choice=Type the letter and press Enter:
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,4%
IF /I '%Choice%'=='Yes' GOTO YES
IF /I '%Choice%'=='No' GOTO No
IF /I '%Choice%'=='Quit' GOTO QUIT
ECHO "%Choice%" is not valid. Please try again.
GOTO MAIN
:YES
cls && ECHO. && ECHO Press Key to Return && SET /P = YOU SAID YES
GOTO MAIN
:NO
cls && ECHO. && ECHO Press Key to Return && SET /P = YOU SAID NO
GOTO MAIN
:QUIT
cls && ECHO. && SET /P = YOU SAID QUIT
EXIT

This Cmd Contains Both Of The Above Code Plus A Menu To Control It

I Edit this now so the menu are straight now

Edited by gunsmokingman
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...