.BAT - IF命令不起作用

时间:2014-03-08 13:38:24

标签: batch-file if-statement

好的,我可能正在做一些极其错误的事情。

我想这样做:

in C:\programs\app\install.exe

是安装程序。

它应该安装到:

c:\program files\app

我的问题是如何将.bat文件中的IF命令设置为:

if c:\program files\app\appStart.exe  exist start c:\program files\app\appStart.exe
if c:\program files\app\appStart.exe  not exist start C:\programs\app\install.exe

使其更简单我在此文件夹中的此文件上进行测试(从未尝试过上面的文件夹和文件)

c:\folder\1.txt
c:\folder\2.txt
c:\folder\A.txt
c:\folder\B.txt

我想做:

if c:\folder\1.txt exist start c:\folder\A.txt
if c:\folder\1.txt not exist start c:\folder\B.txt

我在google上找到了大量有回答的网站,但似乎没有什么对我有用,他们甚至都不会打开。

试着这个:

IF EXIST "C:\Nowy folder" (
    REM 1.txt
    IF EXIST "1.txt" (Start "C:\folder\1.txt") ELSE (Start "C:\folder\A.txt")
) ELSE (
    REM 2.txt
    IF EXIST "2.txt" (Start "C:\folder\2.txt") ELSE (Start "C:\folder\B.txt")
)
Pause&Exit

or this 

if exist "%CD%\1.txt" (Start /wait "%CD%\A.txt") ELSE  Start /wait "%CD%\B.txt"
Pause

也行不通。

无论我提供完整路径还是%CD%,它都不起作用。

任何人都可以告诉我我做错了什么?

2 个答案:

答案 0 :(得分:0)

if exist "c:\program files\app\appStart.exe" ( start "" "c:\program files\app\appStart.exe"
 ) else (start "" "C:\programs\app\install.exe")

您需要"quote names containing spaces"并且START的第一个引用参数成为窗口标题,因此您为第一个参数(或"whatever window title you want")添加一个空引号字符串

我对你问题的其余部分感到困惑。还有一个问题吗?

答案 1 :(得分:0)

这将检查两个exe文件,并将优先使用第一个exe文件。

@echo off
if exist "c:\program files\app\appStart.exe" (
        start "" "c:\program files\app\appStart.exe"
   ) else (
       if exist "C:\programs\app\install.exe" start "" "C:\programs\app\install.exe"
)