views:

162

answers:

3

Hi Guys, I wrote a simple batch file which checks whether the c drive path exists then execute the exe in that path else try the d drive path and execute it.

IF EXIST c:\program files\x goto a 

ELSE goto b


:a
cd c:\program files\x

executable.exe  c:\temp\col.zip 


:b
cd d:\program files\x

executable.exe  c:\temp\col.zip

Im getting this error:

----Error Ouput-- 'ELSE' is not recognized as an internal or external command, operable program or batch file. The system cannot find the path specified. 'executable.exe' is not recognized as an internal or external command, operable program or batch file. 'dellsysteminfo.exe' is not recognized as an internal or external command, operable program or batch file.

I don't know why.

A: 

The error message is pretty self-explanatory, there's no such thing as ELSE in batch files. However, since it's a GOTO, it's entirely unnecessary.

IF EXIST c:\program files\x goto a

goto b

If it makes it past the first line, it's inherently an else.

As for the other errors, they're related to not finding the files you're trying to execute. Batch files are case-sensitive, so you need to fix the capitalization of the file/folder names to match the actual system.

Chad Birch
I m able to fix the else correction, want to thank you for that.But the second error is still existing.I am providing the actual exact path but for some reason that error keeps on coming
alice7
You can't be, the error is directly saying that the file does not exist.
Chad Birch
does it have to do with 32 or 64 bit machine.Because I can see the file, because I downloaded the file into that path.
alice7
64-bit Windows adds a second program files directory for 32-bit applications. Are you sure you're aiming at the right one? The 32-bit application one is called "Program Files (x86)".
Chad Birch
Oh, you probably also need to put double-quotes around your `cd` commands, like: `:a cd "c:\program files\x"`
Chad Birch
There is an `else`, just look at `help if`.
Joey
And batch files are *not* case-sensitive. The entire answer is pretty much nonsense. And quotes aren't necessary for `cd`—again, see the help: “`CHDIR` command does not treat spaces as delimiters, so it is possible to `CD` into a subdirectory name that contains a space without surrounding the name with quotes.”.
Joey
A: 

Yep, theres no multiline if/else, just do this

IF EXIST c:\program files\x goto a
goto b

:a 
cd c:\program files\x
executable.exe c:\temp\col.zip
rem don't you want a goto here??


:b 
cd d:\program files\x
executable.exe c:\temp\col.zip
John Knoeller
I was able to fix the first error but the second error still exist.Even if I provided the absolute path.
alice7
See JRL's answer. You can easily make it multi-line, however, the `else` has to appear on the same line as the `if` (or the end of the block opened after the `if`). Yes, that's a little confusing in the documentation.
Joey
+2  A: 

The ELSE must be on the same line. Change it to:

IF EXIST c:\program files\x (
  goto a
) ELSE (
  goto b
)

See this tutorial for more details, or refer to this Microsoft documentation.

JRL