-1

I'm coding a program in Batch and recently I stumbled upon a new issue. The program is supposed to ask me for an answer and it's programmed to go to a certain label when I type in a specific answer (in this case, if the answer is "file", go to the label "file"). The problem is, when I did this, the program instantly closed itself every time. I tried running the program in the Visual Studio Code terminal, it throws the error "The system cannot find the batch label specified - file".

The label file is indeed located within the program and I don't see any issues that would prevent the batch file from transferring into that position. It worked perfectly earlier, and since that, I don't recall changing anything within this portion of the code.

I tried carefully checking for typos once more, e.g. accidentally deleting a character somewhere, but everything seems fine and I can't find out what's wrong.

The code goes like this:

set /p choice="Enter your choice: "

if /i "%choice%" == "exit" exit
if /i "%choice%" == "file" goto file
echo.
echo Invalid choice.
pause

...

:file
  cls

...

Any ideas on what should I do to fix this problem? I'm kind of a beginner in Batch and it's entirely possible I just made a stupid mistake somewhere. Any help is appreciated!

Question / choice system code screenshot

File label code screenshot

VSCode terminal output screenshot

Edit

The file I'm taking this code from is BIG. I won't be able to show the full code using the "type" command or any other command in general, however, I'm able to copy any specific part you want me to copy.

  • a) for clarification; do the other labels work ok? b) did you run with `echo on` to see where it fails? – Stephan Aug 10 '23 at 15:05
  • Your code snipped works well. There must be more to it (maybe the snipped is within a code block?) – Stephan Aug 10 '23 at 15:12
  • 1
    Your code, as posted works fine. `Choice` is a keyword in batch, used for menus. Please use the top bar to search for `[batch] menu` for many examples. Please cut-and-paste the code from what `type yourbatchfilename` displays from the prompt, obfuscating and redacting as necessary. Note that using a word-processor (even `Notepad`) to edit batch files can cause such peculiar errors. There are many `text editor` programs available which do the job. I use `editplus`. Ensure that the code is saved in `ANSI`, not as Unicode. – Magoo Aug 10 '23 at 15:14
  • 1
    It also looks like you could have just simply used ```GoTo %choice% 2>NUL || GoTo LabelAboveYourSetPLine```, instead of a whole load of `If` statements. – Compo Aug 10 '23 at 15:38
  • @Stephan All of the other labels work fine. It seems that "file" is the only one causing problems. – marshmellxw Aug 10 '23 at 15:54
  • Then I can't see a reason for it to not work. Delete the `if... goto file` line completely and type it anew. – Stephan Aug 10 '23 at 16:01
  • Additionally, using `echo on` basically shows the same thing. `if /I "file" == "file" goto file` , then the error pops up. – marshmellxw Aug 10 '23 at 16:02
  • @Stephan Sadly, rewriting it didn't help, it still shows the same error. – marshmellxw Aug 10 '23 at 16:04
  • 1
    I'd also recommend reading https://stackoverflow.com/help/minimal-reproducible-example so that you can post code that actually represents your problem and we can help you better. – SomethingDark Aug 10 '23 at 18:30

0 Answers0