2

I am writing bat file having if statement goto command not working in first statement

:purge_operation
set /p ABC_DEF=Are you want to Purge ABC or DEF Data ? (ABC/DEF):

:start
if %ABC_DEF%==ABC goto ABC
if %ABC_DEF%==DEF goto DEF
:end

:ABC
echo purge ABC data !

:DEF
echo purge DEF data !

pause >nul

output:------------

Actual Result---------

Are you sure you want to Purge DATA ? (Y/N):Y
Are you want to Purge ABC or DEF Data ? (ABC/DEF):ABC
purge ABC data !
purge DEF data !

Expected Results----

Are you sure you want to Purge DATA ? (Y/N):Y
Are you want to Purge ABC or DEF Data ? (ABC/DEF):ABC
purge ABC data !
Rajesh Nayak
  • 63
  • 1
  • 5
  • 1
    Labels are just labels you can jump to and don't hinder program flow from running through. So change your expectation or insert additional `goto :eof` where eof is an unvisible lable denoting file end. –  Sep 08 '19 at 16:00
  • How Can I use goto :eof in my statament – Rajesh Nayak Sep 08 '19 at 16:08

2 Answers2

6

Goto changes the execution point to a label. A label on it's own won't change the execution behavior. In other words, after each logical unit in a batch file, you'll need to change control flow, otherwise it will just continue with the next statement.

For instance, you could modify your example batch file to only perform one of the selected outcomes like this:

:purge_operation
set /p ABC_DEF=Are you want to Purge ABC or DEF Data ? (ABC/DEF):

:start
if %ABC_DEF%==ABC goto ABC
if %ABC_DEF%==DEF goto DEF
echo Unknown entry
goto :EOF

:ABC
echo purge ABC data !
goto :done

:DEF
echo purge DEF data !
goto :done

:done
pause >nul

The second goto :done is strictly unnecessary, but it's a good habit to get into, in case you add more branch possibilities in the future.

Anon Coward
  • 9,784
  • 3
  • 26
  • 37
  • how to print message in Unknown entry in bat file in if condition can you please help.And also given chance type again after unknown input message @Anon Coward – Rajesh Nayak Sep 08 '19 at 16:23
1

Look on my answer on How to stop Windows command interpreter from quitting batch file execution on an incorrect user input? After reading what are common problems with prompting user with set /P and how much effort must be made to make it fail safe, read how much easier a choice between defined options can be on using command choice designed for such tasks.

@echo off
echo Select the data to purge:
echo/
echo   1 ... ABC
echo   2 ... DEF
echo/
%SystemRoot%\System32\choice.exe /C 1ABC2DEF /N /M "Do you want to purge ABC or DEF data?"
echo/
if errorlevel 4 goto DEF

echo Purge ABC data!
goto Done

:DEF
echo Purge DEF data!

:Done
echo/
pause >nul

The batch file user can press

  • 1, A, B, C, a, b, c to choose data ABC or
  • 2, D, E, F, d, e, f to choose data DEF or
  • Ctrl+C to exit batch file processing after an additional confirmation by the user.

Every other key press results in a short error noise if audio output is not muted and choice waits on one of the keys defined after option /C case-insensitive.

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • choice /?
  • echo /?
  • goto /?
  • if /?
  • pause /?

See also DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/

Mofi
  • 46,139
  • 17
  • 80
  • 143