Problem with ELSE.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PAB
Posts: 113
Joined: 12 Aug 2019 13:57

Problem with ELSE.

#1 Post by PAB » 21 Jul 2020 17:30

Good evening,

I have this code . . .

Code: Select all

set "Drive="
echo  Which drive would you like to check for ERRORS?
set /p "Drive=>Please enter the drive letter and press <Enter>: "

if not exist %volume% %Drive%:\ (
  echo  ERROR: The drive does NOT exist^^!
  echo ^>Press ANY key to return to the Options . . . & pause >nul
  cls & goto :Options
) else (
  if "%Drive%"=="C" (
  chkdsk %Drive%: /F
  echo  press ^<ENTER^> to return to the Options.
  echo  press ^<Y^> and ^<ENTER^>.
  goto :REBOOT_Choice
) else (
  chkdsk %Drive%: /F
  Please thoroughly check the results above
  echo ^>Press ANY key to return to the Options . . . & pause >nul
  cls & goto :Options
 )
)
What works OK:

[1] If I press <ENTER> instead of entering a drive letter it gives me the correct message . . .

Code: Select all

  echo  ERROR: The drive does NOT exist^^!
[2] If I enter an incorrect drive letter it gives me the correct message . . .

Code: Select all

  echo  ERROR: The drive does NOT exist^^!
[3] If I plugin a USB and it gets allocated drive letter G: for example, and I then run the code, it performs the chkdsk correctly and gives me the correct text after it has finished as shown in the code here . . .

Code: Select all

  Please thoroughly check the results above
  echo ^>Press ANY key to return to the Options . . . & pause >nul
  cls & goto :Options
What DOESN'T works OK:

If I enter C as the drive letter, it prompts me to either enter "Y" to perform chkdsk on the next reboot, or "N" to cancel the process. So far so good.

Code: Select all

  if "%Drive%"=="C"
Regardless of whether I enter "Y" or "N" I want it to show this . . .

Code: Select all

  echo  press ^<ENTER^> to return to the Options.
  echo  press ^<Y^> and ^<ENTER^>.
. . . and regardless of which I enter for it to . . .

Code: Select all

  goto :REBOOT_Choice
I have spent hours on this and it is almost there.

Any help will be greatly appreciated.

Thanks in advance.

PAB
Posts: 113
Joined: 12 Aug 2019 13:57

Re: Problem with ELSE.

#2 Post by PAB » 22 Jul 2020 07:22

I just couldn't seem to get it to do the WHOLE thing using If..Else...Else so I have come up with this . . .

Code: Select all

set "Drive="
echo  Which drive would you like to check for ERRORS?
set /p "Drive=>Please enter the drive letter and press <Enter>: "

if /i not exist %Drive%:\ goto :ERROR
if /i "%Drive%"=="C" goto :Drive_C
if /i not "%Drive%"=="C" goto :Drive_Other

:ERROR

echo  ERROR: The drive does NOT exist.
pause
cls & goto :Options

:Drive_C

chkdsk %Drive%: /F
echo  press ^<ENTER^> to return to the Options.
echo  press ^<Y^> and ^<ENTER^> to REBOOT.
goto :REBOOT_Choice

:Drive_Other

chkdsk %Drive%: /F
echo Please thoroughly check the results above
pause
cls & goto :Options
If anybody can get the above nested it would be great.

Thanks in advance.

IcarusLives
Posts: 121
Joined: 17 Jan 2016 23:55

Re: Problem with ELSE.

#3 Post by IcarusLives » 23 Jul 2020 13:36

Perhaps your problem is this line?

Code: Select all

chkdsk %Drive%: /F
It should be

Code: Select all

chkdsk %Drive%:\ /F

Post Reply