Needhelp to add a line in a bat file

March 18, 2020 at 18:41:10
Specs: Windows 10
I am a rank beginner with creating bat files..
I hope some experienced person can help.

I need to add a line in a this bat file to complete the ECHO "5 = Abort" to offer a Abort option:

-------------------
@echo OFF

ECHO "Choose an option .."
ECHO "1 = Logoff"
ECHO "2 = Reboot"
ECHO "3 = Hibernate"
ECHO "4 = Shutdown"
ECHO "5 = Abort"

SET /p option=Choose one option-

IF %option%==1 SHUTDOWN /l
IF %option%==2 SHUTDOWN -r -t 10
IF %option%==3 SHUTDOWN /h
IF %option%==4 SHUTDOWN /s /f /t 0

PAUSE
--------------------


See More: Needhelp to add a line in a bat file

Reply ↓  Report •

#1
March 19, 2020 at 01:12:34
If you use Notepad:
1. Move the cursor to the desired position using mouse or arrow keys.
2. Press <ENTER>

If you use COPY CON <File name> from the Command prompt:
1. Re-write the file
2. When you reach the position where you want the new line; press <ENTER>. Just as you did with every other new line.

Wind slow


Reply ↓  Report •

#2
March 19, 2020 at 11:36:28
I believe that zzB is asking what the content of the
next line should be, rather than how to use the editor.

zzB, I don't know much about batch files, but it kinda
looks to me like what you are asking for is already in the file.
If the user decides to abort, pressing any key other than 1-4
will abort. Someone else needs to confirm or refute that
because my experience with batch files is extremely limited
and decades old.

I noticed the word "choose" in there, and I think I jumped to the
conclusion that the file gives the user a way to input a choice,
but I don't remember the "set" command doing that-- I think
there is a "choose" command, which I believe you need, and
which you tricked me into thinking was there.

You definitely need someone who knows more than I know.

-- Jeff, in Minneapolis


Reply ↓  Report •

#3
March 20, 2020 at 07:42:33
Jeff,
Thanks for the reply.
It is not working so I am offering more detail.

When I select option 4 and enter and then immediately hit the A key it still shuts down after about 30 secods.
There was originally only 4 options so I was trying to add a 5th option:
ECHO "5 = Abort"
but I did not know how to add an accompanying entry to:
IF %option%==4 SHUTDOWN
So I tried a number of things to add to after the word SHUTDOWN.
But after each try insted of aborting, it wouldopen the command view and display the commamd as if I hadentered:
C:\Users\Brian2>shutdown /?

So knowing vary little about bat files I thought it was best to seek some help.
I hope this makes things a little clearer.
zzB



Reply ↓  Report •

Related Solutions

#4
March 20, 2020 at 08:51:18
I'm looking at this as someone who knows a bit about
programming in general but almost NOTHING AT ALL
about batch programming specifically. So...

In my ignorance of batch programming commands, I do
not see the input method used to get the keypress 1-4.
Does the input method require you to press the Enter key
after hitting the number key? That is reasonable. If I were
writing a program I would probably use an input method
that did not require the Enter key to be pressed, but having
to press Enter gives you a chance to correct a mistaken
keypress. That's good.

Assuming the batch file you posted originally works, and
you are trying to add the ability to abort the proceedure,
so that you do not log off, reboot, hibernate, or shut down,
then it looks to me like if you hit any key other than 1-4
followed by Enter it will abort. It should also abort if you
just hit Enter without hitting any other key first.

If you hit 1-4 followed by Enter, and THEN want to abort,
you are out of luck. That is too late.

Does that help?

-- Jeff, in Minneapolis


Reply ↓  Report •

#5
March 20, 2020 at 19:13:48
Jeff,

"hit any key other than 1-4 followed by Enter it will abort."

You are absolutely right.That is exactly how it works. THANKS.

What I was trying to do was to add some fancy steps to the bat file.
I was hoping I could set the file so I could click 5 (a new option) when it displayed:

"Choose an option
"1 = Logoff"
"2 = Reboot"
"3 = Hibernate"
"4 = Shutdown"
"5 = EXIT"
Choose one option-

and it would then display:
"this PC will shutdown in 5 seconds; press A to abort"

