在批处理blah.bat中找不到文件

时间:2019-07-02 17:04:13

标签: batch-file

: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

1 个答案:

答案 0 :(得分:0)

这是带有批注的批处理文件,这些批注是以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

除评论外,我建议阅读:

请注意,用%~dp0引用的目录路径始终以反斜杠结尾,因此,即使上面的代码使文件/文件夹字符串更难以阅读。

要了解所使用的命令及其工作方式,请打开命令提示符窗口,在其中执行以下命令,并非常仔细地阅读每个命令显示的所有帮助页面。

  • cls /?
  • dir /?
  • echo /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • start /?