SEARCH

The How-To Geek Forums Have Migrated to Discourse

How-To Geek Forums / Windows 7

(Solved) - How to make a variable empty/null in BATCH?

(6 posts)
  • Started 1 year ago by needshelp
  • Latest reply from needshelp
  • Topic Viewed 1123 times

needshelp
Posts: 158

greetings again, its me back needing more batch help.

I already know how to test for an empty variable by if %choice%==[%1]==[] goto whatever
But what I'd like to know is how to make the variable empty again after setting it. For example.
In this example if the user presses enter without typing anything it goes back to :start.

Ok so the user types "one" then they see the next "section"
then it goes back to the first :start "section."
But the issue is that if the user presses enter again after coming back to :start (without typing anything this time around) the cmd acts like they typed in the last thing typed in. So I'd like to stop that from happening.

---------------------------------------
@echo off

:start
cls
echo hey there.
echo you can pick one or two
set /p choice= choose either.
if %choice%==[%1]==[] goto start
if /i %choice%==one goto one
if /i %choice%==two goto two

:one
cls
echo picked one first.
pause
goto start

:two
cls
echo meh
pause
goto start
------------------------------

you've never failed me before!

Posted 1 year ago
Top
 
Enthusiast
Posts: 566

Try this:

@echo off
:start
cls
echo hey there.
echo you can pick one or two, or end to exit
set /p choice= Make a selection:
if %choice%==[%1]==[] goto start
if /i %choice%==one goto one
if /i %choice%==two goto two
if /i %choice%==end goto end
:invalid
cls
echo Invalid selection: [%choice%]
pause
set choice=
goto start
:one
cls
echo picked one first.
pause
set choice=
goto start
:two
cls
echo meh
pause
set choice=
goto start
:end

The problem with your code is that there is no error condition tested for. If the user enters "one" it goes to :one, if the user enters "two" it goes to :two. If the user enters anything else, it bypasses your checks and ends up at :one anyway, as if the user entered "one". You have to put an error check after the conditions you test for, or else the processing continues right after your tests.

The "set choice=" clears the variable so the prior choice isn't there if the user just presses ENTER when asked to make a choice.

I also added a test for if the user entered "end", to exit the batch file process.

Accepted Answer · Posted 1 year ago
Top
 
needshelp
Posts: 158

My aoplogise for exempting that portion but I felt it was not nessecary for sake of my example. Even with error checking in, it still has this issue.
---------------------------------------
@echo off

:start
cls
echo hey there.
echo you can pick one or two
set /p choice= choose either.
if %choice%==[%1]==[] goto start
if /i %choice%==one goto one
if /i %choice%==two goto two
goto fail

:fail
cls
echo "%choice%" is not an option
pause
goto start

:one
cls
echo picked one first.
pause
goto start

:two
cls
echo meh
pause
goto start
------------------------------

Posted 1 year ago
Top
 
needshelp
Posts: 158

Here's what it looks like.

Posted 1 year ago
Top
 
Enthusiast
Posts: 566

Read through my code again and note where I have placed "set choice=". This statement clears the value of "choice" before returning to the top of the batch file.

Posted 1 year ago
Top
 
needshelp
Posts: 158

Oh my goodness, I feel like such the fool for missing that. Thank you for helping me fix that.

Posted 1 year ago
Top
 



Topic Closed

This topic has been closed to new replies.