Cmd: if A and B exist, then

What is the easiest and / or most readable method for IFc ANDin the CMD shell? In pseudo code:

IF EXIST file1 AND file2:
   then do stuff
ELSE: 
   do something else

this Q should be somewhere in SO, but my search few does not work for this. excuse me

+5
source share
5 answers

Assuming you're talking about DOS / Windows batch files, I think you want something like this:

SET do_stuff=false
IF EXIST file1 IF EXIST file2 SET do_stuff=true
IF "%do_stuff%"=="true" (
    REM do stuff
) ELSE (
    REM do something else
)

, DOS if / , if ( then else), , if ( ). , . , .:)

(SO, ), , (help if if /? IIRC).

+13

@Randall Cook :

IF EXIST file1 IF EXIST file2 (
  do stuff
  GOTO cont
)
do something else

:cont
get on with other stuff
+5
IF EXIST A (
    IF EXIST B (
        ECHO A and B exist
        )
    )
+4

, @RandallCook answer . , :

@echo off
IF EXIST "File1" IF EXIST "File2" GOTO :do_stuff
GOTO :not_exist
GOTO :EOF

:do_stuff
    echo File1 and File2 exist.
    echo -- Doing stuff here...
    goto :EOF

:not_exist
    echo  Condition not met, not doing stuff.
    goto :EOF

:EOF , script.

, CALL over GOTO, , , :

@echo off
:: Successful CD resets errorlevel to 0, in case it was already set this shell
cd
IF EXIST "File1" IF EXIST "File2" CALL :do_stuff
IF ERRORLEVEL 10 GOTO :EOF
CALL :not_exist
GOTO :EOF

:do_stuff
    echo File1 and File2 exist.
    echo -- Doing stuff here...
    exit /b 10
    goto :EOF

:not_exist
    echo  Condition not met, not doing stuff.
    goto :EOF
+1
set /p k="Please enter Choice : "

"% k%" == "A" goto A if "% k%" == "B" goto B

:   echo "Hello from A" : B   echo "Hello from B"

0

All Articles