samedi 30 mars 2019

Batch file not opening correct menu option when errorcode is returned

I have written a batch file that gets input from the user to open a file, then displays a menu. Input is then prompted from the user using a .exe C file I wrote and returns the number the user entered, or "-1" if input was not a number. No matter which option I use, the program always open notepad regardless of the menu option. Any help would be great. Ive included code for the batch file and my c input file.

I have checked my C program and it seems to be returning what it is supposed to so it might just be an issue with formatting on the batch side. I included just for reference.

BATCH FILE:

REM 1. Clear the screen
REM ------------------------------------------------------
cls


REM 2. Getting user input
REM ------------------------------------------------------

SET /p "FileToProcess=Please enter file(s) to process:" 

REM 3. Checking for file
REM ------------------------------------------------------
IF EXIST "%FileToProcess%" (
    cls
    :MENU
    ECHO 1. Open in Notepad
    ECHO 2. Open in Word
    ECHO 3. Open in Notepad ++
    ECHO 4. Print

    myChoice.exe
    )

    IF ERRORLEVEL 1 (
    cd C:\Windows
    notepad.exe %FileToProcess%
    GOTO END
    )

    IF ERRORLEVEL 2 (
    cd C:\Program Files (x86)\Microsoft Office\root\Office16
    WINWORD.EXE %FileToProcess%
    GOTO END
    )

    IF ERRORLEVEL 3 (
    cd C:\Program Files\Notepad++
    notepad++.exe %FileToProcess%
    GOTO END
    )

    IF ERRORLEVEL 4 (
    cd C:\Windows
    notepad.exe /P %FileToProcess%
    GOTO END
    )

    IF ERRORLEVEL -1 (

    ECHO Sorry your input was not accepted!
    pause
    GOTO MENU
    )
REM 4. Display error if no file found
REM -----------------------------------------------------
) ELSE (

    ECHO File does not exist!
    GOTO END

)


:END

C INPUT PROGRAM CODE:

#include <stdio.h>
#include <string.h>

#pragma warning(disable:4996)
// main
int main(void)
{
    // variables
    int num;
    char userInput[10] = "";

    // requesting input
    printf("Please enter a menu option: ");
    fgets(userInput, 81, stdin);

    // checks input
    if (sscanf(userInput, "%d", &num) == 1)
    {
        return num;
    }
    else
    {
        return -1;
    }

    return 0;

}

Aucun commentaire:

Enregistrer un commentaire