-
-
Save jcefoli/fb9400aafee2ac585db3 to your computer and use it in GitHub Desktop.
@ECHO OFF | |
:start | |
SET choice= | |
SET /p choice=Do something? [N]: | |
IF NOT '%choice%'=='' SET choice=%choice:~0,1% | |
IF '%choice%'=='Y' GOTO yes | |
IF '%choice%'=='y' GOTO yes | |
IF '%choice%'=='N' GOTO no | |
IF '%choice%'=='n' GOTO no | |
IF '%choice%'=='' GOTO no | |
ECHO "%choice%" is not valid | |
ECHO. | |
GOTO start | |
:no | |
ECHO Do all of the no things here! | |
PAUSE | |
EXIT | |
:yes | |
ECHO Do all of the yes things here! | |
PAUSE | |
EXIT |
Sir, is there any way that automatically takes yes if the user does nothing?
Yes you can. Using his example, on line 10, just change no to yes and that should do it.
Sir, is there any way that automatically takes yes if the user does nothing?
Yes you can. Using his example, on line 10, just change no to yes and that should do it.
Changing line 10 from no to yes will not automatically select yes if no input is provided. IF '%choice%'=='' GOTO yes
would just skip to the :yes block instead of the no block if Enter was pressed without providing input. Set /p will wait forever for input.
Is there any reason you'd need this? It's rather dated. I would recommend using PowerShell as a much more modern and capable way of scripting.
Changing line 10 from no to yes will not automatically select yes if no input is provided.
IF '%choice%'=='' GOTO yes
would just skip to the :yes block instead of the no block if Enter was pressed without providing input. Set /p will wait forever for input.
Yes, you are correct. I misread that. I was thinking he meant if they just hit "Enter" without putting in a selection, it would just take them to the "yes" block.
Minor fixes this:
IF '%choice%'=='Y' GOTO yes
IF '%choice%'=='y' GOTO yes
Replace with this:
IF /i '%choice%'=='Y' GOTO yes
It supports uppercase and lowercase text input
Minor fixes this:
IF '%choice%'=='Y' GOTO yes IF '%choice%'=='y' GOTO yes
Replace with this:
IF /i '%choice%'=='Y' GOTO yes
It supports uppercase and lowercase text input
This may depend on OS (???) because on my machine, this input is case-sensitive (meaning that you need both the upper- and lowercase options or else one will be read as an invalid input). I'm on Windows 10, running in console.
I would recommend using PowerShell as a much more modern and capable way of scripting
Could you give a hint, how one would write an equal batch file for PowerShell? Would that file use the same extension *.bat?
Thanks!
I would recommend using PowerShell as a much more modern and capable way of scripting
Could you give a hint, how one would write an equal batch file for PowerShell? Would that file use the same extension *.bat? Thanks!
PowerShell and Batch are two different things. Batch files are handled by cmd.exe and you can double click them to run a string of commands. They are very old and it's not as good as linux bash or PowerShell
PowerShell scripts have the .ps1
extension. You cannot just click them like a batch file, and you must call them from the powershell console.
There are much more elegant ways to create a menu in Powershell, but porting the code above over to parity looks like so:
$choice = ""
while ($choice.ToUpper() -ne "Y" -and $choice.ToUpper() -ne "N") {
$choice = Read-Host "Do Something (Y/N)"
if ($choice.ToUpper() -eq "Y") {
Write-Host "You chose yes"
} elseif ($choice.ToUpper() -eq "N") {
Write-Host "You chose no"
} else {
Write-Host "$choice is not valid"
}
}
This batch file handles Yes/No inputs and performs actions based on the user's choice. It uses case-insensitive checks for "Y" or "N" and defaults to "No" if no input is given. Looks solid! I was working on an essay about victimology and found a highly informative sample on https://www.topessaywriting.org/samples/victim This sample delved into the different types of victimization, the psychological effects on victims, and the societal response to victims. The paper was well-researched and presented in a clear and structured manner. It provided me with valuable insights and a better understanding of the subject, which significantly improved the quality of my essay. This is an excellent resource for anyone exploring the topic of victimology.
Sir, is there any way that automatically takes yes if the user does nothing?