Batch File Crashes From Goto Command - batch-file

I was making my own little batch file.
It will say Bad Command then goto the typing prompt again but everything I type the wrong command (if) I made cmd my batch shuts off
Code:
:ccc
set /p cmd=C:\
if %cmd%==edit goto edit
if %cmd%==Edit goto edit
if %cmd%==exit goto exit
if %cmd%==Exit goto exit
if %cmd%==Restart goto restart
if %cnd%==restart goto restart
echo Bad Command
goto ccc

You should adding quotes and take a look at this : If /I case Insensitive string comparison
#echo off
:ccc
set /p "cmd=C:\ "
if /I "%cmd%"=="edit" goto edit
if /I "%cmd%"=="exit" goto exit
if /I "%cmd%"=="restart" goto restart
echo Bad Command
goto ccc

Related

Batch file won't open a hidden .vbs file

I have a batch file which makes a .vbs file and is then supposed to open it. They are both in the same folder. I've tried using start, but it simply doesn't work for some reason. I also tried cscript, but since the script is supposed to be hidden, this doesn't work either. If I try to run the vbs file by hand, it works.
code:
#echo off
tasklist | find "wscript.exe" >nul: && goto runningalr
:reset
type nul > sound.vbs
(echo Set Sound = CreateObject("WMPlayer.OCX.7"^))>>sound.vbs
:begin
set /p loop=Do you want the music to repeat? (y/n) ...
if /i %loop%==y goto repeat
if /i %loop%==n goto begin1
goto begin
:repeat
(echo do until false)>>sound.vbs
goto begin1
:begin1
set /p damd=The music's directory? (type "exit" if no more) ...
if /i %damd%==exit goto start
( echo Sound.URL = "%damd%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000)>>sound.vbs
goto begin1
:runningalr
set /p kill=Music already playing. Do you want to stop it? (y/n) ...
if %kill%==y taskkill /f /im wscript.exe && exit
if %kill%==n goto gonnaexit
:gonnaexit
<nul set /p "=Press any key to exit ... "
pause >nul
exit
:start
if /i %loop%==y (echo loop)>>sound.vbs
start C:\Users\User\Desktop\server\sound.vbs
Why didn't start work for me and what works?
instead of having just "find wscript" try using "find %WINDIR%/System32/wscript.exe /I"
You might also need to run the script as an administrator, however this is unlikely.
you give a dedicated path with start, but you write the vbs script to the current working directory. The following line starts from that working directory:
start "" "%~dp0sound.vbs"

My batch script doesn't use if command correctly

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

What's Wrong With My Batch File

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**

Windows batch script goto error

i have been trying to write a batch script in windows 7 sp1. After i start my batch script i try to type an option into the input and then i get "goto unexpected at this time" then the command prompt closes. Please help i need this for a personal project.
#echo off
:start
echo Is this the first time you ran this program on this computer
set input=
set /p intput= "y/n"
if%input%==yes goto Yes
if%input%==no goto No
:No
pause
echo Okay skipping the installation process
cd Minecraft_Server
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:serverrestart
echo Would you like to restart the server?
set input=
set /p intput= y or n
if%input%==y
pause
goto restart
if%input%==n
pause
goto norestart
:restart
echo To restart the server press any key.
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:norestart
exit
:Yes
pause
echo I will now install the Minecraft Serevr files, please make sure you
echo have me in the "Parent Directory" of the Minecraft_Server folder
echo --If you don't know what a parent directory is GOOGLE it!--
pause
mkdir Minecraft_Server
move files.exe Minecraft_Server
cd Minecraft_Server
start files.exe
timeout /t 3
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto :serverrestart
You misspelled input in the set /p line and you should use quotes. I used /i to make the compare case insensitive and shortened to one key required. Do as you wish with that. Try this:
#echo off
:start
echo Is this the first time you ran this program on this computer
set "input="
set /p input= "y/n"
if /i "%input%"=="y" goto :Yes
if /i "%input%"=="n" goto :No
echo.Enter a valid choice
pause
goto :start

Batch opening wrong file type

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

Resources