Solved

How To Automate a DiskCleanup Script Using a Batch Script

Posted on 2011-02-24
13
729 Views
Last Modified: 2012-08-13
Hi,

I am Trying to Create an batch Script to do a Disk Cleanup then a Disk Defrag. I have got the Defrag part working ok but I am having problems trying to automate the diskcleanup script.

I want the script to be able to have no user input as it will be running over night when nobody is around to be selecting things. Is there a way of running the SageSet:n Command so that it memorises the input I give it so that if I run the SageRun:n Command and n = the same number it will run the Disk cleanup using the settings I predifined? Preferably I would like to do the Settings once and not have to keep on doing SageSet.

Matt
0
Comment
Question by:Contigo1
  • 8
  • 5
13 Comments
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34973455
It seems that you already found the answer out just by yourself.

Sageset just saves the config you used in the register.
So if you use Sagerun you'll be using ever that same config.

Just run firstly one time the cleanmgr /sageset:n
Then choose the directories you want to be cleaned and then click on "clean up system files".
It'll then pop up again the same config screen.
Reselect the options you have selected earlier and then click on OK.

Now if you run "cleanmgr /sagerun:n" with 'n' being the same number, it'll do the job for you all by itself.(still presenting a little UI wich requests no interaction at all)
0
 
LVL 1

Author Comment

by:Contigo1
ID: 34978541
Hi,

Thanks for the help with this. Could you help me with another problem now?

I have got the Defrag Part working and the Disk Cleanup Part working but I tried to put an If Error level Command in so at the end of the the Disk Cleanup it will only procced onto the Defrag part if it has been succesful.

However It finishes the Disk Defrag and then just closes down the CMD screen and stops

The Script I have Created is below


@Echo Off
Title Disk Maintenance
Color 07

:: Disk Maintenance ::
:: Created by Matt Spence ::
:: 24/02/2011 ::
:: Runs Disk Cleanup then Disk Defrag ::

:Start
CLS
Echo Starting Disk Maintenance Please Wait .....
ping 1.1.1.1 -n 3 -w 1000 >NUL
Goto :Change

:Change
CLS
Echo Changing Directory
CD /
CD C:\Windows\System32
Goto :Clean

:Clean
CLS
:: Needs the SageSet:n and SageRun:n Commands ::
:: Need To Config SageSet:55 ::
Echo Start Disk Cleanup
CleanMgr /SageRun:55
If Error Level 1 (Goto :Defrag) Else (Goto :Error)

:Error
ClS
Echo An Error has Occured please Click Enter to End
Exit

:Defrag
:: Defrags the HardDrive ::
:: Need to Change the /C to Drive you want Defraging ::
CLS
Echo Disk Cleanup Finished
ping 1.1.1.1 -n 3 -w 1000 >NUL
Echo Starting Defrag
Defrag /C /U /V
If Error Level 1 (Goto :End) Else (Goto :Error1)

:Error1
CLS
Echo And Error has occured please Click Enter to end
Pause
Exit

:End
CLS
Echo Defrag Completed Click Enter to Close
Pause
Exit
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34979261
Let me see if i got it:

When the Defrag succeeds, you want the :End branch to be executed and when it doesn't, you want the Error1 to be executed, right?

You can do that:
Defrag /C /U /V | find /I "successful"
if error level 1 (Goto :Error1) else (goto :End)


The command find will set the errorlevel to 1 if it doesn't find the specified string.(the /I is just to make it case INsensitive)
0
 
LVL 1

Author Comment

by:Contigo1
ID: 34979848
I am using the If Error level command 2 times.

The first time If the Disk cleanup has finished succesfuly it will goto :Defrag and if it is unsuccesful it will goto :error

The 2nd time If the DiskDefrag was Succesful it will go to :End and if it was unsuccesful it will goto :Error1

