Trying to create a batch file to make these tasks run faster.
Can someone please correct my errors or suggest a better way to write this script
Basically everytime I run it says "request timed out"
#echo off
color 0a
Title
:Beginning
set /p UserInput = What Would You Like To Start?
echo.
if %UserInput% == 1 goto :Windows Update
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 2 goto :Group Policy Update
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 5 goto :Favorites
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 3 goto :Tools
if not %UserInput% == "" goto :Exit
else goto :Exit
if %UserInput% == 4 goto :Printer
if not %UserInput% == "" goto :Exit
else goto :Exit
:Windows Update
start C:\Windows\system32\wuapp.exe
pause
exit
:Group Policy Update
start gpupdate.exe
pause
exit
:Favorites
move %userprofile%\favorites\*.* G:\
pause
exit
:Tools
start \\NoneOFyourBusiness
pause
exit
:Printer
start iexplore.exe http://www.Google.com
pause
exit
:Exit
set /p beginning == Return To The Start?
echo.
echo Y=Yes or N=No
if %beginning% == "Y" goto :Beginning
if not %beginning% == "N" goto :Exit 2
:Exit 2
pause
exit**'
set /p UserInput = What Would You Like To Start?
Will apply the input string to a variable named UserInputSpace
Batch is sensitive to spaces on both sides of the = in a set (but not a set /a)
if not %UserInput% == "" goto :Exit
Because you have no control over the use's input, that may contain spaces and other character to which batch is sensitive.
use
if not "%UserInput%"=="" goto :Exit
Note that the two strings must be exactly equal. IF /i ... will make the test case-insensitive.
Note also that responding simply Enter to a set /p will leave the variable unchanged, it will not "set" it to an empty string. If you want that, use
set "var="
set /p var=
Also, to detect whether var has been set, use
if defined var
if "%UserInput%"=="1" goto :Windows Update
This is a fail-to-fail scenario. The real command executed will be
if "%UserInput%"=="1" goto Windows
The remaining text after the space-separator is documentation.
Not a good idea IMHO to use the colon in a goto. It works, but there is a difference with a CALL statement. CALL :something will execute an internal subroutine (ie subroutine in this batch file) named something, whereas CALL something without the colon will call an external executable. By analogy, GOTO should also not be used with a colon. The one exception is the documented special condition, GOTO :EOF where the colon is required. GOTO :EOF means 'go to the end of the physical batch file.The labelEOFneed not (indeed **should** not) be included in the batch.CMD` knows where it has to go...
So - fix those and it should work a lot better. Not sure what the error response you have is, but I'd suggest it's some utility you'd not expected to run getting tired of waiting for input.
Try this...
color 0a
Title Testing
REM Since exit is a keyword - changed to leave
:Begining
echo choose an option
choice /c 12345
rem set /p UserInput = What Would You Like To Start?
echo.
if %errorlevel% == 1 goto WindowsUpdate
if %errorlevel% == 2 goto GroupPolicyUpdate
if %errorlevel% EQU 5 goto Favorites
if %errorlevel% EQU 3 goto Tools
if %errorlevel% EQU 4 goto Printer
:WindowsUpdate
start C:\Windows\system32\wuapp.exe
pause
goto leave
:GroupPolicyUpdate
start gpupdate.exe
pause
goto leave
:Favorites
move %userprofile%\favorites\*.* G:\
pause
goto leave
:Tools
start \\NoneOFyourBusiness
pause
goto leave
:Printer
start iexplore.exe http://www.Google.com
pause
goto leave
:leave
echo leave the script?
choice /c YN
if %errorlevel% NEQ 1 goto Begining
if %errorlevel% EQU 1 exit
** #echo off
color 0a
Title
:Beginning
set UserInput=Error
set /p UserInput=What Would You Like To Start?
echo.
if "%UserInput%"=="WU" goto Windows Update
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="GPU" goto Group Policy Update
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="Fav" goto Favorites
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="T" goto Tools
if not "%UserInput%"=="" goto Exit
if "%UserInput%"=="P" goto Printer
if not "%UserInput%"=="" goto Exit
:Windows Update
start C:\Windows\system32\wuapp.exe
goto exit
:Group Policy Update
start gpupdate.exe
goto exit
:Favorites
move %userprofile%\favorites\*.* G:\
goto exit
:Tools
start \\T
goto exit
:Printer
start iexplore.exe http://Google.com/
:Exit
set Beginning==Error
set /p Beginning==Return To The Start?
echo.
echo y=Yes or n=No
if "%Beginning%"=="y" call :Beginning
if not "%Beginning%"=="n" goto Exit 2
:Exit 2
pause
exit**
Related
I have a batch-game where there's some questions for one person in particular, I always change the file when she(the person) isn't nearby so its kind of a surprise.
The thing is I keep on displaying questions of different types but sometimes the GOTO or IF statements just simply doesn't work properly, it just follow the line or exit the file.
#ECHO OFF
setlocal EnableDelayedExpansion
ECHO.
ECHO "i just skipped all this echo for you"
pause>nul
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
:somewhere
cls
cd "-somewhere in my pc-"
start "-the file-"
cls
goto :somewhere1
:somewhere1
cls
echo.
echo "skip"
pause>NUL
goto :somewhere2
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
:correct
cls
echo.
echo "skip"
echo.
echo enter to exit
pause>nul
exit
:somewhere_else1
cls
echo.
echo bad answer
pause>nul
goto :somewhere2
Here are my problems:
When it ask's you the question
:choice
cls
set /p c=Question 1[y/n]
if /I "%c%" EQU "Y" goto :somewhere
if /I "%c%" EQU "N" goto :somewhere_else
goto :choice
it's just work properly, it wasn't working in the past but I magically fixed it with no idea what i'm doing.
but then I just wanted to make it bigger and add the next lines:
:somewhere2
cls
echo.
set /p c1=question 2
if /I "!c1!"== "option1" goto :correct
if not /I "!c1!"== "option1" goto :somewhere_else1
and now the file is executing perfectly the ":correct" part but no the ":somewhere_else1". in the case of ":somewhere_else1" its just exit the file. in the past was exiting the file the right one too but again I fixed it just writing it again.
The problem is your line if not /I "!c1!"== "option1" goto :somewhere_else1
The /I needs to be before not
if /I not "!c1!"== "option1" goto :somewhere_else1
Though I should also mention that your use of ! instead of % is unnecessary in this example.
You should have a read through this link to get some insight into how to fix these errors yourself in the future, and perhaps this link on good practice.
I've spent a few days trying to get this batch script to work, but it just does not seem to work properly. It seems to just do whatever it wants after it prompts me to set a variable and i set it.
For example, I might enter n when it says that it doesn't seem to exist, and it will just end the script like it should. But if I re-open it, and it says the same thing as before, and I enter n again, it might just jump to :DeleteCalc, as if I typed y.
Here's my script:
#echo off
:Begin
color fc
title My script
cls
if not exist "C:\calc.exe" (
echo calc.exe doesn't seem to exist. Attempt deletion anyway? ^(Y/N^)
set "calcnotexist="
set /p "calcnotexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal EnableDelayedExpansion
if not !calcnotexist!==!calcnotexist:^"=! set "calcnotexist="
endlocal & if "%calcnotexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto Begin
)
if /i "%calcnotexist%"=="Y" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="Yes" (
echo.
goto DeleteCalc
)
if /i "%calcnotexist%"=="N" goto End
if /i "%calcnotexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto Begin
)
:calcDoesExist
title My script
cls
echo calc.exe found. Delete? ^(Y/N^)
set "calcexist="
set /p "calcexist="
::This command checks to see if the user inputs a quotation mark. If they do, it echos that quotes cannot be inputted.
setlocal enabledelayedexpansion
if not !calcexist!==!calcexist:^"=! set "calcexist="
endlocal & if "%calcexist%"=="" (
echo ERROR - Quotes cannot be entered.
pause
goto calcDoesExist
)
if /i "%calcexist%"=="Y" goto DeleteCalc
if /i "%calcexist%"=="Yes" goto DeleteCalc
if /i "%calcexist%"=="N" goto End
if /i "%calcexist%"=="No" goto End
echo ERROR - Unrecognized input
pause
goto calcDoesExist
:DeleteCalc
cls
echo Deleting...
if not exist C:\calc.exe goto Success
del /f /q C:\calc.exe >nul 2>nul
if not exist C:\calc.exe goto Success
echo Fail!
echo.
echo calc.exe could not be deleted.
echo.
pause
goto End
:Success
echo Deleted!
echo.
echo calc.exe successfully deleted.
echo.
pause
goto End
:End
exit /b
What could I possibly be doing wrong?
Thanks
P.S. I tested this by opening CMD and running the batch script multiple times in there. (but it also doesn't work right when just double clicking it)
If you restructure your script there will be no need for the extended If blocks and therefore no necessity to EnableDelayedExpansion. Also if you use Choice you will not have to do all of the verification of responses.
Example:
#Echo Off
Title My script
Color FC
:Begin
If Exist "C:\calc.exe" GoTo calcDoesExist
Echo(calc.exe doesn't seem to exist.
Choice /M "Attempt deletion anyway"
If ErrorLevel 3 (ClS & GoTo Begin)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo Success
GoTo End
:calcDoesExist
Echo(calc.exe found.
Choice /M "Delete"
If ErrorLevel 3 (ClS & GoTo calcDoesExist)
If ErrorLevel 2 GoTo End
If ErrorLevel 1 GoTo DeleteCalc
GoTo End
:DeleteCalc
ClS
Echo(Deleting...
Del /A /F "C:\calc.exe">Nul 2>&1
If Not Exist "C:\calc.exe" GoTo Success
Echo(Fail!
Echo(
Echo(calc.exe could not be deleted.
GoTo End
:Success
Echo(
Echo(Deleted!
Echo(
Echo(calc.exe does not exist.
:End
Echo(
Echo(Exiting...
Timeout 3 /NoBreak>Nul
Exit /B
I have problems with this code, it should only perform an action when the correct input is given.
enter code here
#echo off
echo I want to play a game. Do you?
set /P INPUT=[Y/N]: %=%
If %INPUT%=="Y" goto YES
If %INPUT%=="y" goto YES
If %INPUT%=="N" goto NO
If %INPUT%=="n" goto NO
:NO
echo FOOL!
goto end
:YES
echo Good, good...
goto end
:end
PAUSE
But the input i give doesnt make a difference, also letters like "h" will trigger an reaction. It will perform the "NO" echo everytime. This is because its the first code after the choise section. Does anybody have an idea how to fix this?
You missed the quotes:
If "%INPUT%"=="Y" goto YES
If "%INPUT%"=="y" goto YES
If "%INPUT%"=="N" goto NO
If "%INPUT%"=="n" goto NO
Else you compare Y with "Y".
After
If "%INPUT%"=="n" goto NO
you need to
goto somewhere
otherwise, cmd simply continues processing, line by line, until it reaches a goto or exit or end-of-file.
:somewhere should be just before ypu do you want to play prompt.
BTW, you can use
if /i ".....
to make the comparison case-insensitive, and
"%input:~0,1%"
will return the first character of the string (so a response like "yello" will return y)
Well in order to achieve what you want the following modifications are needed for that to happen.
Here is the original code you programmed:
#echo off
echo I want to play a game. Do you?
set /P INPUT=[Y/N]: %=%
If %INPUT%=="Y" goto YES
If %INPUT%=="y" goto YES
If %INPUT%=="N" goto NO
If %INPUT%=="n" goto NO
:NO
echo FOOL!
goto end
:YES
echo Good, good...
goto end
:end
PAUSE
Here is the modifications that I made to the code to make it run more smoothly:
echo off
goto :menu
cls
:menu
cls
echo I want to play a game. Do you?
echo[
set /P INPUT=[Y/N]: %=%
If %INPUT% equ Y goto :YES
If %INPUT% equ y goto :YES
If %INPUT% equ N goto :YES
If %INPUT% equ n goto :YES
else goto :NO
:NO
cls
echo FOOL!
pause
goto end
:YES
echo Good, good...
goto end
:end
I am assuming that based on the question that you asked that you want the following inputs (Y,y,N,n) to generate a result and anything else will go to :NO section of the code? Your question wasn't really detailed.
I've been reading how to avoid spaghetti code in batch files.
In the example of what spaghetti code is, I realized that the batch file that I use when I logon almost fits this example. Could someone please help me make my batch file not have spaghetti code?
#ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
SET /P M=Please Enter Selection, then Press Enter:
IF %M%==1 GOTO StarKeePass
IF %M%==2 GOTO Backup
IF %M%==3 GOTO FireFox
IF %M%==4 GOTO :EOF
GOTO MENU
: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
:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe
GOTO MENU
In this case, if you want to use subroutines you should do this:
#ECHO OFF
CLS
:MENU
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
SET /P M=Please Enter Selection, then Press Enter:
IF %M%==1 CALL :StartKeePass
IF %M%==2 CALL :Backup
IF %M%==3 CALL :FireFox
IF %M%==4 GOTO :EOF
GOTO MENU
:StartKeePass
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 :EOF
:Backup
SET "backup=%USERPROFILE%\backup.bat"
call %backup%
GOTO :EOF
:FireFox
cd "C:\Program Files (x86)\Mozilla Firefox\"
start firefox.exe
GOTO :EOF
Note that I changed a few things. Instead of goto... goto menu, you should use call :label goto :eof/ exit /b. Besides that, you had a spelling error StartKeePass, and instead of set variable="value", it's better to use set "variable=value". This will also accept spaces in the value, but it won't add quotes to your variable
Next time you should probably post this to code review, because these things aren't really errors
If you wanted to remove gotos altogether, you can simply call the script again to keep using it. Also, look into the choice command if you're using a version of Windows later than XP, since it will eliminate the need to check if the user entered invalid input.
#echo off
cls
echo Welcome %USERNAME%
echo 1 - Start KeePass
echo 2 - Backup
echo 3 - FireFox
echo 4 - Exit
choice /C:1234 /M "Please enter your selection: " /N
:: The first option listed by choice's /C option will return an errorlevel value of 1, the second 2, and so on
if %errorlevel% equ 1 (
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%
)
:: I've converted these to one-liners simply for personal preference.
:: You can keep these the way you had them if you put them inside of parentheses like with option 1.
if %errorlevel% equ 2 call "%USERPROFILE%\backup.bat"
if %errorlevel% equ 3 start "" "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
if %errorlevel% equ 4 exit /b
:: Calls this script again, simulating a goto :MENU
:: Personally, I'd stick with a label and a goto in this instance,
:: but this is how you could do it if you don't want to use goto at all
call %0
If each choice the user can make is fairly simple (i.e. it can be simplified to one or two commands), you might want to code this way; otherwise, definitely use subroutines like Dennis suggested.
My take on organizing this, added a reset to m variable, allowed some accidental input to be dealt with, and made it all checked in one block of code.
Nothing wrong with 'Dennis van Gils' answer, figured i would show you a different approach.
#echo off
setlocal enableDelayedExpansion
:menu
set "m="
cls
echo/Welcome !username!
echo/
echo/1 - Start keepass
echo/2 - Backup
echo/3 - Firefox
echo/4 - Exit
echo/
set /p "m=Please enter selection, then press enter:"
if not defined m (
cls
echo/Error: Empty input.
pause
) else (
if "!m!" equ "1" (
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!
) else (
if "!m!" equ "2" (
set "backup=!userprofile!\backup.bat"
call !backup!
) else (
if "!m!" equ "3" (
cd "c:\program files (x86)\mozilla firefox\"
start firefox.exe
) else (
if "!m!" equ "4" (
goto :eof
) else (
cls
echo/Error: ["!m!"] not recognized.
pause
)
)
)
)
)
goto :menu
Note: echo/ is used as a habit, as echo: and echo\ i mistake for parts of a file path/url, and echo. is so painstakingly noted for its longer command time.
Also, i prefer using ! over % along with setlocal enableDelayedExpansion by pure preference, and ease of block coding.
So here's my ENTIRE code:
#echo off
cls
color fc
:Start
cls
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
set/p t=Try Again? (Y/N)
if %t%==Y goto Start
if %t%==y goto Start
if %t%==N goto Quit
if %t%==n goto Quit
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
set/p g=Choice:
if %g%== 1 goto KSPEnd
:KSPEnd
start "" "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program\KSP.exe"
cls
goto Quit
:Quit
cls
echo Goodbye
Timeout 1
But the code opens the .exe AND a .txt file with exactly the same name. I can't rename the files. So basically i'm asking how to open a specific file type.
Thanks
Instead of starting C:\....\KSP.exe, first go to the right directory, then start KSP:
cd "C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program"
KSP.exe
Ok I've got two things for you. Firstly I'll give you you desired solution.
Treat it like an operatable program
rem To start Kerbal Space Program:
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
Thats it. Really.
Secondly:
Use the Choice command
you keep on using set /p where choice would be much better.
Just to be convenient I redid you code with everything I would do. Have fun!
Code :
#echo off
cls
color fc
title -{GAMELOADER}-
:Start
echo Welcome to -{GAMELOADER}-
set/p u=Username:
if %u%==username goto Correct1
if not %u%==username goto Incorrect
set Er=Userid
goto :Crash
:Incorrect
cls
echo You Have Entered Incorrect Pass And/Or Username!
choice /c yn /m "Try Again?"
if %errorlevel%==1 goto Start
if %errorlevel%==2 goto Quit
set Er=Loop-End_of_stream
goto :Crash
:Correct1
set/p p=Password:
if %p%==password goto Open
if not %p%==password goto Incorrect
set Er=Passid
goto :Crash
:Open
cls
echo Games:
echo ------------------------
echo [1]Kerbal Space Program
echo ------------------------
echo.
Choice /c 1 /m "Game: "
if %errorlevel%==1 goto KSPEnd
set Er=Gameid
goto :Crash
:KSPEnd
set Path=C:\Program Files (x86)\Steam\steamapps\common\Kerbal Space Program;%Path%
start KSP
goto Quit
set Er=End_of_file___UNKNOWN
goto :Crash
:Quit
cls
echo Goodbye
Timeout 1
Exit
:Crash
Rem Always useful :)
Echo Program has crashed Error: %Er%
Pause
Exit
Hope that helped. Mona