0

So I've got a cmds.bat file, with this "code". It's just a simple tool asking me to execute my most frequent command line commands:

@echo off
:Ask
echo What command should I excute?
echo 1) ncu -g
echo 2) gem outdated
echo 3) gem update
echo 4) pip list --outdated
echo 5) pip upgrade all packages!
set INPUT=
set /P INPUT=Type input: %=%
If "%INPUT%"=="1" goto cmd1
If "%INPUT%"=="2" goto cmd2
If "%INPUT%"=="3" goto cmd3
If "%INPUT%"=="4" goto cmd4
If "%INPUT%"=="5" goto cmd5
:cmd1
ncu -g
:end
:cmd2
gem outdated
:end
:cmd3
gem update
:end
:cmd4
pip list --outdated
:end
:cmd5
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
:end
:quit

Whenever I press 4 it executes the 4th and 5th command instead of just 4th. What am I doing wrong?

Grumpy ol' Bear
  • 725
  • 3
  • 15
  • 27
  • 3
    `:end` doesn't do anything, you should replace it with `goto :quit` or `goto :eof` – Dennis van Gils Jun 19 '16 at 18:59
  • 1
    You should also consider the case the user inputs something else than `1` to `5`, by placing `goto :quit` after the `if` queries, for instance... – aschipfl Jun 19 '16 at 19:27
  • 2
    As each option executes just a single command, I suggest to use `If "%INPUT%"=="1" ncu.exe -g & goto :EOF` and `If "%INPUT%"=="2" gem.exe outdated & goto :EOF` and so on. This makes the batch code more compact. For details on `&` operand see the answer on [Single line with multiple commands using Windows batch file](http://stackoverflow.com/a/25344009/3074564). – Mofi Jun 19 '16 at 19:39
  • @DennisvanGils transform your comment to an answer to get this post resolved. – PA. Jun 19 '16 at 20:30
  • @Mofi it might be more compact, but less clear. – PA. Jun 19 '16 at 20:30

1 Answers1

2

You are using :end in your code, which isn't a command, but rather a label, and does nothing. I suggest using

@echo off
:Ask
echo What command should I excute?
echo 1) ncu -g
echo 2) gem outdated
echo 3) gem update
echo 4) pip list --outdated
echo 5) pip upgrade all packages!
set INPUT=
set /P INPUT=Type input: %=%
If "%INPUT%"=="1" goto cmd1
If "%INPUT%"=="2" goto cmd2
If "%INPUT%"=="3" goto cmd3
If "%INPUT%"=="4" goto cmd4
If "%INPUT%"=="5" goto cmd5
:cmd1
ncu -g
goto :eof
:cmd2
gem outdated
goto :eof
:cmd3
gem update
goto :eof
:cmd4
pip list --outdated
goto :eof
:cmd5
pip freeze --local | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U
goto :eof

goto :eof doesn't need an actual label called :eof, but will simply go to the End Of File (this is also why you shouldn't have a label called :eof)

If this is not your entire code and you still need to do something else in your batch-file, you can use your :quit label and replace goto :eof with goto :quit

Dennis van Gils
  • 3,487
  • 2
  • 14
  • 35