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

Author Topic: Batch program seems to be looping endlessly  (Read 9350 times)

0 Members and 1 Guest are viewing this topic.

iONik

    Topic Starter


    Beginner

    Batch program seems to be looping endlessly
    « on: February 10, 2020, 04:46:58 PM »
    Created a batchfile that seems to be entering an endless loop and am not sure why.
    Would like someone to explain what is wrong here, and since I am terrible at this I believe, don't quote me, but it is probably a no-brainer for some of you!!

    menu6.bat
    Code: [Select]
    @echo off
    color 0a
    title test
    GOTO Main

    :Main
    cls
    echo.
    echo Wipe Free Space on selected drive:
    echo.
    cmdMenuSel f870 "Wipe free space on drive C" "Wipe free space on drive E" "Exit"
    if %ERRORLEVEL% == 1 GOTO DRIVEC
    if %ERRORLEVEL% == 2 GOTO DRIVEE
    if %ERRORLEVEL% == 3 GOTO EXIT
    GOTO Main

    :EXIT
    cls
    cmdMenuSel f870 "Confirm" "Cancel"
    if %ERRORLEVEL% == 1 EXIT
    if %ERRORLEVEL% == 2 GOTO Main
    GOTO EXIT

    :DRIVEE
    cd %windir%\system\Sysinternals
    CLS
    ECHO Pressing ENTER will wipe(1 pass) the free space on drive e:
    pause
    call sdelete -c -z e:
    GOTO Main

    :DRIVEC
    cls
    time
    echo.
    date
    echo.
    echo By iONic
    GOTO Main

    The DRIVEC loop works but the DRIVEE loop fails after performing the function.


    menu5.bat
    Code: [Select]
    @Echo off
    :TOP
    color 0b
    @Mode 70,25
    @Title %~n0
    Batbox /h 0

    :Loop
    Call Button  20 4 "Wipe free space on drive C" 20 8 "Wipe free space on drive E" 25 12 "Exit the program" # Press
    Getinput /m %Press% /h 70

    :: Check for the pressed button
    if %errorlevel%==1 (GOTO DRIVEC)
    if %errorlevel%==2 (GOTO DRIVEE)
    if %errorlevel%==3 (exit)
    goto Loop

    :DRIVEC
    cd %windir%\system\Sysinternals
    CLS
    ECHO.
    ECHO Pressing ENTER will wipe(1 pass) the free space on drive C:
    ECHO.
    pause
    call sdelete -c -z c:
    GOTO Loop

    :DRIVEE
    cd %windir%\system\Sysinternals
    CLS
    ECHO.
    ECHO Pressing ENTER will wipe(1 pass) the free space on drive E:
    ECHO.
    pause

    This one fails after successfully executing but end up in a endless loop with an error "button is not recognizes as an internal or external command"

    Your help would be greatly appreciated.


    Geek-9pm


      Mastermind
    • Geek After Dark
    • Thanked: 1026
      • Gekk9pm bnlog
    • Certifications: List
    • Computer: Specs
    • Experience: Expert
    • OS: Windows 10
    Re: Batch program seems to be looping endlessly
    « Reply #1 on: February 10, 2020, 05:19:31 PM »
    Have you ever done batch files before?
    Do you really understand this:
    Code: [Select]
    if %ERRORLEVEL% == 3 GOTO EXIT
    It might mean something you did not expect.
    Do you program is some other language?

    iONik

      Topic Starter


      Beginner

      Re: Batch program seems to be looping endlessly
      « Reply #2 on: February 10, 2020, 05:34:30 PM »
      Have you ever done batch files before?
      Do you really understand this:
      Code: [Select]
      if %ERRORLEVEL% == 3 GOTO EXIT
      It might mean something you did not expect.
      Do you program is some other language?


      Told you I was bad at this. As far as
      Code: [Select]
      if %ERRORLEVEL% == 3 GOTO EXITin the first batch file if i select 3 and press enter the batch file will begin executing at :EXIT
      What follows is merely conformation to exit the batch file.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch program seems to be looping endlessly
      « Reply #3 on: February 10, 2020, 06:01:11 PM »
      In other languages the IF state rests fir a deterministic truth has has one meaning.
      In BATCH, the error level is more like a maybe test.
      I is like asking "How bad was the error?" The answer might not be a direct specific answer. Instead, it rates the error on a scale of bad to worse.
      Here I am not going to use exact syntax. The is not the point.

      Q. "Hey batch, how bad was the error. As much as a one?
      A. if error-level = 1 "Yeah, I can say that it would be.
      Q. "Hey batch, how bad news in error. As much as a two?
      A. if error-level = 2 "Yeah, I can say at least a 2.".
      Q. "Hey batch, how high was the error. Maye a three?"
      A. if error-level = 3 "I will say thee, if not more."
      Q. "Hey batch,  did you get error. Up to four?"
      A. if error-level = 4 "Four for sure, less I don't see."


      Get it? Read over the examples is samples in the tutorials.
      BATCH is not like any other task control tool. 

      BC_Programmer


        Mastermind
      • Typing is no substitute for thinking.
      • Thanked: 1140
        • Yes
        • Yes
        • BC-Programming.com
      • Certifications: List
      • Computer: Specs
      • Experience: Beginner
      • OS: Windows 11
      Re: Batch program seems to be looping endlessly
      « Reply #4 on: February 10, 2020, 06:46:33 PM »
      In your :DRIVEE label you change the current directory, but you do not change it back before you return to MAIN, so I'd wager it cannot find cmdmenusel anymore so you get stuck in an infinite loop.

      I think that may be the case for your second batch file as well. I'd suggest that you use PUSHD to change to the sysinternals directory, and then before you return you use POPD.

      I don't think Geek-9pm's comments are relevant, as he is thinking of a slightly different construct ("IF ERRORLEVEL X <command>") which is not what you are using.
      I was trying to dereference Null Pointers before it was cool.

      Geek-9pm


        Mastermind
      • Geek After Dark
      • Thanked: 1026
        • Gekk9pm bnlog
      • Certifications: List
      • Computer: Specs
      • Experience: Expert
      • OS: Windows 10
      Re: Batch program seems to be looping endlessly
      « Reply #5 on: February 10, 2020, 08:20:14 PM »
      I stand corrected.  :-[
      No excuse.

      iONik

        Topic Starter


        Beginner

        Re: Batch program seems to be looping endlessly
        « Reply #6 on: February 11, 2020, 04:14:46 PM »
        In your :DRIVEE label you change the current directory, but you do not change it back before you return to MAIN, so I'd wager it cannot find cmdmenusel anymore so you get stuck in an infinite loop.

        I think that may be the case for your second batch file as well. I'd suggest that you use PUSHD to change to the sysinternals directory, and then before you return you use POPD.

        I don't think Geek-9pm's comments are relevant, as he is thinking of a slightly different construct ("IF ERRORLEVEL X <command>") which is not what you are using.
        Thanks, BC_Programmer, for the tip. It worked perfectly. I deleted the CD line and added the path to the call statement.
        Can also work if I place the needed batch files in the same directory. :D