Menus in Batch File - windows

I don't usually create batch files as I just type what I need into the run box or the command prompt but I'm trying to make one just to let me access basic utilities in windows and check up on things (I really don't need it but I think my dad would find it helpful). I am familiar (but new) with python so if using python for these things is a better option I can do that, however I thought batch was the best way of doing something as simple as this. The problem is with my menu. I think because of my menu , it is cycling through all of the commands before doing the command selected. Any help with this will be fully appreciated, the batch script is in a code box below.
echo off
:menu
echo This is a simple cleanup and repair utility. Please select an option:
echo 1 - Check the hard disk c:\ for errors and inconsistancies.
echo 2 - Renew the IP address
echo 3 - View IP Address information
echo 4 - Check internet connection by pinging http://www.google.co.uk/
echo 5 - Start disk cleanup utility
echo 6 - ping 192.168.0.1
echo 7 - ping 192.168.1.1
echo 8 - Open notepad
choice /n /c:12345678 /M "Choose an option (1-8) "
IF ERRORLEVEL == 1 GOTO CHKDSK
IF ERRORLEVEL == 2 GOTO RENEW
IF ERRORLEVEL == 3 GOTO DISPLAYIP
IF ERRORLEVEL == 4 GOTO PINGGOOGLE
IF ERRORLEVEL == 5 GOTO CLEANMGR
IF ERRORLEVEL == 6 GOTO PING0
IF ERRORLEVEL == 7 GOTO PING1
IF ERRORLEVEL == 8 GOTO STARTNOTE
:CHKDSK
CHKDSK C:
PAUSE
goto menu
:RENEW
IPCONFIG /RENEW
PAUSE
goto menu
:DISPLAYIP
IPCONFIG /ALL
PAUSE
goto menu
:PINGGOOGLE
PING HTTP://WWW.GOOGLE.CO.UK/
PAUSE
goto menu
:CLEANMGR
CLEANMGR
PAUSE
goto menu
:PING0
PING 192.168.0.1
PAUSE
goto menu
:PING1
PING 192.168.1.1
PAUSE
goto menu
:STARTNOTE
START NOTEPAD
PAUSE
goto menu

You may use a much simpler approach if you don't test the errorlevel value at all, but just use it to assemble a goto command with multiple destinations. In order for this method to work, the labels must be changed so they include the errorlevel values.
echo off
:menu
echo This is a simple cleanup and repair utility. Please select an option:
echo 1 - Check the hard disk c:\ for errors and inconsistancies.
echo 2 - Renew the IP address
echo 3 - View IP Address information
echo 4 - Check internet connection by pinging http://www.google.co.uk/
echo 5 - Start disk cleanup utility
echo 6 - ping 192.168.0.1
echo 7 - ping 192.168.1.1
echo 8 - Open notepad
choice /n /c:12345678 /M "Choose an option (1-8) "
GOTO LABEL-%ERRORLEVEL%
:LABEL-1 CHKDSK
CHKDSK C:
PAUSE
goto menu
:LABEL-2 RENEW
IPCONFIG /RENEW
PAUSE
goto menu
:LABEL-3 DISPLAYIP
IPCONFIG /ALL
PAUSE
goto menu
:LABEL-4 PINGGOOGLE
PING HTTP://WWW.GOOGLE.CO.UK/
PAUSE
goto menu
:LABEL-5 CLEANMGR
CLEANMGR
PAUSE
goto menu
:LABEL-6 PING0
PING 192.168.0.1
PAUSE
goto menu
:LABEL-7 PING1
PING 192.168.1.1
PAUSE
goto menu
:LABEL-8 STARTNOTE
START NOTEPAD
PAUSE
goto menu

The errorlevel is a bit funny. When you compare like that, it actually checks if the number is equal OR greater than. So to make it work your current way, you need to order the tests in reverse (starting with higher errorlevels, and working down to errorlevel 0, always using a GOTO jump for each condition).
The other way to do it is use the environment variable:
if "%errorlevel%"=="4" goto :PINGGOOGLE
It's usually a good idea to put goto :menu after all the tests, just in case none of the tests pass. For example, if the user presses Ctrl-C, the errorlevel comes out as zero. In your code (after you fix the tests), it will fall through to the :CHKDSK section.

Change the ErrorLevel part to
IF %ERRORLEVEL%==1 GOTO CHKDSK
IF %ERRORLEVEL%==2 GOTO RENEW
IF %ERRORLEVEL%==3 GOTO DISPLAYIP
IF %ERRORLEVEL%==4 GOTO PINGGOOGLE
IF %ERRORLEVEL%==5 GOTO CLEANMGR
IF %ERRORLEVEL%==6 GOTO PING0
IF %ERRORLEVEL%==7 GOTO PING1
IF %ERRORLEVEL%==8 GOTO STARTNOTE
goto menu
Note that the if command comparisons are space sensitive therefore, "0 "==" 0" will not be equal.

Related

Converting CMD script to GUI [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I have a question.
I'm currently trying to convert a simple batch script that I have made to a GUI-version. I'm not a professional, but I love to tinker around and try stuff out.
Is there anyone who could help me to go in the right direction?
Here is my simple batch script:
#echo off
::BatchHasAdmin
:-------------------------------------
REM --> Check if this file has administrator rights.
IF "%PROCESSOR_ARCHITECTURE%" EQU "amd64" (
>nul 2>&1 "%SYSTEMROOT%\SysWOW64\cacls.exe" "%SYSTEMROOT%\SysWOW64\config\system"
) ELSE (
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
)
REM --> If no rights, we don't have setted the flag for it.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params= %*
echo UAC.ShellExecute "cmd.exe", "/c ""%~s0"" %params:"=""%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
:: MainCode
#echo off
title pcHealth - Check your PC's Health! - version 1.5.7-beta
cd /
color D
cls
:MENU
cls
color D
echo.
echo Thanks for downloading and using pcHealth!
echo Please be sure that you are running this Batch file in Administrator mode.
echo.
echo Made by REALSDEALS - Licensed under GNU-3 (You are free to use, but not to change or to remove this line.)
echo You are now using version 1.5.7 (Beta)
echo.
echo %DATE%, %TIME%
echo.
echo ...........................................................
echo Enter number 1 to open a menu regarding testing scripts.
echo Enter number 2 to open a menu regarding programs for testing /w downloadable redirects.
echo Enter number 3 to go to the repository of pcHealth.
echo Enter number 4 to close this batch script.
echo ...........................................................
echo.
SET /P A=Type one of the numbers from the menu above to open the desired menu and then press ENTER. Enter:
IF %A%==1 GOTO TOOLS
IF %A%==2 GOTO PROGRAMS
IF %A%==3 GOTO PCHEALTHGETVER
IF %A%==4 GOTO CLOSE
:TOOLS
cls
color 9
echo.
echo ...........................................................
echo Enter number 1 to gather generic information about the system.
echo Enter number 2 to see which CPU and GPU are in the system.
echo Enter number 3 to run a system scan for missing/corrupt files.
echo Enter number 4 to try and repair missing/corrupt files.
echo Enter number 5 to run a system scan and to start an attempt on repairing missing/corrupt files.
echo Enter number 6 to generate a battery report. (Laptop only)
echo Enter number 7 to open the GUI to Windows Updates.
echo Enter number 8 to start a short ping test.
echo Enter number 9 to start a continues ping test.
echo Enter number 10 to update system programs.
echo Enter number 11 to re-start the audio drivers of your system.
echo Enter number 12 to re-open the generated battery report file.
echo Enter number 13 to re-open the CBS.log (AKA DISM.log)
echo Enter number 14 to get your Ninite! Includes Edge, Chrome, VLC and 7Zip.
echo Enter number 15 to see your systems Windows License key.
echo Enter number 16 BIOS Password Recovery.
echo Enter number 17 to shutdown, reboot or log off from your PC/laptop.
echo Enter number 18 to open the programs menu.
echo Enter number 19 to return to the previous menu.
echo Enter number 20 to close this batch file.
echo ...........................................................
echo.
SET /P B=Type one of the numbers from the menu above to run the desired function, then press ENTER. Enter:
IF %B%==1 GOTO SYSINFO
IF %B%==2 GOTO CPUANDGPUINFO
IF %B%==3 GOTO SCAN
IF %B%==4 GOTO DISM
IF %B%==5 GOTO SCSM
IF %B%==6 GOTO BATTERY
IF %B%==7 GOTO UPDATE
IF %B%==8 GOTO SHORTPING
IF %B%==9 GOTO CONTINUESPING
IF %B%==10 GOTO SYSUPDATE
IF %B%==11 GOTO AUDIORE
IF %B%==12 GOTO BATOPEN
IF %B%==13 GOTO OPENCBSLOG
IF %B%==14 GOTO NINITE
IF %B%==15 GOTO LICENSE
IF %B%==16 GOTO BIOSPW
IF %B%==17 GOTO RESHUT
IF %B%==18 GOTO PROGRAMS
IF %B%==19 GOTO MENU
IF %B%==20 GOTO CLOSE
:PROGRAMS
cls
color B
echo.
echo ...........................................................
echo Enter number 1 to get hardware info.
echo Enter number 2 to get ADW Cleaner.
echo Enter number 3 to get DiskInfo64.
echo Enter number 4 to get DiskMark64.
echo Enter number 5 to get Prime95.
echo Enter number 6 to open the tools menu.
echo Enter number 7 to return to the previous menu.
echo Enter number 8 to close the script.
echo ...........................................................
echo.
SET /P AB=Type one of the numbers above to run the desired function. Enter:
IF %AB%==1 GOTO HARDINFODOWN
IF %AB%==2 GOTO ADWCLEANER
IF %AB%==3 GOTO DISKINFODOWN
IF %AB%==4 GOTO DISKMARKDOWN
IF %AB%==5 GOTO PRIMEDOWN
IF %AB%==6 GOTO TOOLS
IF %AB%==7 GOTO MENU
IF %AB%==8 GOTO CLOSE
:SYSUPDATE
cls
color A
winget upgrade --all
pause
echo.
SET /P LL=Enter number 1 to return to the sub-menu, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %LL%==1 GOTO TOOLS
IF %LL%==2 GOTO MENU
IF %LL%==3 GOTO CLOSE
:SYSINFO
cls
color A
systeminfo
pause
echo.
SET /P C=Enter number 1 to return to the main-menu, enter number 2 to go back to the previous sub-menu or enter number 3 to close the script. Enter:
IF %C%==1 GOTO MENU
IF %C%==2 GOTO TOOLS
IF %C%==3 GOTO CLOSE
:SCAN
cls
color A
sfc /scannow
pause
echo.
SET /P D=If the scan found any corrupt files, enter number 1 to check the .log, enter number 2 to return to the previous sub-menu, enter number 3 when you want to return to the menu or enter number 4 to close the script. Enter:
IF %D%==1 GOTO OPENCBSLOG
IF %D%==2 GOTO TOOLS
IF %D%==3 GOTO MENU
IF %D%==4 GOTO CLOSE
:CPUANDGPUINFO
cls
color A
echo.
echo Your CPU information:
echo.
wmic cpu get caption, deviceid, name, numberofcores, maxclockspeed, status
echo.
echo Your GPU information:
echo.
wmic path win32_VideoController get name
pause
echo.
SET /P E=Enter number 1 to return to the main-menu, enter number 2 to go back to the previous sub-menu or enter number 3 to close the script. Enter:
IF %E%==1 GOTO MENU
IF %E%==2 GOTO TOOLS
IF %E%==3 GOTO CLOSE
:DISM
cls
color A
DISM /online /cleanup-image /checkhealth
DISM /online /cleanup-image /scanhealth
pause
echo.
SET /P F=Enter number 1 to start an attempt on repairing, enter number 2 to return to the previous sub-menu, number 3 for the main-menu or number 4 to exit the script. Enter:
IF %F%==1 GOTO DISMRESTORE
IF %F%==2 GOTO TOOLS
IF %F%==3 GOTO MENU
IF %F%==4 GOTO CLOSE
:DISMRESTORE
cls
color A
DISM /online /cleanup-image /restorehealth
pause
echo.
SET /P G=Enter number 1 to return to the main-menu, enter number 2 to go back to the previous sub-menu or enter number 3 to close the script. Enter:
IF %G%==1 GOTO MENU
IF %G%==2 GOTO TOOLS
IF %G%==3 GOTO CLOSE
:SCSM
cls
color A
sfc /scannow
pause
echo.
SET /P H=If the scan found any corrupt files, enter number 1 to check the .log, enter number 2 to start an attempt to repair the corrupt/missing files,enter number 3 to return to the previous sub-menu, enter number 4 to return to the main-menu or enter number 5 to exit the script. Enter:
IF %H%==1 GOTO SCSMOPENLOG
IF %H%==2 GOTO CONTINUE
IF %H%==3 GOTO TOOLS
IF %H%==4 GOTO MENU
IF %H%==5 GOTO CLOSE
:SCSMOPENLOG
cls
color A
start %windir%\explorer.exe "C:\Windows\Logs\CBS\CBS.log"
pause
echo.
SET /P I=Enter number 1 to start an attempt to repair the corrupt/missing files, if any are found... Enter number 2 to return to the previous sub-menu, enter number 3 to return to the main-menu or enter number 4 to exit the script. Enter:
IF %I%==1 GOTO CONTINUE
IF %I%==2 GOTO TOOLS
IF %I%==3 GOTO MENU
IF %I%==4 GOTO CLOSE
:CONTINUE
cls
color A
DISM /online /cleanup-image /checkhealth
DISM /online /cleanup-image /scanhealth
pause
DISM /online /cleanup-image /restorehealth
pause
echo.
SET /P J=Enter number 1 to return to the previous sub-menu, enter number 2 to return to the main-menu or enter number 3 to exit. Enter:
IF %J%==1 GOTO TOOLS
IF %J%==2 GOTO MENU
IF %J%==3 GOTO CLOSE
:BATTERY
cls
color A
powercfg /batteryreport
pause
echo.
SET /P K=Enter number 1 to open the generated file, enter number 2 to return to the previous sub-menu, number 3 to return to the main-menu or enter number 4 to exit the script. Enter:
IF %K%==1 GOTO BATOPEN
IF %K%==2 GOTO TOOLS
IF %K%==3 GOTO MENU
IF %K%==4 GOTO CLOSE
:UPDATE
cls
color A
control update
pause
echo.
SET /P L=Enter number 1 to return to the previous sub-menu, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %L%==1 GOTO TOOLS
IF %L%==2 GOTO MENU
IF %L%==3 GOTO CLOSE
:SHORTPING
cls
color A
ping 8.8.8.8
pause
echo.
SET /P M=Enter number 1 to return to the previous sub-menu, enter number 2 to return to the main-menu, enter number 3 to start a continues ping test or enter number 4 to exit the script. Enter:
IF %M%==1 GOTO TOOLS
IF %M%==2 GOTO MENU
IF %M%==3 GOTO CONTINUESPING
IF %M%==4 GOTO CLOSE
:CONTINUESPING
cls
color A
ping 8.8.8.8 -t -l 256
pause
echo.
SET /P N=Enter number 1 to return to the previous sub-menu menu, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %N%==1 GOTO TOOLS
IF %N%==2 GOTO MENU
IF %N%==2 GOTO CLOSE
:AUDIORE
cls
color A
if "%1"=="am_admin" (powershell start -verb runas '%0' am_admin)
net stop audiosrv
net stop AudioEndPointBuilder
net start AudioEndPointBuilder
net start audiosrv
echo.
echo Your audio drivers have been reset, hope it solved your audio problem!
echo.
pause
GOTO TOOLS
:BATOPEN
cls
color A
start %windir%\explorer.exe "C:\battery-report.html"
pause
echo.
SET /P O=Enter number 1 to return to the previous sub-menu, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %O%==1 GOTO TOOLS
IF %O%==2 GOTO MENU
IF %O%==3 GOTO CLOSE
:OPENCBSLOG
cls
color A
start %windir%\explorer.exe "C:\Windows\Logs\CBS\CBS.log"
pause
echo.
SET /P P=Enter number 1 to return to the previous sub-menu, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %P%==1 GOTO TOOLS
IF %P%==2 GOTO MENU
IF %P%==3 GOTO CLOSE
:NINITE
cls
color A
start "" https://ninite.com/7zip-chrome-edge-vlc/ninite.exe
pause
echo.
SET /P Q=Enter number 1 to return to the previous sub-menu, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %Q%==1 GOTO TOOLS
IF %Q%==2 GOTO MENU
IF %Q%==3 GOTO CLOSE
:LICENSE
cls
color A
echo.
echo "Your systems license key:"
wmic path SoftwareLicensingService get OA3xOriginalProductKey
pause
color E
echo.
echo If it didn't showed a key, it is possible that this PC is using a 'illegal' key, or a key that was used for a previous installation of Windows 7/8 - then upgraded to 10/11.
echo.
echo You can also try to use a different script for the license key, you can find it in the 'Scripts' folder in this pcHealth folder!
echo.
SET /p R=If you want to return to the previous sub-menu, enter number 1. To return to the main-menu, enter number 2. To exit the script, enter the number 3. Enter:
IF %R%==1 GOTO TOOLS
IF %R%==2 GOTO MENU
IF %R%==2 GOTO CLOSE
:BIOSPW
cls
color E
echo.
echo The BIOS Password Recovery tool is a website that can be used to gather/generate a recovery code for the BIOS.
echo.
echo If you don't know how to use this function/website, then
echo I would suggest that you enter '2' on the next line to learn more.
echo.
echo The credits for this function and repository goes to the owner: #bacher09
echo.
SET /P SK=Enter number 1 to visit the website, enter number 2 to go to the repository of BIOS-PW and learn more! Enter number 3 to return to the sub-menu, enter number 4 to return to the main-menu or enter number 5 to close the script. Enter:
IF %SK%==1 start "" https://bios-pw.org && GOTO BIOSPW
IF %SK%==2 start "" https://github.com/bacher09/pwgen-for-bios && GOTO BIOSPW
IF %SK%==3 GOTO TOOLS
IF %SK%==4 GOTO MENU
IF %SK%==5 GOTO CLOSE
:RESHUT
cls
color A
echo.
SET /P S=If you want to log off from your PC/Laptop enter number 1, to restart enter number 2, to shutdown enter number 3 and to return to the previous sub-menu enter number 4 or to exit the script... enter number 5. Enter:
IF %S%==1 GOTO LOGOFF1
IF %S%==2 GOTO RESTART2
IF %S%==3 GOTO SHUTDOWN3
IF %S%==4 GOTO TOOLS
IF %S%==5 GOTO CLOSE
:LOGOFF1
cls
color A
echo.
SET /P T=Are you sure that you want to log off your PC? Enter number 1, enter number 2 to return to the previous sub-menu, enter number 3 to return to the main-menu or enter number 4 to exit the script. Enter:
IF %T%==1 GOTO LOGOFFCONFIRM1
IF %T%==2 GOTO TOOLS
IF %T%==3 GOTO MENU
IF %T%==4 GOTO CLOSE
:LOGOFFCONFIRM1
cls
color A
shutdown /l
EXIT /B
:RESTART2
cls
color A
echo.
SET /P U=Are you sure that you want to restart your PC? Enter number 1, to do so. Enter number 2 to return to the previous sub-menu, enter number 3 to return to the main-menu or enter number 4 to exit the script. Enter:
IF %U%==1 GOTO RESTARTCONFIRM2
IF %U%==2 GOTO TOOLS
IF %U%==3 GOTO MENU
IF %U%==4 GOTO CLOSE
:RESTARTCONFIRM2
cls
color A
shutdown /r
EXIT /B
:SHUTDOWN3
cls
color A
echo.
SET /P V=Are you sure that you want to shutdown your PC? Enter number 1, to continue. Enter number 2 to return to the previous sub-menu, enter number 3 to return to the main-menu or enter number 4 to exit the script. Enter:
IF %V%==1 GOTO SHUTDOWNCONFIRM3
IF %V%==2 GOTO TOOLS
IF %V%==3 GOTO MENU
IF %V%==4 GOTO CLOSE
:SHUTDOWNCONFIRM3
cls
color A
shutdown /s
EXIT /B
:PCHEALTHGETVER
cls
color A
echo.
echo Are you sure that you want to download the newest version of pcHealth?
echo.
SET /P AC=If yes, enter the number 1, if not enter number 2 to return to the previous sub-menu. Enter:
IF %AC%==1 GOTO PCHEALTHGETVERDOWNLOADLINK
IF %AC%==2 GOTO MENU
:PCHEALTHGETVERDOWNLOADLINK
cls
color A
echo.
echo Your download will start now!
echo.
start "" https://github.com/REALSDEALS/pcHealth/archive/refs/heads/main.zip
echo.
SET /P AD=To return to the main menu enter 1, to close the script enter 2. Enter:
IF %AD%==1 GOTO MENU
IF %AD%==2 GOTO CLOSE
:HARDINFODOWN
cls
color A
echo.
echo Are you sure that you want to download the newest version of Hardware Info?
echo.
SET /P AE=If yes, enter the number 1, if not enter number 2 to return to the sub-menu. Enter:
IF %AE%==1 GOTO HARDINFODOWNLOADLINK
IF %AE%==2 GOTO PROGRAMS
:HARDINFODOWNLOADLINK
cls
color A
echo.
echo Your download will start now; if not click on 'installer' on the download page!
echo.
start "" https://www.fosshub.com/HWiNFO.html?dwl=hwi_726.exe
echo.
SET /P AF=To return to the previous sub-menu enter 1, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %AF%==1 GOTO PROGRAMS
IF %AF%==2 GOTO MENU
IF %AF%==3 GOTO CLOSE
:ADWCLEANER
cls
color A
echo.
echo Are you sure that you want to download the latest version of ADW Cleaner?
echo.
SET /P AG=If yes enter the number 1 to start the download, enter number 2 to return to the previous sub-menu. Enter:
IF %AG%==1 GOTO ADWCLEANERDOWNLOADLINK
IF %AG%==2 GOTO PROGRAMS
:ADWCLEANERDOWNLOADLINK
cls
color A
echo.
echo Your download will start now!
echo.
start "" https://downloads.malwarebytes.com/file/adwcleaner
echo.
SET /P AH=To return to the previous sub-menu enter 1, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %AH%==1 GOTO TOOLS
IF %AH%==2 GOTO MENU
IF %AH%==3 GOTO CLOSE
:DISKINFODOWN
cls
color A
echo.
echo Are you sure that you want to download the latest version of Disk Info?
echo.
SET /P AI=If yes enter the number 1 to start the download, enter the number 2 to return to the previous sub-menu. Enter:
IF %AI%==1 GOTO DISKINFODOWNLOADLINK
IF %AI%==2 GOTO PROGRAMS
:DISKINFODOWNLOADLINK
cls
color A
echo.
echo Your download will start now!
start "" https://osdn.net/projects/crystaldiskinfo/downloads/77538/CrystalDiskInfo8_17_4.zip/
echo.
SET /P AJ=To return to the previous sub-menu enter 1, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %AJ%==1 GOTO TOOLS
IF %AJ%==2 GOTO MENU
IF %AJ%==3 GOTO CLOSE
:DISKMARKDOWN
cls
color A
echo.
echo Are you sure that you want to download the latest version of Disk Mark?
echo.
SET /P AK=If yes enter the number 1 to start the download, enter the number 2 to return to the previous sub-menu. Enter:
IF %AK%==1 GOTO DISKMARKDOWNLOADLINK
IF %AK%==2 GOTO PROGRAMS
:DISKMARKDOWNLOADLINK
cls
color A
echo.
echo Your download will start now!
start "" https://osdn.net/projects/crystaldiskmark/downloads/77539/CrystalDiskMark8_0_4b.zip/
echo.
SET /P AL=To return to the previous sub-menu enter 1, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %AL%==1 GOTO TOOLS
IF %AL%==2 GOTO MENU
IF %AL%==3 GOTO CLOSE
:PRIMEDOWN
cls
color A
echo.
echo Are you sure that you want to download the latest version of Prime95? Enter:
echo.
SET /P AM=If yes enter the number 1 to start the download, enter the number 2 to return to the previous sub-menu.
IF %AM%==1 GOTO PRIMEDOWNLOADLINK
IF %AM%==2 GOTO PROGRAMS
:PRIMEDOWNLOADLINK
cls
color A
echo.
echo Your download will start now!
start "" https://www.guru3d.com/files-get/prime95-download,3.html
echo.
SET /P AN=To return to the previous sub-menu enter 1, enter number 2 to return to the main-menu or enter number 3 to exit the script. Enter:
IF %AN%==1 GOTO TOOLS
IF %AN%==2 GOTO MENU
IF %AN%==3 GOTO CLOSE
:CLOSE
EXIT /B
Main goal:
I want to make a GUI version of this script.
Problems or things that I'm struggling with:
How can I make commands like batch/ps and vbs run with each other, like for example from a click in the GUI. Because it are all languages on their own, I have now made 3 seperate folders. (The .vbs script is not my own, I got it and changed it a bit)
A local database for users to run certain test, like the battery-report and save it to a local file/database that users could later acces.
A kind of version control that when the script (GUI) is launched it would fetch from GitHub to check if there is a new version available.
I'm sorry if I'm asking some dumb questions, I'm trying to teach and help myself by doing some projects for myself. So all help is welcome! The pcHealth repo may be dumb, but a few people do use it :)
Please keep in mind that I'm not a pro, I would consider myself as a complete noob on this subject.
I'm looking forward to your responses though!
~ REALSDEALS
It is possible using python, but with a batch it is not possible.

Batch File Won't Jump to Label

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
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 goto :EOF
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
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
chkdsk /?
choice /?
cleanmgr /?
cls /?
defrag /? ... console version of dfrgui.exe
dism /?
echo /?
endlocal /?
goto /?
if /?
pause /?
set /?
setlocal /?
sfc /?
Please read also:
How does the Windows Command Interpreter (CMD.EXE) parse scripts?
Where does GOTO :EOF return to?
Symbol equivalent to NEQ, LSS, GTR, etc. in Windows batch files
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
DosTips forum topic: ECHO. FAILS to give text or blank line - Instead use ECHO/
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.

make a batch script foward an IP

Hi so my coworker requested if I could make a script. I am sure what I want it to do and wrote some pseudo code in bash style now of course this not useable for him since he is on Windows. So I tried to implement in a .bat script now here is where my knowledge comes a bit short. What I need the script to do is to connect to a certain VPN-ip if that is not avaible the localsystem should foward it to another VPN so he doesn't need to worry about it. Either one of 2 should always be reachable. But they are never at the same time. This is for test tooling.
Pseudo bashcode
while true
do
From local if
10.10.1.15 avaible connect to it
else
10.168.84.47 connect to it
elseif
try to connect to 10.10.1.15 again && verify that'
else
echo 'error device over VPN unavaible'
My Attempted batch script, I am pretty sure that what I have now is not gonna work
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
:loop
set state=down
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%b"=="xunreachable." goto :endloop
if "x%%a"=="xReceived" if "x%%c"=="x1," set state=up
)
:endloop
echo.Link is !state!
ping -n 6 10.10.1.15 >nul: 2>nul:
goto :loop
endlocal
IF EXIST 10.10.1.15 (
is reacheable connect
) ELSE (
netsh interface portproxy add v4tov4 listenport=80 connectaddress=10.10.1.15 fowardaddress=10.168.84.47
)
So far as I know, you can just try to find "ttl=" to determine success/fail for a ping without worrying about all those tokens and different versions of cmd. I'm sure someone will correct me if I'm wrong.
untested
set ip=127.0.0.1
rem 2 pings waiting 900ms for a reply
ping -n 2 -w 900 %ip%|find /i "ttl=">nul || goto :fail
rem If we get here then the ping succeeded
rem do what you want here
goto :eof
:fail
rem If we get here then the ping failed.
rem do what you want here
goto :eof
The || operator basically means "if the previous command fails then do this".

Creating a shop. Won't subtract money correctly

I expect to be able to buy items in the shop and have it do the correct subtraction. In the code below, you start out with 10 gold pieces, (gp), but whenever option 2 or 4, to spend 5gp or 1gp, is entered, it takes away all 10gp. I know that it's because it doesn't go past the first if %input%== 1 but I don't know how to fix it, I have tried almost everything, including if/else statements, but I may not have been doing them properly.
:shop
cls
echo You see a middle aged man behind the counter
echo of the shop as well as a younger man sweeping the floors.
echo "Hello young travelers. Welcome, is there anything
echo I can help you find?"
:purchase
echo --------------------------------------------------------
echo %name%
echo Gold: %gp%
echo --------------------------------------------------------
echo.
echo 1) Battleaxe 10gp Stats: 1d8(S) Versatile(1d10)
echo 2) Mace 5gp Stats: 1d6(B)
echo 3) L.Crossbow 20gp Stats: 1d8(P) Range 80/320
echo 4) 5 Bolts 1gp Equip with Crossbow
echo 5) Go Back
echo.
set /p input=Enter:
if %input%== 5 goto main
if %input%== 1
if %gp% LSS 10 goto nofunds
set /a gp= %gp% - 10
goto shopcont
if %input%== 2
if %gp% LSS 5 goto nofunds
set /a gp= %gp% - 5
goto shopcont
if %input%== 3
if %gp% LSS 20 goto nofunds
set /a gp= %gp% - 20
goto shopcont
if %input%== 4
if %gp% LSS 1 goto nofunds
set /a gp= %gp% - 1
goto shopcont
goto shop
:nofunds
cls
echo You don't have enough gold to purchase that item.
pause >nul
goto shop
:shopcont
cls
echo Would you like to purchase anything else?
goto purchase
I am still new at this so examples and explanations would be wonderful!
Please do not tell me to use choice.exe instead of Set /P, unless it will fix the actual issue.
In the below example, I have used Set /P under :purchase to satisfy your ill advised stipulation to not use choice.exe, (which I used under :shopcont instead).
:shop
ClS
Echo You see a middle aged man behind the shop counter, as well as a
Echo younger man sweeping the floor.
Echo(
Echo "Welcome young travellers, is there anything I can help you with?"
:purchase
Set "input="
Set "invalid=true"
Echo(
Echo ------------------------------------------------------------------
Echo(%name%
Echo Gold: %gp%
Echo ------------------------------------------------------------------
Echo(
Echo 1. Battleaxe 10gp [Stats: 1d8(S) Versatile(1d10)]
Echo 2. Mace 5gp [Stats: 1d6(B)]
Echo 3. L.Crossbow 20gp [Stats: 1d8(P) Range 80/320]
Echo 4. 5 Bolts 1gp [Equip with Crossbow]
Echo 5. Go Back
Echo(
Set /P "input=Enter: "
For /L %%A In (1,1,5) Do If "%%~A" == "%input:"=%" Set "invalid="
If Defined invalid ClS & GoTo purchase
If %input% Equ 5 GoTo main
If %input% Equ 4 If %gp% GEq 1 Set /A gp -=1 & GoTo shopcont
If %input% Equ 3 If %gp% GEq 20 Set /A gp -=20 & GoTo shopcont
If %input% Equ 2 If %gp% GEq 5 Set /A gp -=5 & GoTo shopcont
If %input% Equ 1 If %gp% GEq 10 Set /A gp -=10 & GoTo shopcont
Echo You do not have enough gold to purchase that item.
:shopcont
"%__AppDir__%choice.exe" /M "Would you like to purchase anything else"
If "%ErrorLevel%"=="1" ClS & GoTo purchase
Please note that I have tried to replicate that which you posted in your question, this assumes that %gp% and %name% are already defined prior to this code section and that the label :main exists elsewhere in your unposted code.
You asked for examples and explanations, but those are readily available under each command's usage information and via web searches, so I will not be pointlessly including such things.
The usage of command set /P is not recommended for a simple choice menu. A simple typing mistake by user of batch file can easily result in a syntax error on further processing of the batch file detected by Windows command processor resulting in an unexpected exit of batch file execution. A user playing this batch file game by double clicking on it will not be happy on typing for example by mistake " instead of 2 and suddenly the console window is closed because cmd.exe exited batch file processing because of a serious syntax error caused by " and not good coded batch file.
See also:
How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?
Safe number comparison in Windows batch file
However, the main problem is the used syntax on all IF commands. The syntax of command IF can be seen by opening a command prompt, running if /? and reading the output help. if %input%== 1 without a command or a command block starting with ( and ending with matching ) to execute on condition is true on same line results in a syntax error on batch file execution. This can be seen on debugging the batch file.
The indentations have no meaning for cmd.exe regarding to process flow. Windows command processor is not Python. Windows command processor executes one command line respectively command block after the other independent on how many leading spaces or tabs are used to indent the command lines.
See also: How does the Windows Command Interpreter (CMD.EXE) parse scripts?
An arithmetic expression is the string after set /A evaluated by cmd.exe on execution of the batch file. The help output on running set /? explains that within an arithmetic expression it is possible to reference the value of an environment variable by writing just its name without % or ! around variable name. That has two advantages:
If the environment variable does not exist at all, Windows command processor uses value 0 for not existing environment variable. Using %NotExistingVariable% in an arithmetic expression results in a syntax error because of this string is replaced by nothing which usually results in a missing operand error on evaluation of the arithmetic expression.
Environment variables can be modified with arithmetic expressions multiple times in a command block without usage of delayed expansion.
For that reason set /a gp= %gp% - 10 is not a recommended syntax to decrement the environment variable gp by 10. Better is using set /A gp=gp - 10 and best set /A gp-=10.
The DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ explains that echo. can fail to print an empty line into console window and that echo/ or echo( is better for this task.
A minimal, complete, and verifiable example for this task is following batch file:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "gp=50"
set "name=B1u3Soul"
:Shop
cls
echo You see a middle aged man behind the counter
echo of the shop as well as a younger man sweeping the floors.
echo "Hello young travelers. Welcome, is there anything
echo I can help you find?"
:Purchase
echo --------------------------------------------------------
echo %name%
echo Gold: %gp%
echo --------------------------------------------------------
echo/
echo 1) Battleaxe 10gp Stats: 1d8(S) Versatile(1d10)
echo 2) Mace 5gp Stats: 1d6(B)
echo 3) L.Crossbow 20gp Stats: 1d8(P) Range 80/320
echo 4) 5 Bolts 1gp Equip with Crossbow
echo 5) Go Back
echo/
%SystemRoot%\System32\choice.exe /C 12345 /N /M "Enter: "
if errorlevel 5 goto Main
if errorlevel 4 set "GoldAmount=1" & goto GoldInPurse
if errorlevel 3 set "GoldAmount=20" & goto GoldInPurse
if errorlevel 2 set "GoldAmount=5" & goto GoldInPurse
set "GoldAmount=10"
:GoldInPurse
if %gp% LSS %GoldAmount% goto NoFunds
set /A gp-=GoldAmount
echo/
%SystemRoot%\System32\choice.exe /C YN /N /M "Would you like to purchase anything else [Y/N]? "
cls
if errorlevel 2 goto Main
goto Purchase
:NoFunds
echo/
echo You don't have enough gold to purchase that item.
pause >nul
goto Shop
:Main
endlocal
See also single line with multiple commands using Windows batch file for an explanation of operator & as used in this batch file.
It would be of course possible to use just choice instead of %SystemRoot%\System32\choice.exe. But the usage of full qualified file name (drive + path + file name + file extension) makes the batch file independent on environment defined outside the batch file. For this batch file it does not matter how PATH and PATHEXT is defined on starting the batch file. It depends only on environment variable SystemRoot defined by Windows and which is not modified by applications or users in general.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
cls /?
echo /?
endlocal /?
goto /?
if /?
pause /?
set /?
setlocal /?
See also:
What are the ERRORLEVEL values set by internal cmd.exe commands?
Which cmd.exe internal commands clear the ERRORLEVEL to 0 upon success?
Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Microsoft article about Using command redirection operators

I am using a Batch File to search a list of functions for an exact match, wondering if its possible to do a keyword search instead

I am using a Batch File to search a list of functions for an exact match, wondering if its possible to do a key word search, so right now i have to type "open cmd"
(variations on spacing and capital letters are accounted for)
Id like to switch it over to a system that can look for "cmd" and perform the action so "hey, open cmd please" would yield the same result as the old system
Old system:
setlocal
:: /STARTUP
set speech=start scripts\nircmd.exe speak text
cls
:begin
set TALK=TypeSomething
SET /P TALK=
set TALK=%TALK:?=%
call :%TALK: =% 2>NUL
if %errorlevel% equ 0 goto begin
exit /B 0
:unknown
echo Old function no longer supported
:opencmd
:BOSopencmd
:cmd
echo Command Prompt has now been opened in a new window, sir.
%speech% "Command Prompt has now been opened in a new window, sir."
start scripts\cmd.bat
exit /B 0
It is based of a chat bot i tried to make in middle school so the %speech% is not an important item, i can add that and the echo later. I just need a system that works like the old one if possible. The other i can have any number of functions with
:cmd
start cmd
Exit /B 0
or
:reddit
start http://www.reddit.com/
exit /B 0
at these need to be able to stack. I can transition to having scripts for each function in a separate batch files if needed. Ive tried trying findstr but it wasn't giving the desired results. Ive exhausted my knowledge on what i might be able to do but I've come up short lol, If you are having trouble understanding what i'm asking don't hesitate to let me know
I learn by taking things apart so partial code is appreciated but will not be much help until after I've figured out what does what .
Here's a sample of how you might approach it using ECHO, FINDSTR, and CALL (This is a modified example from the original per your request to be able to process multiple keywords):
#echo off
set TST_FNDFLG=FALSE
set TST_USRANS=
set /P TST_USRANS=Enter keywords:
if "%TST_USRANS%" == "" goto ENDIT
echo %TST_USRANS% | findstr /i "CMD" >NUL 2>&1
if ERRORLEVEL 1 goto TRYRED
call :DOCMD
:TRYRED
echo %TST_USRANS% | findstr /i "REDDIT" >NUL 2>&1
if ERRORLEVEL 1 goto TRYGOO
call :DORED
:TRYGOO
echo %TST_USRANS% | findstr /i "GOOGLE" >NUL 2>&1
if ERRORLEVEL 1 goto TRYEND
call :DOGOO
goto TRYEND
:DOCMD
if [%TST_FNDFLG%] == [FALSE] echo.
echo CMD was found in "%TST_USRANS%"
set TST_FNDFLG=TRUE
goto :EOF
:DORED
if [%TST_FNDFLG%] == [FALSE] echo.
echo REDDIT was found in "%TST_USRANS%"
set TST_FNDFLG=TRUE
goto :EOF
:DOGOO
if [%TST_FNDFLG%] == [FALSE] echo.
echo GOOGLE was found in "%TST_USRANS%"
set TST_FNDFLG=TRUE
goto :EOF
:TRYEND
echo.
if [%TST_FNDFLG%] == [TRUE] echo No more keywords found
if [%TST_FNDFLG%] == [FALSE] echo Did not find any known keywords
goto ENDIT
:ENDIT
echo.
set TST_USRANS=
set TST_FNDFLG=

Resources