Batch file 如何编写一个批处理脚本,在两个文件路径中查找可执行文件,然后运行它
我编写了一个批处理文件,在两个文件夹路径中查找可执行文件,然后运行它。我刚开始编写批处理文件,有人告诉我这是草率的,可以用if/else语句更好地编写Batch file 如何编写一个批处理脚本,在两个文件路径中查找可执行文件,然后运行它,batch-file,if-statement,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 (
@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\
)
你可以做:
@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
或
或者更好的是,在环境中找到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
)
因此,您要在任一文件夹中查找
helper.exe
,如果存在,则执行该文件?如果尝试,则执行的语法不完整。当然,如果存在,还需要将完整路径放入,仅helper.exe
是不够的。您也可以尝试以下操作:%ProgramFiles%\Mozilla Firefox\uninstall\helper.exe”/s
%ProgramFiles(x86)%\Mozilla Firefox\uninstall\helper.exe”/s
@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
)