Need help with batch file

  • Thread starter n o s p a m p l e a s e
  • Start date
N

n o s p a m p l e a s e

I want a batch file say "myshutdown.bat" that does the following:

* Empties my Recycle Bin
* Deletes Temp files for myself and all users. My OS is Vista Home
Premium and I have administrator's right
* Scan my computer, "C:\Program Files\Alwil Software
\Avast4\ashAvast.exe" C:\
* Does defragmentation of C:\
* shuts down the computer.

I really don't know how I can empty Recycle Bin and how I can execute
one command after the other.
 
A

Andrew McLaren

n o s p a m p l e a s e said:
I want a batch file say "myshutdown.bat" that does the following:
* Empties my Recycle Bin
* Deletes Temp files for myself and all users. My OS is Vista Home
Premium and I have administrator's right

You can do this using Vista's "Disk Cleanup" tool, cleanmgr.exe.

First, you need to create what Disk Cleansup calls a "sageset" - a list of
things you want to delete, stored in the registry and identified by a
number. You can have up to 64K different sagesets (not that you'd want to).

At a Administrator command prompt, run this command:

C:\>cleanmgr.exe /sageset:1

The Disk Cleanup Settings dialogue will appear. Select the items which you
want to delete in your batch file (eg Recycle Bin and Temporary files), and
press OK. This stores your selection of options in the registry.

To run Disk Cleanup, using the sageset you just saved, use this command:

C:\>cleanmgr.exe /sagerun:1
^^^
with a "run" instead of "set". This causes Disk Cleanup to run
automatically, no user intervention is required. This sagerun command would
be suitable for use in a batch file.
* Scan my computer, "C:\Program Files\Alwil Software
\Avast4\ashAvast.exe" C:\
* Does defragmentation of C:\

At a command prompt, run "defrag /?" to see the command line options. You'll
probably want a command like:

"defrag C: -w -f"
* shuts down the computer.

At a comand prompt, run "shutdown /? to see the command line options. You'll
probably want a comamnd like:

"shutdown /s /f"
I really don't know how I can empty Recycle Bin and how I can execute
one command after the other.

For non-GUI commands, just put them one after the other in the batch file.
For GUI commands, you can try using the START /WAIT command - this will wait
for the GUI app to complete, before going on to the next command. So, yu'll
have something like:

:===========================================
@ECHO OFF
REM Clean recycle bin and temp files
START /WAIT %SystemRoot%\SYSTEM32\CLEANMGR.EXE /Sagerun:1
REM
REM Run anti-virus
START /WAIT "C:\Program Files\Alwil Software\Avast4\ashAvast.exe" C:\
REM
REM Defrag C: drive, with full defrag and forced in nec.
%SystemRoot%\SYSTEM32\DEFRAG C: -W -F
REM
REM Now shut down, frcing apps closed if necessary
%SystemRoot%\SYSTEM32\SHUTDOWN /S /F
:END
:===========================================

Hope it helps,
 
N

n o s p a m p l e a s e

:===========================================
@ECHO OFF
REM Clean recycle bin and temp files
START /WAIT %SystemRoot%\SYSTEM32\CLEANMGR.EXE /Sagerun:1
REM
REM Run anti-virus
START /WAIT "C:\Program Files\Alwil Software\Avast4\ashAvast.exe" C:\
REM

Thanx. The above works.
REM Defrag C: drive, with full defrag and forced in nec.
%SystemRoot%\SYSTEM32\DEFRAG C: -W -F

This doesn't work.
REM
REM Now shut down, frcing apps closed if necessary
%SystemRoot%\SYSTEM32\SHUTDOWN /S /F
:END
:===========================================

This also works.

The objective of this script is to just double click and everything
happens. I tried to execute %SystemRoot%\SYSTEM32\DEFRAG C: -W -F from
commandline with a command promt open with Administrator's right. It
then works.

Is it possible to tell this command to run with administrator's right
by just double clicking the script?

Thanx/NSP
 
A

Andrew McLaren

n o s p a m p l e a s e said:
This doesn't work.
The objective of this script is to just double click and everything
happens. I tried to execute %SystemRoot%\SYSTEM32\DEFRAG C: -W -F from
commandline with a command promt open with Administrator's right. It
then works.

Unfortunately, that's contrary to the spirit of UAC. If you want an app to
run with administrative prvilege, you need to enter administrator
credentials. Otherwise, you're just returning to the Windows 95 world of
viruses, trojans and other exploits, where processes can quietly run as
administrator, without explicit consent.

A few approaches:

- leave out the defrag. By default on Vista, defrag is scheduled to run
regularly, at a low priority, and in the user context of "System". If your
machine is routinely left running, it will defrag itself.

- instead of double-clicking the batch-file, right-click and then choose
"Run as Administrator" from the context menu. You'll need to enter
credentials, but it happens immediately.

- or, to run the batch file as is, create a shortcut to the batch file and
mark the shortcut "Run as Administrator". Again, you'll still need to give
administrative consent, but it will happen right after you double-click it.

Hope it helps,
 
N

n o s p a m p l e a s e

A few approaches:

- leave out the defrag. By default on Vista, defrag is scheduled to run
regularly, at a low priority, and in the user context of "System". If your
machine is routinely left running, it will defrag itself.

- instead of double-clicking the batch-file, right-click and then choose
"Run as Administrator" from the context menu. You'll need to enter
credentials, but it happens immediately.

- or, to run the batch file as is, create a shortcut to the batch file and
mark the shortcut "Run as Administrator". Again, you'll still need to give
administrative consent, but it will happen right after you double-click it.

Hope it helps,

Thanx. I got the point.
 
N

n o s p a m p l e a s e

Thanx. I got the point.

I can confirm the script below when run as administrator works.

:===========================================
@ECHO OFF
REM Clean recycle bin and temp files
START "" /WAIT %SystemRoot%\SYSTEM32\CLEANMGR.EXE /Sagerun:1
REM
REM Run anti-virus
START "" /WAIT "C:\Program Files\Alwil Software\Avast4\ashQuick.exe" C:
\
REM
REM Defrag C: drive, with full defrag and forced in nec.
START "" /WAIT %SystemRoot%\SYSTEM32\DEFRAG C: -W -F
REM
REM Now shut down, frcing apps closed if necessary
START "" /WAIT %SystemRoot%\SYSTEM32\SHUTDOWN /S /F
:END
:===========================================
 
M

munchie

Is there any way to get windows update from command line and install
it?

Is there anyway to check if a defrag _needs_ to be done in the commad
line? I'm guessing this is either possible by a)checking some error
level or b)using findstr to parse the output of "defrag -a"

