4

I'm trying to make a batch file that executes a simple command:

shutdown -h

I've created a shutdown.bat file with that exact line in it, yet when I run it, all it does is spam the command prompt like crazy.

I've looked at batch file sites, @echo off seems popular, but doesn't that just hide output while the commands are executed just the same?

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
ryanscottmurphy
  • 398
  • 1
  • 2
  • 14

4 Answers4

11

It turns out that adding a change directory command to the root of the drive fixes the problem. The final text that I ended up using in the .bat file was:

cd c:\
shutdown /h
ryanscottmurphy
  • 398
  • 1
  • 2
  • 14
4

I believe I am very late for this but just in case someone else comes across this. The issue was indeed with the name of the bat file. You were calling shutdown -h and the bat file was called shutdown.bat hence the bat file was calling itself causing the loop of the command prompts. To fix this you either rename the bat file or specify the directory where shutdown is located.

Adam Michalik
  • 9,678
  • 13
  • 71
  • 102
LJ Neveral
  • 41
  • 1
0

Would love to say I figured this out but I simply googled it. The code you need is

%windir%System32rundll32.exe powrprof.dll,SetSuspendState

If you go to start -> run and then type this in it should work. So if you have hibernate enabled in the poweroptions this should also work in a batch file.

Hope this helped you

Edit: P.s. click the white little V under the arrows (left of this answer) to accept the answer ;)

Teun Pronk
  • 1,367
  • 12
  • 24
  • I will definitely try that, but what I'm more interested in is why it spams the command window instead of actually executing the command. This isn't the only batch file I'm writing that behaves this way. – ryanscottmurphy Nov 02 '11 at 09:11
  • Can you write down the code you have used? As in the full code of the batch file – Teun Pronk Nov 02 '11 at 09:13
  • its a batch file called shutdown.bat it contains only one line: shutdown -h this exact command when run in command line instantly puts the computer into hibernation – ryanscottmurphy Nov 02 '11 at 09:16
  • im not totally into batch files but if you put it in like that I think he will just recognize it as a string. He thinks its a text and since you dont pause anything he will just keep repeating itself and writing down the text you just put in there. To try this out just go to start --> run, type in CMD write down your command there and see how it will respond. If it doesnt go into hibernate it will never do it in a batch file either. – Teun Pronk Nov 02 '11 at 09:25
  • Okay I found it. its not shutdown -h It should be shutdown /h If you go to CMD and type shutdownhelp it will show all commands there are for shutdown. Good enough mate? ;) Accept the answer? :P – Teun Pronk Nov 02 '11 at 09:31
  • But dooooood, it still doesn't work in my batch file... Do you have some text that you used for a working batch file that runs the shutdown /h command without just spamming it and doing nothing? – ryanscottmurphy Nov 02 '11 at 10:30
  • Can you go to start -> run type in CMD Once in CMD type in shutdown /h and tell me what it says – Teun Pronk Nov 02 '11 at 10:39
  • When you run that command from command line it hibernates the computer... When run from a .bat file it just spams the command without having any effect. – ryanscottmurphy Nov 05 '11 at 01:04
-3

For shutdown:

c:\windows\system32\shutdown -s -f -t 00

(or do ...shutdown -p -f).

E.g.: set the time -t 1000 and save and run it.

To abort just c:\windows\system32\shutdown -a in different batch file.

Very important point to note: if you locate this batch file in your startup then it will execute the s/h/r immediately. E.g. you create a logoff batch file and you locate it in startup it will logoff the pc within the given time/immediately. However, when you hold shift when logging then it will abort the logoff batch file otherwise you pc will logoff again and again. You don't have to do this I am not sure if it works on every PC.

Create a user and try it there in case you could not log in. good luck

For restart:

c:\windows\system32\shutdown -r -t 00

For hibernate:

c:\windows\system32\shutdown /h

Reference: https://www.instructables.com/id/Shutdown-restart-or-hibernate-your-computer-on-a/

dbc
  • 104,963
  • 20
  • 228
  • 340