I had seen this text displayed in other bat files that did not offer to
Logoff, Reboot", Hibernate, Shutdown.---they only offered to shutdown or abort.

So I was really trying to combine the two!
I did not think it would be too difficult; but I was sure wrong.

Unless there is a simple way to do that, , I think I would be wise to just use what I have now, Logoff, Reboot", Hibernate, Shutdown options as you suggested and I will still be able to easily abort the process .

Thanks for you attention to my posting and your help.

zzB



Reply ↓  Report •

#6
March 20, 2020 at 19:55:05
Ah! I understand. I like to make things nice, and I like to
make things simple. That usually involves a tradeoff.

If you really want to have the selected function proceed
unless aborted within a specified time period, you will need
to add a timer. In other programming languages that is
generally done by recording the current time in a variable,
then entering a loop in which the key buffer is checked for
a keypress or mouse button press, and if neither is pressed,
then the current time is compared to the recorded time to
determine whether to continue the loop or exit and perform
the selected function.

I'm pretty sure it works the same in batch programming.

-- Jeff, in Minneapolis


Reply ↓  Report •

#7
March 20, 2020 at 20:31:34
I should add that in virtually all programming languages--
probably including batch programs-- there are special
functions to do this kind of countdown timing. They make
the programming a little bit simpler, but it is a fairly easy
thing to do even without them. You basically need to read
through the list of commands in the given language and
get an understanding of what each one does, and what it
is useful for. Then when you need a command, you will
remember that you read about it, and can look up how to
use it.

-- Jeff, in Minneapolis


Reply ↓  Report •

#8
March 23, 2020 at 01:41:32
-------------------
@echo OFF

ECHO "Choose an option .."
ECHO "1 = Logoff"
ECHO "2 = Reboot"
ECHO "3 = Hibernate"
ECHO "4 = Shutdown"
ECHO "5 = Abort"

SET /p option=Choose one option-

IF %option%==1 SHUTDOWN /l
IF %option%==2 SHUTDOWN -r -t 10
IF %option%==3 SHUTDOWN /h
IF %option%==4 SHUTDOWN /s /f /t 0
IF %option%==5 exit/b

PAUSE
--------------------

specs: https://www.userbenchmark.com/UserR...


Reply ↓  Report •

#9
March 25, 2020 at 10:40:45
hidde663,

Thanks so much. It is so easy when you know what you are doing!
Now I see it is the command Exit /? exit/b that you added to number 5 to make it work.

And also thanks for the link to your web site. Maybe I can learn a little there.

Again


Reply ↓  Report •

#10
March 30, 2020 at 05:31:54
If you press enter on the "Choose one option"-question, you'll get a nice error, and get thrown out of the batch. If the above is the only thing you do, no problem. But if other code needs to run...

Also note that the "echo" command doesn't need double quotes.

And, the command to abort a shutdown or restart, is:
shutdown.exe /A

In the opening post, it seems you mean to abort a shutdown or reboot, rather than "aborting" just a script.

"Abort" is a big of a big word, if you just want to end a script on a normal basis. Hidde's command is just the EXIT command, so that is what it is ..

message edited by Looge


Reply ↓  Report •

#11
April 1, 2020 at 13:44:32
Looge,

Thanks for the comments; now that is is working the only improvement I wanted to include is some additional text to the file.

That is, how to make changes to #5 so when it is selected a popup would display:

"This PC will shutdown in 5 seconds; press Y= Yes, N= No"

If I select Y, it would shutdown after 5 seconds from making the selection.
If I select N, it would immediately exit the task.

Hopefully that would be possible.

Thanks to all for the help.

This is the what the file includes now:
-----------------------------
@echo OFF

ECHO "Choose an option .."
ECHO "1 = Logoff"
ECHO "2 = Reboot"
ECHO "3 = Hibernate"
ECHO "4 = Shutdown"
ECHO "5 = EXIT"


SET /p option=Choose one option-

IF %option%==1 SHUTDOWN /l
IF %option%==2 SHUTDOWN -r -t 10
IF %option%==3 SHUTDOWN /h
IF %option%==4 SHUTDOWN /s /f /t 0
IF %option%==5 EXIT/b

PAUSE
---------------------------


Reply ↓  Report •

Ask Question