?
Solved

How To Automate a DiskCleanup Script Using a Batch Script

Posted on 2011-02-24
13
Medium Priority
?
747 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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
 
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 1000 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Suggested Courses

752 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