We use cookies to provide you with a better experience. If you continue to use this site, we'll assume you're happy with this. Alternatively, click here to find out how to manage these cookies

hide cookie message
Contact Forum Editor

Send an email to our Forum Editor:


PLEASE NOTE: Your name is used only to let the Forum Editor know who sent the message. Both your name and email address will not be used for any other purpose.

Tech Helproom


It's free to register, to post a question or to start / join a discussion


 

How to create an choice option in an batch file


raziel08

Likes # 0

I am trying to create an batch file which can be used to remotely defragment an machine via the psexec tool.

This is to be used in my technical role at work.

I have the first part of the script running fine - currently this asks for the name of the machine and then analysis the machine to see how fragmented the machine is.

However i am currently struggling on the next part which is the choice command; i want to be given the choice to defrag the machine or exit

so far i have this which i have written from help on the internet.

@echo off cls

start echo echo 1.do you want to defrag this machine?

echo 2.cancel

set/p choice = "1. Defrag or 2. Cancel"

if not '%choice%' == set choice=%choice~0,1%

if'%choice%'=='1'goto :Defrag

if'%choice%'=='2'goto :cancel

echo "%choice%"is not a valid command please try again

echo

goto start

:Defrag

psexec \%machine% defrag c: -f

got to end

:cancel

exit

got to end

:end

However it fails to run and opens up an secondary cmd shell

Can anyone help with this?

Like this post
Terry Brown

Likes # 0

What I tink you require is the DOS Qbasic instructions as was available on Versions win 3.1 to Win 98.

See the link for further help

link text

Terry

Like this post
raziel08

Likes # 0

Thanks for the help - will have to try this tomorrow to see if it works.

At the moment when i run the batch file the cmd screen shows

echo off Do you want to defrag this machine? 1. Defrag or 2. Cancel [1,2]? - at this point i enter 2 and it shows press any key to continue - so i press enter

press enter to continue - again i press enter

press enter to continue - now it closes

(still opens up another command prompt)

so below is my batch file so far (minus the first working part)

@echo off

cls

start echo echo Do you want to defrag this machine?

choice /C 12 /M "1. Defrag or 2. Cancel"

IF ERRORLEVEL 2 goto end

IF ERRORLEVEL 1 goto defrag

echo "%choice%"is not a valid command please try again

echo

goto start

:Defrag

psexec \%machine% defrag c: -f

got to end

:cancel

exit

got to end

:end pause

so have i got this right so far?

is there a way to copy the machine name that i would have typed in at the beginning so that i don't have to re-type it in later?

Like this post
Eric10

Likes # 0

"start echo echo Do you want to defrag this machine?"

This is the line that is causing your problems.

START should be a label as in :Start but by omitting the colon it is being interpretted as the START command which opens a new command prompt.

Also I think that by using echo twice you are trying to create a blank line but to do that you should use "echo." with the full stop after it and it should be on a separate line. As you have used it, the first echo just prints out everything following it on that line including the second echo which just becomes part of the text.

"is there a way to copy the machine name that i would have typed in at the beginning so that i don't have to re-type it in later?"

At what point are typing in the machine name? If you are using it as an argument when starting the batch program then it is stored in a variable called %1.

Like this post
raziel08

Likes # 0

Ok thanks for the help thus far. After a bit of re-working i have almost got an working script.

@echo off

title Defragmentation tool

echo This tool is intended to defragment an machine remotely over the network

echo "Enter the machine name" set /p machine= psexec \%machine% defrag c: -a

ECHO Do you want to defrag this volume? : ECHO. ECHO (1) Defrag (2) Cancel ECHO. Set /p Version_VAR=Version (Select 2 to Cancel) :

SET Choice_VAR=Invalid

IF %VersionVar% == 1 SET ChoiceVar=Defrag IF %VersionVar% == 2 SET ChoiceVar=Cancel

IF %Choice_Var% == Invalid GOTO InvalidInput IF %Choice_Var% == 1 GOTO Defrag IF %Choice_Var% == 2 GOTO Cancel

:Defrag psexec \%machine% defrag c: -f

ECHO Machine successfully defragmented PAUSE EXIT

:Cancel EXIT

However there are two issues - if i press 1 it defragments the machine fine. But if i press 2 it still stll does this. If i move the :Cancel option above the defrag option the cancel option works but then the defrag option doesn't.

Like this post
raziel08

Likes # 0

This new forums is corrupting the layout of my text

ECHO Do you want to defrag this volume? : ECHO.

ECHO (1) Defrag (2) Cancel

ECHO. Set /p Version_VAR=Version (Select 2 to Cancel) :

Hopefully it will now show on the seperate lines this time

Like this post
Eric10

Likes # 0

I don't understand all of your code in respect of setting the variables so I can't comment on these. However I do know that when using CHOICE you must test the errorlevels in reverse order so with choices of 1 and 2 you must test the condition for 2 before you test for 1. Reversing the order of these will probably let you "Cancel" correctly.

Like this post
Eric10

Likes # 0

Following my last reply I have been looking at your code again and I think that your problems are compounded by you having removed the GOTOs at the end of the IF tests.

When CHOICE is initialised each of the errorlevels take a value of FALSE. When you press either of your designated keys (1 or 2 in your case) the corresponding errorlevel is set to TRUE but because of the way CHOICE works all errorlevels lower than the one you choose are also set to TRUE. This is why you have to test in reverse order from the highest down to the lowest. This is also why you need to jump out of the CHOICE routine at the first TRUE result so that subsequent errorlevels are not checked. Remember, all lower errorlevels will also be TRUE and will be acted upon if tested. The best way to leave CHOICE is with a GOTO.

You should keep it simple.

If errorlevel 2 goto Cancel

If errorlevel 1 goto Defrag

In the code following the Cancel and Defrag labels you can set any variables you like over as many lines as may be needed.

Strictly speaking you don't need a GOTO at the end of the errorlevel 1 test since it will always be the last test made but the Defrag code would need to follow straight on and I don't want to complicate this explanation any more than it already is.

Like this post

Reply to this topic

This thread has been locked.



IDG UK Sites

Best January sales 2015 UK tech deals LIVE: Best New Year bargains and savings on phones, tablets,...

IDG UK Sites

Chromebooks: ready for the prime time (but not for everybody)

IDG UK Sites

Best Photoshop Tutorials 2014: 10 inspiring step-by-step guides to creating amazing art,...

IDG UK Sites

Complete guide to iPhone and iPad settings: Get to know iOS 8 Settings UPDATED