Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Exiting program gracefully if file not found.  (Read 2157 times)

0 Members and 1 Guest are viewing this topic.

Tigers!

    Topic Starter


    Rookie

    Exiting program gracefully if file not found.
    « on: December 22, 2009, 04:58:32 PM »
    Gudday all
    I had asked a similar question in this thread http://www.computerhope.com/forum/index.php?topic=95717.0 but I want to modify it slightly.
    Currently the code below
    Code: [Select]
    set file_path=D:\Archives\E20_136_JP\ArchiveCreator\Errors\

    rem echo %file_path%

    rem *** Extract all .SPL files ***
    if exist "!file_path!*.spl" (
    dir !file_path!*.spl /b > SPLfiles.txt
    )

    more code here
    gives the messages that SPLfiles.txt not found if there are no *.spl files present.

    What I would like is for the code to exit gracefully if no *.spl files found.
    I suppose I could modfiy
    Code: [Select]
    if exist "!file_path!*.spl" (
    dir !file_path!*.spl /b > SPLfiles.txt
    )
    to be
    Code: [Select]
    if not exist "!file_path!*.spl" (
      ** what would go in here to have the code finish? **
    ) else (
    dir !file_path!*.spl /b > SPLfiles.txt

    ........
    )

    How would I get the code to end where the ** what would go in here to have the code finish? ** is? What would go in here?


    Merry Christmas from Australia

    Dusty



      Egghead

    • I could if she would, but she won't so I don't.
    • Thanked: 75
    • Experience: Beginner
    • OS: Windows XP
    Re: Exiting program gracefully if file not found.
    « Reply #1 on: December 23, 2009, 12:19:44 AM »
    Have a look at EXIT or EXIT/B

    Or you could display a message for example:

    echo No .SPL files found, job terminated..
    Exit (or Exit/b)

    Merry Xmas from over the ditch.
    One good deed is worth more than a year of good intentions.

    Salmon Trout

    • Guest
    Re: Exiting program gracefully if file not found.
    « Reply #2 on: December 23, 2009, 12:57:34 AM »
    You seem familiar with the if ... else structure, which makes me wonder why you don't do something like this

    Code: [Select]
    if exist "!file_path!*.spl" (
        echo Searching for spl files...
        dir !file_path!*.spl /b > SPLfiles.txt
    ) else (
        echo.
        echo No spl files found
        echo.
        echo Press any key to finish . . .
        echo.
        pause>nul
        exit
    )
    REM Note: you will only get to here if at least 1 spl file was found

    I have chose to redirect the pause output message "Press any key to continue . . ." to nul and substitute another, because I sometimes feel the word "continue" is inappropriate when you are in fact not continuing but finishing.

    « Last Edit: December 23, 2009, 06:44:31 AM by Salmon Trout »

    Tigers!

      Topic Starter


      Rookie

      Re: Exiting program gracefully if file not found.
      « Reply #3 on: December 23, 2009, 05:41:10 AM »
      Thanks chaps.   8)
      I'll use it at work after Christmas.