Defrag/shutdown batch file

O

Oni

I've scheduled a batch file to run defrag and then shutdown the system but
the shutdown command seems to be executed immedialty after the defrag
command. This results in defrag never finishing. can anyone shed some light
on this.

Batch file contains the following:

start /WAIT defrag c: -f
shutdown -s

Thanks in advance
 
T

Thee Chicago Wolf (MVP)

I've scheduled a batch file to run defrag and then shutdown the system but
the shutdown command seems to be executed immedialty after the defrag
command. This results in defrag never finishing. can anyone shed some light
on this.

Batch file contains the following:

start /WAIT defrag c: -f
shutdown -s

Thanks in advance

That's the nature of a batch file. Why not just created some scheduled
tasks that do this instead?

- Thee Chicago Wolf (MVP)
 
P

Pegasus [MVP]

Oni said:
I've scheduled a batch file to run defrag and then shutdown the system but
the shutdown command seems to be executed immedialty after the defrag
command. This results in defrag never finishing. can anyone shed some
light
on this.

Batch file contains the following:

start /WAIT defrag c: -f
shutdown -s

Thanks in advance

The behaviour you observe is probably self-inflicted. Try this instead:
@echo off
defrag c: -f
shutdown -s

Or this:
@echo off
defrag c: -f
:Loop
ping localhost -n 60 > nul
tasklist | find /i "defrag" > nul && goto Loop
shutdown -s
 
O

Oni

Pegasus said:
The behaviour you observe is probably self-inflicted. Try this instead:
@echo off
defrag c: -f
shutdown -s

Or this:
@echo off
defrag c: -f
:Loop
ping localhost -n 60 > nul
tasklist | find /i "defrag" > nul && goto Loop
shutdown -s
Thanks for helping out, both solutions worked like a charm.

Would you care to elaborate why my original batch wouldn't work?
QUOTE<The behaviour you observe is probably self-inflicted.>
 
P

Pegasus [MVP]

Oni said:
Thanks for helping out, both solutions worked like a charm.

Would you care to elaborate why my original batch wouldn't work?
QUOTE<The behaviour you observe is probably self-inflicted.>

Some executable files, when invoked in a batch file, will return control to
the batch file immediately. Write.exe is one of them. Others will cause the
batchfile to pause until they have done their job. Notepad.exe belongs to
this class.

You can cause a batch to resume its work immediately if you launch the
second type of program in its own shell. Here is how it's done:
start /b "My Notepad" notepad.exe

Your defrag program belongs to the second class. It would have paused the
batch file until it had done its job. Since you used the "start" command,
you forced it to hand control back to the batch file immediately - which was
the opposite of what you wanted to achieve.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top