If i use Find /I will it still goto the relevant error page?
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34979967
If you use it in the DiskDefrag it will as it has a return string.
But cleanmgr hasn't.
It's a complete UI.
But as it cannot fail unless the cleanmgr file isn't there.
It maybe is not necessary to test.

But i do believe that if you run the script that you have right now, it will execute cleanmgr and defrag at the same time because clean is UI.

If this happens, you can do:
start /W cleanmgr /sagerun:n

If that doesn't happen.

Also, if errorlevel doesn't work for cleanmgr and you still want a error safe batch.
You can use psexec from sysinternals.

and add in your scrip:

psexec -i cleanmgr "/sagerun:n" | find /I "error code 0"
if error level 1 (goto Error) else (goto defrag)

0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34980007
Just adding the batch modified to maintain it up to date.
It hasn't the modification i suggest about the start /W and psexec because they are just for the case that cleanmgr doesn't returns and error, thus, making error level 1 not work properly.
@Echo Off
Title Disk Maintenance
Color 07

:: Disk Maintenance ::
:: Created by Matt Spence ::
:: 24/02/2011 ::
:: Runs Disk Cleanup then Disk Defrag ::

:Start 
CLS
Echo Starting Disk Maintenance Please Wait .....
ping 1.1.1.1 -n 3 -w 1000 >NUL
Goto :Change

:Change 
CLS
Echo Changing Directory 
CD / 
CD C:\Windows\System32
Goto :Clean 

:Clean 
CLS
:: Needs the SageSet:n and SageRun:n Commands ::
:: Need To Config SageSet:55 ::
Echo Start Disk Cleanup 
CleanMgr /SageRun:55
If Error Level 1 (Goto :Defrag) Else (Goto :Error)

:Error
ClS
Echo An Error has Occured please Click Enter to End 
Exit

:Defrag 
:: Defrags the HardDrive ::
:: Need to Change the /C to Drive you want Defraging ::
CLS
Echo Disk Cleanup Finished 
ping 1.1.1.1 -n 3 -w 1000 >NUL
Echo Starting Defrag 
Defrag /C /U /V | find /I "successful"
If Error Level 1 (Goto :End) Else (Goto :Error1)

:Error1
CLS
Echo And Error has occured please Click Enter to end 
Pause 
Exit 

:End 
CLS
Echo Defrag Completed Click Enter to Close 
Pause 
Exit

Open in new window

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 1

Author Comment

by:Contigo1
ID: 34980354
I have just Commented out the IF statement at the end of the Cleanup section as it is not needed I have also opened it up with echo on and it gets to the IF statement at the end of the defrag and then closes.
0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34980459
If you comment out that if, this clause will be executed:(which has no pause statement)
:Error
ClS
Echo An Error has Occured please Click Enter to End
Exit

If the message from Error1 and Error are the same, it's wiser to let just one copy.
@Echo Off
Title Disk Maintenance
Color 07

:: Disk Maintenance ::
:: Created by Matt Spence ::
:: 24/02/2011 ::
:: Runs Disk Cleanup then Disk Defrag ::

:Start 
CLS
Echo Starting Disk Maintenance Please Wait .....
ping 1.1.1.1 -n 3 -w 1000 >NUL
Goto :Change

:Change 
CLS
Echo Changing Directory 
CD / 
CD C:\Windows\System32
Goto :Clean 

:Clean 
CLS
:: Needs the SageSet:n and SageRun:n Commands ::
:: Need To Config SageSet:55 ::
Echo Start Disk Cleanup 
CleanMgr /SageRun:55

:Defrag 
:: Defrags the HardDrive ::
:: Need to Change the /C to Drive you want Defraging ::
CLS
Echo Disk Cleanup Finished 
ping 1.1.1.1 -n 3 -w 1000 >NUL
Echo Starting Defrag 
Defrag /C /U /V | find /I "successful"
If Error Level 1 (Goto :End) Else (Goto :Error1)

:Error1
CLS
Echo And Error has occured please Click Enter to end 
Pause 
Exit 

