Batch file help (Loops and Menus) - windows

I currently have this menu:
echo What would you like to do?
echo.
echo Choice
echo.
echo 1 Delete File
echo 2 Ignore File
echo.
:choice
set /P C=[1,2]?
if "%C%"=="2" goto Deleting
if "%C%"=="1" goto IgnoreFile
goto choice
Although it does not seem to work, when I select an option e.g. 2 it will not goto the :IgnoreFile section, instead it will continue the script (i.e. continue with the next command in the file, after entering my choice)
Very thrustrating, I am not sur

If I understand correctly your problem, you should make a end mark to skip a sequence of your script.
#echo off
echo What would you like to do?
echo.
echo Choice
echo.
echo 1 Delete File
echo 2 Ignore File
echo.
:choice
set /P C=[1,2]?
if "%C%"=="1" goto Deleting
if "%C%"=="2" goto IgnoreFile
goto choice
:Deleting
echo Deleting
[your code]
goto end
:IgnoreFile
echo IgnoreFile
[your code]
goto end
:end
the "goto end" at the end of each case allow you to skip the reste of the script. Don't forget to make a ":end" part at the very last line of your script.
If you don't use "goto end" and selected the "Deleting" case, the deleting part would be executed and the script would keep on going and run the "IgnoreFile" case

Related

how can I make a batch file create a batch file that can still contain percentage values

