1

I have an unfinished batch file that wont jump to the :TBS label. It will always go to the :BSO label no matter what you put in for the first question. "A" should go to :BSO and "B" should go to :TBS. I don't know how to fix this. I have tried everything I can think of. Any help appreciated.

@echo off
echo ------------------------------------------Computer Optimization and Fixer 1.1-------------------------------------------
echo.
echo Welcome to Computer Optimization and Fixer 1.1 by Anston
echo.
echo Computer Optimization and Fixer 1.1 will do a basic optimization and fix issues on your computer. For example...
echo It can run sfc, chkdsk, Defrag or Optimize, Disk Cleanup, DISM, troubleshooters, and more.
echo.
set /p A="Would you like to do a basic optimization(A) or fix a certain issue(B)?"
IF %A% EQU A GOTO:BSO
IF %A% EQU B GOTO:TBS
:BSO
echo.
echo Running basic optimization...
pause
start "dfrgui.exe" /wait "C:\Windows\System32\dfrgui.exe"
sfc /scannow
chkdsk /r
start diskclean
GOTO:END
:TBS
echo.
set /p ch="What problem do you have?(Power[A], Internet[B], Performance[C], Files[D], Windows Apps[E], Windows Update[F], Devices[G], Audio[H], Search[I], or Printer[J])
IF %ch% EQU A GOTO:PWR
IF %ch% EQU B GOTO:INT
IF %ch% EQU C GOTO:PRF
IF %ch% EQU D GOTO:FLS
IF %ch% EQU E GOTO:WNA
IF %ch% EQU F GOTO:WNU
IF %ch% EQU G GOTO:DEV
IF %ch% EQU H GOTO:AUD
IF %ch% EQU I GOTO:SCH
IF %ch% EQU J GOTO:PRN
:PWR
echo.
echo Running Troubleshooter...
msdt.exe /id PowerDiagnostic
pause
GOTO:END
:END
cls
echo ------------------------------------------Computer Optimization and Fixer 1.1-------------------------------------------
echo.
echo Thank you for using Computer Optimization and Fixer 1.1
pause
Anston Sorensen
  • 323
  • 1
  • 15
  • 2
    It isn't jumping to :BSO - it is getting there because none of the tests match anything. – Jerry Jeremiah May 29 '20 at 02:40
  • 2
    Maybe you could echo %A% to see what is in it before the tests and exit the batch file if nothing matches instead of falling through to BSO (unless of course you want the default to be BSO) – Jerry Jeremiah May 29 '20 at 02:46
  • 2
    The `IF` command is also case sensitive unless you use the `/I` option. – Squashman May 29 '20 at 03:05
  • The if condition isn't being satisfied therefore it goes to`:BSO` since it is beneath the commands. Try putting `ECHO fail` below the if statement and see it it goes to fail. – Nico Nekoru May 29 '20 at 03:26
  • 2
    The best practice for comparing strings is: `IF "%var%"=="A" command`. – Squashman May 29 '20 at 03:27
  • 2
    `%variable% EQU value` is for numbers, `"%variable%" EQU "value"` is for strings. `"%variable%"=="value%"` is for both and is better overall. – SomethingDark May 29 '20 at 03:44
  • ECHO %A% says b when I input b. ECHO fail works too. I used IF "%var%"=="A". Nothing works. It still goes straight to BSO. Maybe someone should try to edit my code and fix it? – Anston Sorensen May 29 '20 at 04:15
  • 1
    Well, have you read Squashman's comments, particularly the first one? And have you ever typed `if /?` into a Command Prompt window? – aschipfl May 29 '20 at 08:19

1 Answers1

0

There is a command available since Windows Vista and on Windows Server versions since Windows Server 2003 for choice menus: CHOICE

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "ToolVersion=1.1"
cls
echo --------------------- Computer Optimization and Fixer %ToolVersion% ---------------------
echo(
echo Welcome to Computer Optimization and Fixer %ToolVersion% by Anston
echo(
echo Computer Optimization and Fixer %ToolVersion% will do a basic optimization
echo and fix issues on your computer. For example, it can run Check Disk
echo Utility, Disk Space Cleanup Manager for Windows, Disk System Integrity
echo Check and Repair, Disk Defragmenter, troubleshooters, and more.
echo(
echo What do you want to do?
echo(
echo    A ... do a basic optimization
echo    B ... fix a certain issue
echo    E ... do nothing and exit
echo(
%SystemRoot%\System32\choice.exe /C ABE /N /M "Your choice:"
if errorlevel 3 exit /B
if errorlevel 2 goto TBS

echo(
echo Running basic optimization...
%SystemRoot%\System32\chkdsk.exe /r
%SystemRoot%\System32\cleanmgr.exe
%SystemRoot%\System32\sfc.exe /scannow
%SystemRoot%\System32\dfrgui.exe
goto END

:TBS
echo(
echo What problem do you have?
echo(
echo    A ... Power
echo    B ... Internet
echo    C ... Performance
echo    D ... Files
echo    E ... Windows Apps
echo    F ... Windows Update
echo    G ... Devices
echo    H ... Audio
echo    I ... Search
echo    J ... Printer
echo    N ... None of above
echo(
%SystemRoot%\System32\choice.exe /C ABCDEFGHIJN /N /M "Your choice:"
echo(
goto Option%Errorlevel%

:Option1
echo Running diagnostics troubleshooting wizard ...
%SystemRoot%\System32\msdt.exe /id PowerDiagnostic
goto FINISH

:Option2
echo Check for Internet problems ...
goto FINISH

:Option3
echo Check for performance problems ...
goto FINISH

:Option4
echo Check for file problems ...
goto FINISH

:Option5
echo Check for problem with Windows Apps ...
goto FINISH

:Option6
echo Check for Windows update problems ...
goto FINISH

:Option7
echo Check for device problems ...
goto FINISH

:Option8
echo Check for audio problems ...
goto FINISH

:Option9
echo Check for search problems ...
goto FINISH

:Option10
echo Check for printer problems ...
goto FINISH

:Option11
echo Sorry, this tool cannot help you.

:FINISH
echo(
pause

:END
cls
echo --------------------- Computer Optimization and Fixer %ToolVersion% ---------------------
echo(
echo Thank you for using Computer Optimization and Fixer %ToolVersion%
echo(
endlocal
pause

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • chkdsk /?
  • choice /?
  • cleanmgr /?
  • cls /?
  • defrag /? ... console version of dfrgui.exe
  • dism /?
  • echo /?
  • endlocal /?
  • exit /?
  • goto /?
  • if /?
  • pause /?
  • set /?
  • setlocal /?
  • sfc /?

Please read also:

Note: The execution of disk defragmenter is not necessary on SSD hard disks. So the batch file should check of which type each local drive is before running disk defragmenter on a drive.

PS: The names of the executables used for basic optimizations can be read by clicking with secondary (usually right) mouse button on an executable like sfc.exe, clicking in opened context menu with primary (usually left) mouse button on last menu item Properties, selecting the tab Details and looking on item File description.

Mofi
  • 46,139
  • 17
  • 80
  • 143