:End 
CLS
Echo Defrag Completed Click Enter to Close 
Pause 
Exit

Open in new window

0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34980503
Remember, it's most likely that cleanup will be executed at the same time defrag will.
In order to prevent it, use the start /W clause to wait until cleanmgr finishes its job.

start /W cleanmgr /sagerun:n

Open in new window


@Echo Off
Title Disk Maintenance
Color 07

:: Disk Maintenance ::
:: Created by Matt Spence ::
:: 24/02/2011 ::
:: Runs Disk Cleanup then Disk Defrag ::

:Start 
CLS
Echo Starting Disk Maintenance Please Wait .....
ping 1.1.1.1 -n 3 -w 1000 >NUL
Goto :Change

:Change 
CLS
Echo Changing Directory 
CD / 
CD C:\Windows\System32
Goto :Clean 

:Clean 
CLS
:: Needs the SageSet:n and SageRun:n Commands ::
:: Need To Config SageSet:55 ::
Echo Start Disk Cleanup 
start /W CleanMgr /SageRun:55

:Defrag 
:: Defrags the HardDrive ::
:: Need to Change the /C to Drive you want Defraging ::
CLS
Echo Disk Cleanup Finished 
ping 1.1.1.1 -n 3 -w 1000 >NUL
Echo Starting Defrag 
Defrag /C /U /V | find /I "successful"
If Error Level 1 (Goto :End) Else (Goto :Error1)

:Error1
CLS
Echo And Error has occured please Click Enter to end 
Pause 
Exit 

:End 
CLS
Echo Defrag Completed Click Enter to Close 
Pause 
Exit

Open in new window

0
 
LVL 1

Author Comment

by:Contigo1
ID: 34980686
I have already put in the Start /W in the code and It still seems to get to the Defrag bit and fail. It seems to be around the IF statement in the Defrag section.

I
0
 
LVL 3

Accepted Solution

by:
sergiobg57 earned 250 total points
ID: 34981129
It was a little mistake of mine.
I wrote "error level" when it was "errorlevel".
@Echo Off
Title Disk Maintenance
Color 07

:: Disk Maintenance ::
:: Created by Matt Spence ::
:: 24/02/2011 ::
:: Runs Disk Cleanup then Disk Defrag ::

:Start 
CLS
Echo Starting Disk Maintenance Please Wait .....
ping 1.1.1.1 -n 3 -w 1000 >NUL
Goto :Change

:Change 
CLS
Echo Changing Directory 
CD / 
CD C:\Windows\System32
Goto :Clean 

:Clean 
CLS
:: Needs the SageSet:n and SageRun:n Commands ::
:: Need To Config SageSet:55 ::
Echo Start Disk Cleanup 
start /W CleanMgr /SageRun:55

:Defrag 
:: Defrags the HardDrive ::
:: Need to Change the /C to Drive you want Defraging ::
CLS
Echo Disk Cleanup Finished 
ping 1.1.1.1 -n 3 -w 1000 >NUL
Echo Starting Defrag 
Defrag /C /U /V | find /I "successful"
If ERRORLEVEL 1 (Goto :End) Else (Goto :Error1)

:Error1
CLS
Echo And Error has occured please Click Enter to end 
Pause 
Exit 

:End 
CLS
Echo Defrag Completed Click Enter to Close 
Pause 
Exit

Open in new window

0
 
LVL 3

Expert Comment

by:sergiobg57
ID: 34981171
Remember that windows vista and seven request administrator acc to do that.(you have to execute it as administrator selecting that option in the context menu)
0
 
LVL 1

Author Comment

by:Contigo1
ID: 34981199
I have just changed the Error level to Errorlevel and this has solved the problem cheers for the help.

0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will show how to configure a single USB drive with a separate folder for each day of the week. This will allow each of the backups to be kept separate preventing the previous day’s backup from being overwritten. The USB drive must be s…

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now