c - 返回错误代码时批处理文件未打开正确的菜单选项

标签 c batch-file if-statement input

我编写了一个批处理文件,它从用户那里获取输入以打开文件,然后显示一个菜单。然后,使用我编写的 .exe C 文件提示用户输入,并返回用户输入的数字,如果输入不是数字,则返回“-1”。无论我使用哪个选项,程序总是打开记事本,无论菜单选项如何。任何帮助都会很棒。我已经包含了批处理文件和我的 c 输入文件的代码。

我检查了我的 C 程序,它似乎返回了它应该返回的内容,因此可能只是批处理方面的格式问题。我列出来仅供引用。

批处理文件:

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 输入程序代码:

#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;

}

最佳答案

@ECHO OFF
SETLOCAL

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

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

SET /p "FileToProcess=Please enter file to process:" || exit /b 0

REM Strip double quotes
SET "FileToProcess=%FileToProcess:"=%"

REM 3. Checking for file
REM ------------------------------------------------------
IF NOT EXIST "%FileToProcess%" goto :FileNotExist
cls

:MENU
ECHO 1. Open in Notepad
ECHO 2. Open in Word
ECHO 3. Open in Notepad ++
ECHO 4. Print

myChoice.exe

IF %ERRORLEVEL% equ 1 (
    cd /d "C:\Windows"
    notepad.exe "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 2 (
    cd /d "C:\Program Files (x86)\Microsoft Office\root\Office16"
    WINWORD.EXE "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 3 (
    cd /d "C:\Program Files\Notepad++"
    notepad++.exe "%FileToProcess%"
) ELSE IF %ERRORLEVEL% equ 4 (
    cd /d "C:\Windows"
    notepad.exe /P "%FileToProcess%"
) ELSE (
    ECHO Sorry your input was not accepted!
    pause
    GOTO MENU
)
exit /b 0

REM 4. Display error if no file found
REM -----------------------------------------------------
:FileNotExist
>&2 ECHO File does not exist!
exit /b 1
  • 路径双引号。
  • IF EXIST "%FileToProcess%"( 更改为 如果不存在“%FileToProcess%”转到:FileNotExist 以避免需要成为一个大的括号 block 。
  • 使用else if来避免使用大量goto :end
  • if errorlevel 更改为 if %errorlevel% 实际情况 number 而不是 number 及更高。
  • 删除了不需要的 :end 标签。

关于c - 返回错误代码时批处理文件未打开正确的菜单选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55438421/

相关文章:

c - 结构设置和初始化变量

sql - 如何使用 SQLExecDirect?

shell - Pentaho执行进程似乎没有启动脚本文件

R 使用 ifelse 更改因子值

java - 将 "condition ? expr1 : expr2"的链转换为 "if"的链

c - 运行时检查失败 #2 - 变量 'tempMatrix' 周围的堆栈已损坏

c - 如何杀死包括自身在内的所有进程以及如何正确执行所有进程

batch-file - 显示 Windows 命令提示符输出并将其重定向到文件

windows - 批量连接字符串和变量

javascript - 比较两个对象数组并过滤结果