Could someone show me how to do this?

-- Munchie
 
A

Andrew McLaren

n o s p a m p l e a s e said:
Is there any way to get windows update from command line and install
it?

Sorry I don't know. wusa.exe will install an update, if you have already
downloaded it. I don't know of a command that will download and install the
update in one go.

Rather like defrag, I think the design philosophy for Windows Update in
Vista is that you should turn on Automatic Updates, and then just let the
machine run. You may, or may not, think this philosophy is a good idea ...
but that's what Microsoft is aiming for. Their goal is to make Windows
fairly self-maintaing - defragging, updating, cleaning the disk regulalry -
without the need for any user intervention, or for users to create elaboate
administrative batch files to perform these routine tasks.

Personally, I think a command line tool to download and install updates
would be very handy.
 
A

Andrew McLaren

munchie said:
Is there anyway to check if a defrag _needs_ to be done in the commad
line?
I'm guessing this is either possible by a)checking some error
level or b)using findstr to parse the output of "defrag -a"

Definitely not errorlevel. Parsing the text output is very crufty and
fragile. The better way to do this, would be in a WMI script (VBScript or
Powershell) calling the DefragAnalysis method, from the Win32_Volume class.
That's what WMI is for :)

For example, see http://msdn2.microsoft.com/en-us/library/aa389827.aspx
 

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