Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Batch file 如何编写一个批处理脚本,在两个文件路径中查找可执行文件,然后运行它_Batch File_If Statement - Fatal编程技术网

Batch file 如何编写一个批处理脚本,在两个文件路径中查找可执行文件,然后运行它

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 (

我编写了一个批处理文件,在两个文件夹路径中查找可执行文件,然后运行它。我刚开始编写批处理文件,有人告诉我这是草率的,可以用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\
)
你可以做:

@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
)