Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3747
  • Last Modified:

Can Disk Cleanup be Automated script?

Hello,

I have a 'batch' file that will automate the CHKDSK, DEFRAG, and DISK CLEANUP commands.  However, the DISK CLEANUP step doesnt remove all the files for all the selections in the list.  Can there be script written into my existing batch file to allow DISK CLEANUP to configure what files to remove when ran?

I want all temp files removed, compress files, etc.  All except the Outlook files, those I don't want removed.

Here is the script:
*********************************************************
REM chkdsk and defrag automation
for /F "eol= tokens=1 delims=( " %%i in (DrvLtr.txt) do set DrvLtr=%%i& call :dsKchk
 
:dsKchk
If %DrvLtr% == end goto :eof
chkdsk %DrvLtr%
If not errorlevel 3 goto :defrag
If not exist %DrvLtr%\winnt If not exist %DrvLtr%\windows If not exist %DrvLtr%\pagefile.sys goto :dskchkon
 
:dskchkoff
cd\
%DrvLtr%
echo Y chkdsk /F /R
goto :defrag
 
:dskchkon
chkdsk %DrvLtr% /F /R
 
:defrag
cd\
%DrvLtr%
defrag %DrvLtr% -b
defrag %DrvLtr%
 
:dskClean
REGEDIT /S cleanmgr.reg 
CLEANMGR /sagerun:64
 
:EOF
********************************************************************

Open in new window

0
zbox
Asked:
zbox
  • 8
  • 5
1 Solution
 
cybrexusCommented:
Click Start, and then click Run. Type cleanmgr /sageset:50 and click OK

Select from the list all the items you would like to be removed, uncheck Outlook
Click OK to save the changes.
Now, the cleanup configuration is stored in the registry.
Just use cleanmgr /sagerun:50 in the batch files to clean the items you have previously selected
0
 
zboxAuthor Commented:
that sounds like it would work.  Allow me to add a few more details.  I need to get this maintenance batch file to over 800 computers.  If possible, I'd like to have this programed all into the batch file.

someone dropped this on me recently, but I don't know how to configure the batch file to run this automatically.
http://www.ss64.com/nt/cleanmgr.html
 
After you create the registry settings for what you want cleaned and exported them to a file named "cleanmgr.reg" use the attached script:

Open in new window

0
 
cybrexusCommented:
it looks like the scipt you posted first should do the job
:dskClean
REGEDIT /S cleanmgr.reg <- this will put the settings in the registry
CLEANMGR /sagerun:64   <- this will the clean mrg with the settings above

if you're on a domain just push it as a logoff script maybe or just as a startup program
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
zboxAuthor Commented:
Hmm, perhaps I should run a few more test.  Tonight I'll be working late to push this to several computers to see what the results are.  Thanks cybrexus, I'll be back later if everything is okay to confirm the solution.

zbox
0
 
zboxAuthor Commented:
about the comments above, this command is part of my script but doesnt give the expected results.

:dskClean
REGEDIT /S cleanmgr.reg
CLEANMGR /sagerun:64

but after it runs, I should be able to go back to cmd prompt and run ' cleanmgr /sageset:64 '  and i should see a popup window that has everything checked except just a few things.

Instead when I run CLEANMGR /SAGESET:64, there is only a couple things checked to clean.  This tells me that the "cleanmgr.reg" file im keeping in my folder is not doing what I want it too.

What is needed is a script that will automate, CLEANMGR /sageset:64  and then check the boxes I want it too, THEN run the CLEANMGR /sagerun:64 command.  That would work.

is this possible.  everything i research online doesn't mention automating the sageset part of it.

by the way here is what my "cleanmgr.reg" file looks like below,

thanks
Windows Registry Editor Version 5.00
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches]
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Content Indexer Cleaner]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Internet Cache Files]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Files]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Offline Pages Files]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Old ChkDsk Files]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Recycle Bin]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Files]
"StateFlags0001"=dword:00000002
 
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Temporary Offline Files]
"StateFlags0001"=dword:0000000

Open in new window

0
 
cybrexusCommented:
Your problem is that when you set for example "StateFlags0001"=dword:00000002 -it corresponds to sageset 1 (number 1) but after that you run sagerun:64 which corresponds to set 64
That's why it doesnt work.
So whether you change "StateFlags0001"=dword:00000002 to "StateFlags0064"=dword:00000002 in your reg file or you run sagerun:1



0
 
zboxAuthor Commented:
Hmm.  That really sounds like the answer and I want it to be, lol.   But I just tried it out.  I opened sageset:1 and removed all the checked boxes.  Then I closed it and I ran the .reg file. (which should check all the boxes I want it too for sageset:1)  Afterwards I opened up the sageset:1 settings again and I see that all the boxes are still left unchecked.  So this .reg file is doing nothing for me.

Any other ideas?  Thanks for all you do.

zbox
0
 
cybrexusCommented:
it has to work, I just tried on 2 different machine (1 XP and 1 Vista)
make sure you follow the steps:
1. Run > cmd
2. type cleanmgr /sageset:1
3. uncheck everything
4. click OK
5. paste your exact reg file above in notepad, save it and run it
6. in the same cmd window press the Up arrow key (or just type cleanmgr /sageset:1 again)
7. all the things you have checked in the reg file should be there!!

to be sure you may open regedit and and see if the entries in the reg file exist in the registry
0
 
zboxAuthor Commented:
wait your right!  I was being a newb.  I was typing "cleanmgr sageset:1" and not /sageset:1.  I forgot the forward slash.

you are absolutely correct cybrexus, sorry I doubted your skills.  haha.

This should take care of it then.  I will accept the above comment as solution, because that answers my question.  I will post back results as to whether the script works properly running the .reg file though.

peace

zbox
0
 
zboxAuthor Commented:
good eye.  thanks again!   i'm testing it now to make sure it works when the script is ran from start to finish.

ill say whether it was successful or not back on the additional comments option for this question.

peace

zbox
0
 
zboxAuthor Commented:
This was the actual solution by cybrexus, can a moderator change it please.

"cybrexus:Your problem is that when you set for example "StateFlags0001"=dword:00000002 -it corresponds to sageset 1 (number 1) but after that you run sagerun:64 which corresponds to set 64
That's why it doesnt work.
So whether you change "StateFlags0001"=dword:00000002 to "StateFlags0064"=dword:00000002 in your reg file or you run sagerun:1"

Thanks, i accidently accepted the explination of the solution as the solution.

zbox
0
 
zboxAuthor Commented:
The answer is no.

You were correct to say that sageset:1 is what was being changed, etc. That was a good solution, however, the script itself is not running the "cleanmgr.reg" file to add the sageset:1 information to the registry.

What's the proper way to use script to execute this file?  its the file i mentioned earlier, "cleanmgr.reg" in the same folder as the batch file.

zbox
0
 
cybrexusCommented:
normally REGEDIT /S cleanmgr.reg is the command, there is no other than this one
you can also try REGEDIT cleanmgr.reg without the /S for troubleshooting -this should display a confirmation message before and after importing the file

and don't forget to change to CLEANMGR /sagerun:1

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now