#1 09 Dec 2019 13:15

PiotrMP006
Member
Registered: 02 Apr 2019
Posts: 10

If Else bracket error Help

Why this code does not work?

if exist "D:\Test1" if exist "D:\Test2" (
echo OK
) else (
echo Error
)
pause

This code works but I don't know why this extra brace?

if exist "D:\Test1" if exist "D:\Test2" (
echo OK
)
) else (
echo Error
)
pause

Please Help

Offline

#2 09 Dec 2019 21:43

Rekrul
Member
Registered: 17 Apr 2016
Posts: 53

Re: If Else bracket error Help

PiotrMP006 wrote:

Why this code does not work?

if exist "D:\Test1" if exist "D:\Test2" (
echo OK
) else (
echo Error
)
pause

This code works but I don't know why this extra brace?

if exist "D:\Test1" if exist "D:\Test2" (
echo OK
)
) else (
echo Error
)
pause

Please Help

For what it's worth, your first script works fine for me in Windows XP. The second complains that ") was unexpected at this time."

Offline

#3 10 Dec 2019 14:26

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,101

Re: If Else bracket error Help

What are you trying to achieve?  Do you want it only print to print "OK" if both of the files exist?

As intimated by Rekrul, I'm not sure your second code example has been pasted in correctly due to the unbalanced "( )".

I'm guessing maybe you are looking for something like:

if exist "D:\Test1" (
  if exist "D:\Test2" (echo OK) else (echo Error)
) else (
  echo Error
)
pause

But chaining conditions like this becomes hard to work with as you wind up having to implement your "else" responses repeatedly.

As such, I would structure it more like this:

set "MISSING="
for %%a in ("D:\Test1" "D:\Test2") do (if not defined MISSING if not exist "%%~a" set "MISSING=true")
if "%MISSING%" EQU "true" (echo:OK) else (echo:Error missing file^(s^))
pause

Last edited by bluesxman (17 Dec 2019 09:16)


cmd | *sh | Ruby | Chef

Offline

Board footer

Powered by FluxBB