I'm trying to create a batch file that creates a batch file, but as it seems I can't get the first batch file to also send over percentage value's (e.g. %example%). How do I make the first batch file, which creates the second batch file ? But the generated file should contain % ? Also if you find any other errors in my script please correct it :)
Also while I'm here, as you can see I've put an "example, please help!" underneath the ":prep" I can't seem to figure out the ">nul" thing. No matter how many "^" I put it won't save to the last sector (which is "gnome.bat"). It will however save to the "setup.bat" but not any further. Please also help me with that!
The error is under ":Prep"
here's my full script: (not even close to being done with the script...)
#echo off
title Annoying Menu
mode 150
color a
:begin
cls
echo Welcome to "Annoying Menu"
echo This menu is made to prank your friends
echo All pranks that you make on your friends are completely your own responsibility
echo Please be aware that this menu contains what could be consideret "Virus"
echo No harm will however be done to either your, or your freinds pc
echo Please read through all the instructions before use, of the menu...
echo ---------------------------------------------------------------------------------------
echo 1. Please select a password to cancel the prank, by pressing "3" (only numbers)
echo 2. Please make sure that the setup files are ready to be transferred, by pressing "1"
echo 3. Now you are ready to send off the setup file to one of your freinds
echo ---------------------------------------------------------------------------------------
echo type "1" to create setup file
echo type "2" to prepare setup files for use
echo type "3" to download it on your own pc
echo type "4" to select password to cancel the prank (please only use numbers)
echo type "5" to test password
echo type "exit" to close the menu
echo ---------------------------------------------------------------------------------------
echo.
set /p opt= Option:
if %opt%==1 goto create
if %opt%==2 goto prep
if %opt%==3 goto UD
if %opt%==4 goto pass_select
if %opt%==5 goto testpass
if %opt%==exit goto end
cls
echo please enter a valid number to continue...
echo.
pause
goto begin
:create
break>"C:\Users\%Username%\Desktop\setup.bat"
echo.
echo setup file created, please prepare the setup file before use, by pressing "2" in the main menu
echo.
pause
goto begin
:prep
#echo #echo off> setup.bat
#echo title quick_setup>> setup.bat
#echo color a>> setup.bat
#echo mode 150>> setup.bat
#echo :begin>> setup.bat
#echo break^>"C:\Users\%Username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup\gnome.bat">> setup.bat
#echo cd C:\Users\%Username%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup>> setup.bat
(Example, please help!)
#echo #echo ping localhost -n 2 ^>nul^>^> gnome.bat>> setup.bat)
#echo #echo set /a time=%clock%-1^>^> gnome.bat>> setup.bat
#echo #echo if %clock% EQU 0 goto Timesup^>^> gnome.bat>> setup.bat
#echo #echo :Timesup^>^> gnome.bat>> setup.bat)
goto begin
(Not Done!)
:UD
(Not Done!)
:pass_select
cls
echo Please enter a password that will be used to cancel the prank (please only use numbers)
echo.
set /p pas= Set Password:
set /a pss=%pas%
cls
echo the password has now been set to %pss%
echo to enter a new password simply select "3" again and choose a new password
echo.
pause
goto begin
:testpass
cls
echo we are now ready to test your newly set password (if not you've set a password, please do so first)
echo.
echo type "1" to start the test
echo type "2" to set a password/new password
echo type "exit" to go back to the main menu
echo.
set /p lll= Option:
if %lll%==1 goto test
if %lll%==2 goto pass_select
if %lll%==exit goto begin
cls
echo please enter a valid number to continue...
echo.
pause
goto testpass
:test
cls
echo please enter password to continue...
echo type "exit" to go back to main menu
echo.
set /p tes= Password:
if %tes%==%pss% goto completetest
if %tes%==exit goto begin
cls
echo either you typed the wrong password or it didn't work, please try to set a new password if the same thing happens twice...
echo.
pause
goto test
:completetest
cls
echo The test was successful!
echo you're password works as intended
echo.
pause
goto begin
:end
In order to have percentages in your second script you need to put %%. Then the you will have the % in the output script:
#echo cd C:\Users\%%Username%%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup>>setup.bat
Escaping% with another % is a documented feature as described here in chapter Escape Percent : https://ss64.com/nt/syntax-esc.html
Here's your prep section rewritten as required.
:prep
( Echo #Title quick_setup
Echo #Color A
Echo #Mode 125
Echo :begin
Echo #CD /D "%%AppData%%\Microsoft\Windows\Start Menu\Programs\Startup"
Echo #CD.^>"gnome.bat"
Echo ( #Echo #Timeout 1 /NoBreak^^^>NUL
Echo #Echo #Set /A time=clock-1
Echo #Echo #If %%%%clock%%%% EQU 0 GoTo timesup
Echo #Echo :timesup
Echo ^)^>^>"gnome.bat"
)>>"%UserProfile%\Desktop\setup.bat"
GoTo begin
Please note however, that I have written the content as you had, (other than using Timeout to replace the very hacky Ping command for your 1 second delay and my replacement of Break for CD. in creating the empty file). You will therefore need to account for the fact that your gnome.bat code uses a variable %clock% which has not been pre-defined anywhere in that code, and you need to be very sure that you're wanting to modify the content of the system variable %TIME%.

Windows Batch Use of If and If not commands

I'm working on a child batch file for a text-based game parent I'm tinkering with. I have to use an if not statement per my professor's instructions. I want to make it to where if the user chooses anything besides "Y" after choice, the program exits. I tried using 'if not exit' and 'if not y==y exit', but the program just loops back to the parent batch file no matter the input after choice.
How can I guarantee that my if not exit statement always closes the program? Note: Program is run from desktop
:: Child Batch File
#echo off
Title Halloween......
color 06
echo set speech = wscript.Createobject("SAPI.spvoice") >> "temp.vbs"
set text=You cannot kill the boogie Man!
echo speech.speak "%text%" >> "temp.vbs"
start temp.vbs
pause
del temp.vbs
Echo Another Round??
Pause
cls
Choice
if y==y call scarymovie.bat
if not goto exit
:exit
Parent Batch File:
#Echo Off
Title Slim Shady - Scary Movie
color 04
:start
set /a counter+=1
Echo You Have Run This Program %counter% times
Echo %counter% >> answers.txt
Echo Greetings Traveler, Scrawl Your Name Across The Screen
set /p name=
Echo %name% >> answers.Txt
Echo What's Your Favorite Scary Movie??
Pause
cls
Echo Press 1 for Halloween
Echo Press 2 for The Exorcist
Echo Press 3 for I Doesn't Afraid No Ghost
set /p movie=
Echo %movie% >> answers.txt
if %movie%==1 call Halloween.bat
if %movie%==2 call Exorcist.bat
if %movie%==3 Start www.SesameStreet.org
Pause
Cls
if y==y call scarymovie.bat
if not goto exit
This is not how it works... the thing you mean with if not is else.
The common syntax is:
if %variable%==value (
echo true
) else (
echo false
)
not is only used to negate the condition like in "Is the variable anything but not this value":
if not %variable%==value (
echo not value
)
Further in your script there is unreachable code:
if y==y call scarymovie.bat
will always evaluate to true as #JeffBlock noted in the comments. The correct way for checking the result of the choice command is to check the value of the errorlevel:
choice
Goto label%errorlevel%
:label0
echo Y
Goto nextStep
:label1
echo N
Goto nextStep
Feel free to ask questions if something is unclear :)

Batch File To Loop Menu

