从文件中加载文本作为变量时出错,并对其进行检查

时间:2019-07-25 20:53:30

标签: batch-file cmd

这是一些代码,它假定检查某个位置中的adb.exe,然后如果存在则将继续进行编码,如果不存在,将要求用户手动输入位置,然后再次检查位置是否正确,将输入保存在文件“ dontremoveoredit”中”以在用户下次打开bat文件时使用它


问题是,如果文件为空或包含路径,它将使蝙蝠崩溃并关闭

:: @echo off
If exist "C:\Program Files\ui\adb.exe" (
    goto yes
) else (
    goto adbexist
    ) 

:yes
set PATH=%PATH%;C:\Program Files\ui"
:: @cd/d "%~dp0"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
Echo.
Echo.
Echo.
echo Yes Was Executed
Pause>nul
Exit 

:no    
msg * "Couldn't find The adb.exe in Default Bath"
ping localhost -n 3 >Nul 
goto noadbtext


:adbexist
If exist dontremoveoredit (
    goto adbexist1
) else (
    goto no
    ) 

:adbexist1
set /p var=<dontremoveoredit
if [%var%] == [] (
    goto noadbtext
) else (
    goto lala
    )
:lala
If exist %var%\adb.exe (
    set PATH=%PATH%;%var%"
    adb.exe kill-server
    adb.exe devices
    adb connect localhost:5037
    goto dns
) else (
    msg * "Make Sure You Entered The Right Path To UI"
    goto noadbtext
    ) 

:noadbtext
break>"dontremoveoredit"
set /p EmulatorUIBath=Enter Your Emulator's UI folder Path : 
If exist "%EmulatorUIBath%\adb.exe" (
    @echo %EmulatorUIBath%>> "dontremoveoredit"
    goto adbexist2
) else (
    msg * "Make Sure You Entered The Right Path To UI"
    goto noadbtext
    ) 


:adbexist2
set PATH=%PATH%;%EmulatorUIBath%"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
echo adb Exist2 
Pause>nul
exit

1 个答案:

答案 0 :(得分:0)

在这里,您是我的笔记本电脑快要死了,但这应该做得很好。 :)

我提到过的版本稍有更新。

@(
  SETLOCAL EnableDelayedExpansion
  ECHO OFF
  SET "_ConfigFilePath=%~0dpn_DO_NOT_REMOVE.dat"
  SET "_ADBPath="
  SET "_eLvl=0"
  SET "ECHO_RW=Call :Echo_Color CF "
  SET "ECHO_GB=Call :Echo_Color 20 "
  SET "ECHO_AB=Call :Echo_Color B0 "
  SET "ECHO_LY=Call :Echo_Color 1E "
  SET "ECHO_YL=Call :Echo_Color E1 "
  SET "ECHO_YR=Call :Echo_Color EC "
  SET "ECHO_RY=Call :Echo_Color CE "
  SET "ECHO_BY=Call :Echo_Color 1E "
  )

CALL :Main

(
  ECHO. Script Completed With ErrorLevel of %_eLvl%
  ENDLOCAL
  Exit /B %_eLvl%
)

:Echo_Color
  ECHO.>"%~2"
  FINDStr /A:%~1 /I ".*" "%~2" NUL
  DEL /F /Q "%~2" >nul 2>nul
GOTO :EOF

:Main
  REM Checing for ADB
  CALL :CheckADB

  IF /I %_eLvl% NEQ 0 (
    ECHO. Error Encountered, Exiting!
  ) ELSE (
    CALL :RunADB
  )
  PAUSE

GOTO :EOF



:CheckADB
  REM Check For ADB Installed in Program Directories
  FOR /F "Tokens=1* Delims==" %%A IN ('
    SET ProgramFiles
  ') DO (
    FOR /F "tokens=*" %%a IN ('
      WHERE /R "%%A" /F "adb.exe" 2^>nul
    ') DO (
      ECHO Found ADB here: "%%~a"
      SET "_ADBPath=%%~a"
    )
  )
  REM ECHO.Finished Where Loop
  IF NOT DEFINED _ADBPath (
    ECHO. ADB Install not Found, checking Config file.
    IF EXIST "!_ConfigFilePath!" (
      ECHO. Config File Exists checking contents..
      FOR /F "Tokens=*" %%A IN ('
        TYPE "!_ConfigFilePath!"
      ') DO (
        ECHO. Saved ADB Path Found: "%%~A" in Config file.
        IF EXIST "%%~A" (
          SET "_ADBPath=%%~A"
        ) ELSE (
          ECHO. Saved ADB Path "%%~A" Does Not Exist!  Deleteing Config File.
          CALL :Get_ADB_Path_From_User
        )
      )
    ) ELSE (
      CALL :Get_ADB_Path_From_User
    )
  )
GOTO :EOF


:Get_ADB_Path_From_User
  CLS
  REM COLOR CF
  ECHO.
  COLOR 1E
  FOR %%A IN (
    "==========================================================="
    "=           ADB was not detected on your system           ="
    "==========================================================="
  ) DO (
    REM ECHO.>"%%~A"
    REM FINDStr /A:CF /I ".*" "%%~A" NUL
    REM DEL /F /Q "%%~A" >nul 2>nul
    %ECHO_RY% "%%~A"
  )
  ECHO.
  ECHO.
  %ECHO_AB% "Please enter a custom path to ADB below."
  ECHO.
  ECHO.
  SET /P "_TmpADBPath=What Is the Path to your ADB Install? "
  CLS
  IF NOT EXIST "%_TmpADBPath%" (
    %ECHO_RW% "==========================================================="
    %ECHO_RW% "==                                                       =="
    %ECHO_RW% "==    Unable to Verify that The path Provided Exists     =="
    %ECHO_RW% "==                                                       =="
    %ECHO_RW% "==========================================================="
    SET "_eLvl=1"
    GOTO :EOF
  ) ELSE (
    COLOR 2F
    SET "_ADBPath=%_TmpADBPath%"
    ECHO.===========================================================
    ECHO. Path Exists:
    ECHO. 
    ECHO. "%_TmpADBPath%"
    ECHO. 
    ECHO. ===========================================================
    ECHO.
    ECHO.
    CHOICE /M "Save this Path for future Use?"
    IF /I "%ERRORLEVEL%" EQU "0" (
      ECHO.%_TmpADBPath%>"%_ConfigFilePath%"
      ECHO.
      ECHO. Saved to: "%_ConfigFilePath%"
    ) ELSE (
      ECHO.
      ECHO. Okay, Will only use it temporarily.
    )
      COLOR
  )
GOTO :EOF

:RunADB
    "%_ADBPath%" kill-server
    "%_ADBPath%" devices
    "%_ADBPath%" connect localhost:5037
    Echo.
    Echo.
    Echo.
    Echo.Yes Was Executed
    Pause>nul
Exit