如何编写批处理脚本,该脚本将在可执行文件的两个文件路径中查找然后运行

时间:2019-03-21 14:11:42

标签: batch-file if-statement

我编写了一个批处理文件,该文件在两个文件夹路径中查找可执行文件,然后运行它。我刚开始编写批处理文件,并被告知这很草率,可以使用if / else语句更好地编写。

@echo off
Taskkill /im firefox.exe >nul 2>nul
echo Remove and re-install Mozilla Firefox
"C:\program files\Mozilla Firefox\uninstall\helper.exe" /s 
"C:\program files (x86)\Mozilla Firefox\uninstall\helper.exe" /s

到目前为止,我发现没有任何东西似乎起作用。这是最后一次无效的尝试;

@echo off
Taskkill /im firefox.exe >nul 2>nul
echo Remove and re-install Mozilla Firefox

IF exist helper.exe /s ( "C:\program files\Mozilla Firefox\uninstall\
) else helper.exe /s ( "C:\program files (x86)\Mozilla firefox\uninstall\
)

1 个答案:

答案 0 :(得分:1)

您可以这样做:

@echo off
taskkill /im firefox.exe >nul 2>nul
echo Remove and re-install Mozilla Firefox
if exist "C:\program files\Mozilla Firefox\uninstall\helper.exe" (
    "C:\program files\Mozilla Firefox\uninstall\helper.exe" /s
  ) else (
    "C:\program files (x86)\Mozilla firefox\uninstall\helper.exe" /s
)

但您实际上并不需要else语句:

@echo off
taskkill /im firefox.exe >nul 2>nul
echo Remove and re-install Mozilla Firefox
if exist "C:\program files\Mozilla Firefox\uninstall\helper.exe" /s
if exist "C:\program files (x86)\Mozilla firefox\uninstall\helper.exe" /s

"C:\program files\Mozilla Firefox\uninstall\helper.exe" /s || "C:\program files (x86)\Mozilla firefox\uninstall\helper.exe" /s

甚至更好的是,在环境中找到Firefox的路径(如果安装正确)并使用它的路径:

@echo off
taskkill /im firefox.exe >nul 2>nul
echo Remove and re-install Mozilla Firefox
for /f "delims=" %%i in ('where firefox.exe') do (
    "%%~dpihelper.exe" /s
)