?
Solved

Running defrag

Posted on 2004-08-01
4
Medium Priority
?
313 Views
Last Modified: 2013-11-20
How can I run a Windows built-in disk defragmenter from my MFC app?

My app creates/deletes large numbers of large files, soI would like it to automatically defragment the hard drive, when it finds appropriate. The defrag should run with no user assistance.
0
Comment
Question by:olegsp
[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
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 240 total points
ID: 11690684
You can use WinApi WinExec funtion to call defrag.exe:

::WinExec("defrag c:", SW_HIDE);

Also you can use WinAPI ShellExecute() function.
0
 
LVL 14

Assisted Solution

by:wayside
wayside earned 160 total points
ID: 11695911
You could use the file system control commands to write your own simple defragger as outlined here with source code: http://www.sysinternals.com/ntw2k/info/defrag.shtml

Or you could use WMI, using the Win32_Volume object and the DefragAnalysis and Defrag methods and the Win32_DefragAnalysis object to determine if you need to defragment, and then actually do it.

Defragging can take hours and significantly impacts the performance of the system when it runs,  I'm not sure it's such a good idea to do this automatically, unless the user has requested that it be done that way.
0
 

Assisted Solution

by:cheapstr
cheapstr earned 100 total points
ID: 11969342
WinExec("c:\\windows\\system32\\dfrg.msc", SW_MAXIMIZE); should bring up the defrag program for you
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

765 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