在运行脚本之前检查 OneDrive 是否已启动失败

在运行脚本之前检查 OneDrive 是否已启动失败

我正在尝试创建一个 bat 脚本来检查 OneDrive 是否正在运行,如果 OneDrive 未运行,则将启动 OneDrive.exe。我有两个问题:如果 OneDrive 正在运行,它会在出现消息“正在执行脚本”后关闭 cmd 窗口,并停止运行其余脚本。另一个问题是,如果 OneDrive 未运行,它会在出现消息“OneDrive 正在启动...”后关闭 cmd 窗口,并再次停止运行其余脚本。我该如何解决这两个问题?

@echo off
set /p username="username: "
echo.
echo [33mChecking if OneDrive is running[0m
echo.
timeout 2 >nul

@echo off

Tasklist /FI "IMAGENAME eq OneDrive" | find /I "OneDrive">nul && (
    
    echo [92mOneDrive is running[0m
    timeout 2 >nul
    echo.
)||(
    echo [91mOneDrive is not running![0m
    echo.
    echo [33mStarting OneDrive [0m
    start cmd.exe @cmd /c "runas /user:"driv\%username%" "%LocalAppData%\Microsoft\OneDrive\OneDrive.exe /background"
    echo.
    echo OneDrive is started...
    Pause>nul
    timeout 1 >nul
    echo.
exit
)
@echo off

Tasklist /FI "IMAGENAME eq OneDrive" | find /I "OneDrive">nul && (
    
    echo [33mProceeding script[0m
    timeout 2 >nul
    echo.
)||(
    echo [91m%OneDrive is not running![0m
    echo:
    echo [91mPlease reinstall OneDrive[0m
    timeout 1 >nul
exit
)

@echo off
timeout /T 3 /NOBREAK > nul
echo.
echo Please start robocopy
``

答案1

@echo off && setlocal EnableDelayedExpansion

echo.
set "_status=running"
set /p "_username=username: " 

for /f %%E in ('prompt $E^&%ComSpec% /k ^<nul')do set "_$E=%%~E"

timeout 2 | echo;%_$E%[33mChecking if OneDrive is %_status%!.. %_$E%[0m

tasklist /fi "status eq running" | find /i "onedrive.exe" >nul && (
     set "_status=running"
     timeout 2 | echo; %_$E%[92mOneDrive is running!.. %_$E%[0m
    ) || (
     set "_status=not running"
     echo;%_$E%[91mOneDrive is !_status!^^!!.. %_$E%[0m
     echo;%_$E%[33mStarting OneDrive... %_$E%[0m
     start "" /b 2>&1 runas /user:"driv\%_username%" "%LocalAppData%\Microsoft\OneDrive\OneDrive.exe /background" | find ":"
     timeout 3 >nul 
     tasklist /fi "status eq running" | find /i "onedrive.exe" >nul && (
          set "_status=started"
          echo;OneDrive is !_status!^^!!.. 
         )
    )
    
tasklist /fi "status eq running" | find /i "onedrive.exe" >nul && (
     timeout 2 | echo;%_$E%[33mProceeding script... %_$E%[0m
     goto :Proceeding
    ) || (
     set "_status=not started"
     echo;%_$E%[91mOneDrive is !_status!^^!!..  %_$E%[0m
     echo;%_$E%[91mPlease reinstall OneDrive!.. %_$E%[0m
    )

timeout 3 /nobreak | echo;Please start robocopy
endlocal & exit /b 

:Proceeding

  rem ::  Proceeding script from here
  rem ::  
  rem ::  ...
  rem ::  ...
  rem ::  ...
  rem ::

endlocal

您的问题对于我的英语理解来说有点令人困惑(请原谅我的局限性),但我相信退出可能是导致您出现问题的原因。

相关内容