I'm creating a batch file that will launch when I login to my user account. I followed this tutorial to create a batch file with a menu. It works, however, if the user enters a number that is not listed, I want it to go back to the menu. How would I implement that?
Side note: I understand I could use something more flexible like Powershell, however, I prefer batch files.
Here is what I have so far:
#ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - Exit
SET /P M=Type 1,2,3 then press Enter:
IF %M%==1 GOTO StarKeePass
IF %M%==2 GOTO Backup
IF %M%==3 GOTO :EOF
:StarKeePass
SET keePass="%USERPROFILE%\KeePass\KeePass-2.30\KeePass.exe"
SET kdb="%USERPROFILE%\KeePass\PasswordDatabase\PasswordDatabase.kdbx"
echo I'll start KeePass for You
START "" %keePass% %kdb%
GOTO MENU
:Backup
SET backup="%USERPROFILE%\backup.bat"
call %backup%
GOTO MENU
To build a menu using set /P, I recommend the following changes:
after all the if queries, put a goto :MENU to catch unintended entries;
reset the selection variable by something like set "SELECT=" before the set /P command, because set /P keep the former value of the variable in case the user presses just ENTER;
put quotes "" around the expressions in the if statements to avoid syntax errors in case the variable is empty or it contains some characters that have special meanings (^ & ( ) < > |; the " character still causes problems though);
use the quoted syntax set [/P] "SELECT=anything" to avoid trouble with special characters;
in case letters are queried, add the /I switch to if to force case-insensitive comparison;
if you wish, you could put a cls command after the :MENU label to let the screen be built up every time you return to the menu;
Here is an example that demonstrates what I am talking about:
#echo off
:MENU
cls
echo --- MAIN MENU ---
echo 1 - do something
echo 2 - do something else
echo Q - quit
set "SELECT="
set /P "SELECT=Type 1, 2, or Q <Enter>: "
if "%SELECT%"=="1" GOTO :SOMEWHERE
if "%SELECT%"=="2" GOTO :SOMEWHERE_ELSE
if /I "%SELECT%"=="Q" GOTO :EOF
goto :MENU
:SOMEWHERE
rem do something here...
echo.
echo Print some text.
pause
goto :MENU
:SOMEWHERE_ELSE
rem do something else here...
echo.
echo Print some other text.
pause
goto :MENU
To avoid the above mentioned troubles with the " character, modify the set /P block as follows:
set "SELECT=""
set /P "SELECT=Type 1, 2, or Q <Enter>: "
set "SELECT=%SELECT:"=%"
if "%SELECT%"=="1" GOTO :SOMEWHERE
if "%SELECT%"=="2" GOTO :SOMEWHERE_ELSE
if /I "%SELECT%"=="Q" GOTO :EOF
goto :MENU

DOS Batch File: If else goto statement understanding?

Making a batch program and I seem to have run into a problem with if and else statements.
I am trying to make it goto another line if anything else then "list" is typed.
Can anybody tell me what I did wrong?
Code:
#echo off
:start
echo PROVOLONE-------------------------
echo ------------------------------TEAM
echo -------------PRESENTS-------------
echo The Simple Batch Program Collection
echo 2015 Provolone Team
sleep 3
echo.
echo First, let's start off by listing what programs we have.
sleep 2
goto tutorial
:tutorial
echo type in "list"
set /p list="provolone>"
if %list%== "list" goto list else goto wronglist
:wronglist
echo I said type in "list!" not %list% !
sleep 2
goto tutorial
:list
echo.
echo All SBPC Programs as listed
echo starting with program names
echo placeholder
pause
The quotes are included in the comparison, so you need them on both sides of the comparison:
if "%list%"== "list" goto list else goto wronglist

Bat file and labels

I am trying to write a bat file for a network policy that will install a program if it doesn't exist as well as several other functions. I am using GOTO statements depending on whether or not certain criterion are met. However, it seems that the labels are not firing correctly as all of them do.
I have simplified my script so as to grasp some idea of what may be happening.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
:MISSING
echo file missing
ping localhost -n 5 >NUL
Basically it checks to see that the file "test.txt" exists in folder "c:\test" which id does. So it should echo file exists to the console. However, both "file exists" and "file missing" are echoed to the console. I find that if I remove the file from the folder or simply rename it, it only echoes "file missing"
Why is it running running both labels?
Because a GOTO is just a jump in execution to a point in the script, then execution continues sequentially from that point. If you want it to stop after running 'EXISTING', then you need to do something like this. Note the extra GOTO and new label:
#ECHO OFF
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :NEXTBIT
:MISSING
echo file missing
:NEXTBIT
ping localhost -n 5 >NUL
It's worth noting though that with cmd.exe (i.e., the NT-based command shells [NT, Win2k, XP, etc]), you can do IF...ELSE blocks like this:
#ECHO OFF
IF EXIST c:\test\test.txt (
ECHO File exists
) ELSE (
ECHO File missing
)
ping localhost -n 5 >nul
...so you can eliminate your GOTOs entirely.
It's because you need to skip over the "missing" bit if it exists:
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO :MISSING
:EXISTING
echo file exists
goto :COMMON
:MISSING
echo file missing
:COMMON
ping localhost -n 5 >NUL
You may also want to keep in mind that the current cmd.exe batch language is a fair bit more powerful than that which came with MS-DOS. I would prefer this one:
#echo off
if exist c:\test\test.txt (
echo file exists
) else (
echo file missing
)
ping localhost -n 5 >nul
After you echo file exists the next command is
echo file missing
You need to do something to skip the missing case. Perhaps another goto to a :PING label?
When you're debugging it helps to keep the echo on.
Because GOTO statement moves the execution to that label. To use it in the situation like yours, you need to add another GOTO label.
#echo off
IF EXIST c:\test\test.txt (GOTO :EXISTING) ELSE GOTO MISSING
:EXISTING
echo file exists
GOTO END
:MISSING
echo file missing
GOTO END
:END
ping localhost -n 5 >NUL
#echo off
IF EXIST "c:\test\test.txt" ( :: warning double quotes
GOTO EXISTING
) ELSE ( :: this format best in batch
GOTO MISSING
) :: don't forget
:EXISTING
echo file exists
goto OTHER :: if file exist jump OTHER
:MISSING
echo file missing
:: label is not required
:OTHER
timeout /t 5 >nul
pause

Resources