Home > database >  Question of nested if-else condition in batch file
Question of nested if-else condition in batch file

Time:03-04

I don't know why it doesn't work when nested if-else start from "Memo File AND Other File Not Found" to "Memo File Not Found".

Anyone can please help me?

Much appreciated if you could solved. Thanks!

Original Code

@ECHO OFF
SET FOLDER="D:\temp\test"
SET TRANS="D:\temp\test\Trans\Trans.txt"
SET MEMO="D:\temp\test\Memo\Memo.txt"
SET OTHER="D:\temp\test\Other\Other.txt"

IF NOT EXIST %FOLDER% (
    ECHO Folder Not Found
) ELSE (
    IF NOT EXIST %MEMO% (
        IF NOT EXIST %TRANS% (
            IF NOT EXIST %OTHER% (
                ECHO All Files Not Found
            ) ELSE (
                IF NOT EXIST %MEMO% (
                    IF NOT EXIST %TRANS% (
                        ECHO Memo File AND Trans File Not Found
                    ) ELSE (
                        IF NOT EXIST %MEMO% (
                            IF NOT EXIST %OTHER% (
                                ECHO Memo File AND Other File Not Found
                            ) ELSE (
                                IF NOT EXIST %TRANS% (
                                    IF NOT EXIST %OTHER% (
                                        ECHO Trans File AND Other File Not Found
                                    ) ELSE (
                                        IF NOT EXIST %TRANS% (
                                            ECHO Trans File Not Found
                                        ) ELSE (
                                            IF NOT EXIST %MEMO% (
                                                ECHO Memo File Not Found
                                            )
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
            )
        )
    )
)

CodePudding user response:

Does this example output the information you're trying to retrieve:

@ECHO OFF
SET "FOLDER=D:\temp\test"
SET "TRANS=%FOLDER%\Trans\Trans.txt"
SET "MEMO=%FOLDER%\Memo\Memo.txt"
SET "OTHER=%FOLDER%\Other\Other.txt"

IF NOT EXIST "%FOLDER%\*" (
    ECHO Folder Not Found
) ELSE (
    IF NOT EXIST "%MEMO%" (
        IF NOT EXIST "%TRANS%" (
            IF NOT EXIST "%OTHER%" (
                ECHO All Files Not Found
            ) ELSE (
                ECHO Memo File AND Trans File Not Found
            )
        ) ELSE (
            IF NOT EXIST "%OTHER%" (
                ECHO Memo File AND Other File Not Found
            ) ELSE (
                ECHO Memo File Not Found
            )
        )
    ) ELSE (
        IF NOT EXIST "%TRANS%" (
            IF NOT EXIST "%OTHER%" (
                ECHO Trans File AND Other File Not Found
            ) ELSE (
                ECHO Trans File Not Found
            )
        ) ELSE (
            IF NOT EXIST "%OTHER%" (
                ECHO Other File Not Found
            ) ELSE (
                ECHO All Files Found
            )
        )
    )
)
PAUSE

CodePudding user response:

This is the way I would do it:

@ECHO OFF
SET FOLDER="D:\temp\test"
SET TRANS="D:\temp\test\Trans\Trans.txt"
SET MEMO="D:\temp\test\Memo\Memo.txt"
SET OTHER="D:\temp\test\Other\Other.txt"

IF NOT EXIST %FOLDER% (
    ECHO Folder Not Found
    GOTO :EOF
)

SET "NOTFOUND="
IF NOT EXIST %TRANS% SET "NOTFOUND=Trans"
IF NOT EXIST %MEMO% SET "NOTFOUND=Memo %NOTFOUND%"
IF NOT EXIST %OTHER% SET "NOTFOUND=Other %NOTFOUND%"

IF DEFINED NOTFOUND ECHO These files not found: %NOTFOUND%
  • Related