I can't fix this I want to be able to write anything as a response which works with
:being
but not
:Fap
here is my code
:begin
cls
echo You wake up and realize you are never going to amount to anything, oh well, might as well get on with your worthless life
echo fap or vidya games
echo.
set /p input=
if /i "%input%"=="Fap" goto Fap
if /i "%input%"=="Vidya games" goto vidya games
if /i "%input%"=="vidya" goto vidya games
if not "%input%"=="Fap"/"vidya"/"Vidya games" goto begin
:Fap
cls
echo Since you can't get a girl you decide to fantize about the girl of your dreams so you download some watamote doujins after a mixture of crying and masterbaiting you decide to change to mood
echo vidya or sleep
echo.
set /p input=
if /i "%input%"=="vidya" goto vidya fap
if /i "%input%"=="sleep" goto sleep
if not "%input%"=="vidya"/"sleep" goto Fap
Labels in batch scripts are marked by a colon like :begin. Same approach is used when redirecting to a certain label from IF, FOR or similar statement. So, edit your script by following this example:
if /i "%input%"=="Fap" (goto :Fap
) else if /i "%input%"=="Vidya" (goto :Vidya_games
) else (goto :begin)
Instead of those many ifs, you can check, if a label exists and if yes, jump to it:
If you have several questions with different destinations, you can name your labels accordingly:
#echo off
:loop
echo fap or vidya games
set /p "label=Command: "
findstr /b /i ":Q1-%label%" "%~f0" && goto :Q1-%label%
REM previous line checks, if the label exists and if yes, jumps to it
echo no such command: %label% & goto :loop
:Q1-fap
echo reached Question1 label one.
goto :eof
:Q1-vidya
echo reached Question1 label two.
goto :eof
Some notes:
labels are single word only. Everything after the first space is ignored.
findstr /i makes it insensitive to capitalization.
/b searches for lines that start with the string.
&& means "if previous command (findstr here) was successful, then"
%~f0 is the name of your batchfile.
This goes to Q1-vidya when you enter vidya, Vidya, VIDYa, vidya Games or Vidya anything.
Related
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 am trying to just make a casual conversation using notepad as a batch file creator but whatever I search and try, it keeps saying the syntax of the command is wrong.I was just making it and went to test it and it came up. I have tried doing other stuff on different lines and then set /p ... but it never works. What am I doing wrone; I just want to continue on my just-for-fun project. Thanks:
%echo off
set /p name="Hello, what's your name?"
set /p S?1="Oh, well: hello %name%! Is that what you would like me to call you? (Y/N)"
if /i "%S?1?%" EQU "Y" goto :Yes
if /i "%S?1%" EQU "N" goto :No
goto :No answer
:Yes
echo Okay! Just making sure so I do not get on your nerves!
goto :Next1
:No
set /p name="Oh, then what shall I call you, then?"
echo Oh, alright. I will cal you %name% from now on! Sorry.
goto :Next1
:No answer
echo Sorry, but I do need an answer.
timeout 1
:No answer again
set /p S?2=So, is that what you would like me to call you? (Y/N)
if /i "%S?2%" EQU "Y" goto :Yes
if /i "%S?2%" EQU "N" goto: No
goto :No answer again
:Next1
pause
Get rid of the extra question mark on line four, (typo).
Then change your labels.
Labels are single strings without spaces, so some of them are returning to :No because it sees "No", "No Answer" and "No Answer Again" as the same.
I'd suggest you change them perhaps to :No, :Answer and :Again.
#echo off
title Music
color 0f
:main
cls
echo Welcome to you Music!
echo.
set /p var=What would you like to do:
if %var%==find goto find
if %var%==play goto play
goto main
:find
cls
set /p song=What song would you like me to check for (Case Sensitive):
if exist music\"*%song%*.mp3" goto exist
if not exist music\"*%song%*.mp3" goto notExist
:exist
cls
echo %song% is here.
echo.
pause
goto main
:notExist
cls
echo %song% is not here. Check the spelling and letter case for errors.
echo.
pause
goto main
:play
cls
set /p song=Enter the name of a song to play:
if exist music\"*%song%*.mp3" goto play2
if not exist music\"*%song%*.mp3" goto notExist
goto main
:play2
cls
set songName = [file name]
:playing
:: Will put more code later.
The purpose of this program is to search my music for a specific song and be able to play it from command line. I will later add code to control the music but for now I just want to be able to find the song and play it. I am using the '' so someone could enter "Another Brick in the Wall" and let it find "Another Brick in the Wall Part 2" The issue is when saving a variable, it can not use '' or they will be saved as part of the string.
your question isn't very clear. The following should answer what I guessed from your question and comments:
set /p "song=What song would you like me to check for (Case insensitive): "
REM if you are afraid of qoutes entered by the user, remove them:
set song=%song:"=%
<nul set /p "=Songs found: "
dir /b "music\*%song%*.mp3"|find /c /v ""
echo the names are:
dir /b "music\*%song%*.mp3"
echo executing them...
for /f "delims=" %%i in ('dir /b /s "music\*%song%*.mp3"') do (
echo ... executing %%i
REM whatever you use to play it
)
Ok, I have this batch file, and I would like to detect if the user's input includes a certain character, like ..
Here is what I have so far...
IF /I %option%== ATTRIB +h
(I would like it to detect if there is a . pressed)
I would like the .bat to detect If there is something other than Attrib +h pressed, like a ., for a file extension. So that if there was a file given, I would not ask for a file name again, because it wouldn't run properly!
Here is a part of the code, for Magoo.
#echo off
title iControl
color 80
:home
cls
echo iControl: Logan Murphy Version 1.5.7
echo Copyright 2014 Logan Murphy. All rights reserved.
GOTO jiki
:jiki
title iHelp
echo.
set /p "SSL=%USERNAME%>Execute>"
echo %SSL%"|find "." >nul
cls
if errorlevel 1 (echo notfound) else (ATTRIB +h)
rem IMPLEMENTED IT HERE!
rem Here we go
rem need to fix execution
IF /I %SSL%==attrib goto attrib
IF /I %SSL%==pause goto pause
IF /I %SSL%==backup goto backup
Edit: Ok, the answers from magoo and john really helped, but if i wanted this to detect a folder name, as well as a file name, what would i change?
Edit: Ok Magoo, testing it now.
Edit: That doesnt really work well, as I am trying to find out if the variable INCLUDES a certain character, not if the whole variable is = to a valid file/folder name, but nice try. Will mess around with it a bit... Thanks for your time Magoo, I appreciate it:)
Edit: I think I worked it out! Thanks everyone who helped me!
Here is my new code...
#echo off
title iControl
color 80
:home
cls
echo iControl: Logan Murphy Version 1.5.7
echo Copyright 2014 Logan Murphy. All rights reserved.
GOTO jiki
:jiki
title iHelp
echo.
set /p "SSL=%USERNAME%>Execute>"
%SSL%
cls
goto Seconds
:Seconds
IF /I %SSL%==attrib goto attrib
IF /I %SSL%==attrib +h goto attrib+-
IF /I %SSL%==attrib -h goto attrib+-
IF /I %SSL%==attrib +r goto attrib+-
IF /I %SSL%==attrib -r goto attrib+-
The general formula would be
echo "%var%"|find "characterorstringrequired" >nul
if errorlevel 1 (echo notfound) else (echo found)
So, for your instance,
echo "%option%"|find "i" >nul
if errorlevel 1 (echo notfound) else (ATTRIB +h)
Beyond which, I have to be as vague as your question, I'm afraid...
After seeing part-code...
echo "%SSL%"|find "." >nul
cls
if errorlevel 1 (echo notfound) else (ATTRIB +h)
I'm not sure whether cls affects errorlevel. The test should be executed directly after the command that establishes errorlevel - which is the echo %SSL... line.
echo "%SSL%"|find "." >nul
if errorlevel 1 (echo notfound) else (ATTRIB +h)
Serious damage can be done by cmd if the commands are not understood. The attrib command requires a target, so it should be
... else (attrib +h something)
to set something to hidden.
beyond that - it's not a good idea to use cmd keywords like attrib, pause and backup as labels. It can cause damage in the case of a tyop.
The benefit of sying what you want to do instead of how you want to do whatever-it-is.
To test for a file's existence, try
if exist "%SSL%" (echo "%SSL%" exists) else (echo "%SSL%" is not there)
where echo can be any legitimate (series of) command(s).
What about something like this for all special chars ?
#echo off
:_Start
set /p "FID="
set FID="%FID:"=%"
setlocal EnableDelayedExpansion
for %%I in (^| ^& ^< ^> ^^ ^( ^) ' ` ^%% ^") do set FID=!FID:%%I=!
setlocal DisableDelayedExpansion
set FID="%FID:!=%"
endlocal&set FID=%FID:~1,-1%
:_Back
set _Flag1=
for /f "tokens=1* delims=~=*;,?-+\/.##${}[]: " %%J in ('echo !FID!') do (
set FID=%%J%%K
set _Flag1=%%J
set _Flag2=%%K
)
if NOT "%_Flag2%"=="" goto :_Back
if NOT defined _Flag1 goto :_Start
endlocal&set FID=%FID%
echo %FID%
pause
The VBA command for finding a character in a string is INSTR so if you are looking for a . for example then in VBA could could use INSTR. There are two ways to do what you want:
Write a QBASIC or VBscript version of INSTR and then call that from a BATCH script
Write a batch script that does INSTR.
If you are interested in the second one look at http://www.dostips.com/forum/viewtopic.php?f=3&t=1986
It isn't as efficied as the first one would be because INSTR is pretty much a one liner in VBScript and the batch version does a character by character comparison, but if you are looking for a batch only solution that may do what you want.
I'm making a batch file with need to have two inputs as follow:
echo What's the name of your two brothers
set/p input1=
set/p input2=
Now what do I want is something like this:
if %input1==anything and if %input2%==OtherThing goto Continue
But I don't know how to do it right. Please NEED HELP
Thnks.
if "%input1%" EQU "anything" if "%input2%" EQU "OtherThing" goto :Continue
if "%input1%/%input2%" == "anything/OtherThing" goto Continue
Try like this :
#echo off
echo What's the name of your two brothers
set /p "input1=Brother 1 :"
set /p "input2=Brother 2 :"
if /i "%input1%"=="anything" (
if /i "%input2%"=="OtherThing" goto Continue
echo One brother matched
pause
exit/b)
echo No Brother Matched
pause
exit/b
:continue
echo Two Brother matched
pause
If you're trying to execute the GOTO command only if the first AND the second IF statements are true, use this:
if %input1%==anything && if %input2%==OtherThing goto continue
The && tells the program to execute the command after it only if the one before it is true.
You could also add some other features to the program, like ignoring the order that you enter the brothers. To do that, use this:
if %input1%==anything && if %input2%==OtherThing goto continue
if %input1%==OtherThing && if %input2%==anything goto continue
echo Your brothers do not match description etc...
pause
exit
You could also use case insensitive comparing like this:
if /I %input1%==anything && if /I %input2%==OtherThing goto continue
There are alot of other things possible to embelish your program. If you need more help with a certain command, open cmd.exe and type a command with the /? switch.
Update:
Sorry my first answer didn't work, but I have come up with the start of a new .bat program that should do the job. If this action is the first action of your program, copy and paste this code at the very beginning of your program:
#echo off
echo Enter the name of your first brother.
set /P input1=
cls
echo Enter the name of your second brother.
set /P input2=
if %input1%==anything goto %input1%
if not %input1%==OtherThing goto checkinput2
goto %input1%
:anything
if %input2%==OtherThing (goto continue) else cls & echo Only one brother matched. & echo. & pause & exit /b
:OtherThing
if %input2%==anything (goto continue) else cls & echo Only one brother matched. & echo. & pause & exit /b
:checkinput2
if %input2%==OtherThing cls & echo Only one brother matched. & echo. & pause & exit /b
if %input2%==anything cls & echo Only one brother matched. & echo. & pause & exit /b
cls
echo No brothers matched.
echo.
pause
exit /b
:continue
"the rest of your program here"
Wherever you see "anything" replace it with the name of one brother. Wherever you see "OtherThing" replace it with the name of the other brother. The names will be case senitive. Where you see "the rest of your program here", that is your :continue statement. Just continue your program from this line.
If this action is somewhere in the middle of your program, replace the first line, #echo off, with cls to clear whatever was on the screen from the other program commands.
I hope this works well for you.