-1
:exe
dir /a plugins
cls
set /p load="Which file do you want to load?: "
if exist plugins\%load%.bat goto loadtrue
if not exist plugins\%load%.bat goto loadfail
:loadfail
cls
echo error, file does not exist or you typed the file name wrong, try again
pause
goto exe
:loadtrue
cls
start %load%.bat
pause
goto terminal
Compo
  • 36,585
  • 5
  • 27
  • 39
  • 3
    Please revisit your question to bring it on topic. You can read [ask] and its associated links, to find out how to do so, and thus improve your chances of receiving help. – Compo Jul 02 '19 at 17:14
  • Blah.bat is in the folder plugins –  Jul 02 '19 at 17:16
  • 2
    I have voted to have the above closed. Had you made an attempt at following the advice in my previous comment, or at creating an on topic question, I wouldn't have needed to. Please follow that advice, should you wish to prevent the inevitable closure of your 'question'. – Compo Jul 02 '19 at 17:42
  • Hi @LEVI, your paths are not "rooted" meaning that the path is whatever your current working directory is plus the path you are using. This is most likely what your problem is. Before the line `if exist plugins\%load%.bat` , try dding a debug statement like `echo looking in %CD%\plugins\%load%.bat for the file.`. You should immediately see what your problem is. – Señor CMasMas Jul 02 '19 at 17:56
  • One more thing @LEVI... If you run a batch file as an administrator, your working directory will be "%windir%\System32" (a bug imho), otherwise, (by default) it will be the directory the batch is in. If you add `cd /d %~DP0` to the beginning of your batch file, it will always be in the directory you expect. – Señor CMasMas Jul 02 '19 at 18:01
  • can you rewrite my code to be correct? –  Jul 02 '19 at 18:07
  • then nothing i will delete my account –  Jul 02 '19 at 18:32

1 Answers1

0

Here is a batch file with comments which are the lines starting with rem:

@echo off

:UserPrompt
cls
rem Output the names of all batch files in subdirectory plugins in directory
rem of this batch file without file extension and next an empty line.
for /F "eol=| delims=" %%I in ('dir "%~dp0plugins\*.bat" /A /B 2^>nul') do echo %%~nI
echo/

rem Make sure the environment variable Load is not defined by chance
rem for example by a previous execution of this batch file in same
rem Windows command prompt window.
set "Load="

rem Prompt the user for the batch file name to load respectively execute.
set /P "Load=Which file do you want to load? "

rem Has the user input anything at all?
if not defined Load goto UserPrompt

rem Remove all double quotes from input string.
set "Load=%Load:"=%"

rem Is there anything left after removing all double quotes?
if not defined Load goto UserPrompt

rem Is there a batch file with user input name in subdirectory plugins
rem in the directory containing this batch file?
if exist "%~dp0plugins\%Load%.bat" goto StartPlugin

echo/
echo Error: The file name was typed wrong.
echo Please try it again.
echo/
pause
goto UserPrompt

:StartPlugin
cls
rem Start a separate command process for execution of the user selected
rem batch file with window title of console window set to name of batch
rem file and current directory set to subdirectory plugins of the
rem directory containing this batch file.
start "%Load%" /D"%~dp0plugins" ".\%Load%.bat"
pause
goto Terminal

:Terminal

I recommend to read in addition to the comments:

Please note that the directory path referenced with %~dp0 always ends with a backslash and therefore concatenation of this path with a directory or file name should be done without an additional backslash as shown in code above even if it makes the file/folder strings more difficult to read.

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.

  • cls /?
  • dir /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • start /?
Mofi
  • 46,139
  • 17
  • 80
  • 143