1.

Solve : Batch program seems to be looping endlessly?

Answer»

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.

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? Quote from: Geek-9pm 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?


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.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. 
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 ") which is not what you are using.
I stand corrected. 
No excuse. Quote from: BC_Programmer 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.
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.


Discussion

No Comment Found