HowTo Defragment a Drive From The Command Prompt
One of the most common complaints that I get from people is that their computer just seems to be getting slower and slower as time goes on. There can be several reasons behind this but one of the common issues is file fragmentation.
File fragmentation occurs when a file is saved on the hard drive in pieces. These pieces are located at different places on the hard drive whic, in turn makes the hard drive work harder to get that file and it takes longer for that file to be retrieved.
One of the best things that you can do to help aleviate this issue is to defragment your hard drive. This is, essentially, the process of bringing all of the file pieces together again so that the hard drive can access them quicker.
There are several different ways to defragment your hard drive but I prefer to use the command prompt because it is quick to access and can be easily run from a batch file or scheduled.
Here are the steps to follow so that you can defragment your hard drive from the command:
- Open up a command prompt by going to Start→Run…. Type in cmd and click OK.
- Let’s assume that you would like to perform a complete defragment of the C: drive. To do this, you would simply type
defrag C: -fand then press the Enter button. - Once the defragmentation process has completed, you can close the command prompt.
You can repeat this process for every hard drive.
If you want to make the process even easier, create a file in Notepad called defrag.bat and save it to your desktop. The, in the file create a defrag entry for each hard drive on your system and save the file. For example, if you had hard drives C:, E:, and F: on your system, your defrag.bat file would look like this:
defrag C: -f
defrag E: -f
defrag F: -f
Then, all you would need to do to defragment your hard drive would be to double click on the defrag.bat file.
9 Responses to “HowTo Defragment a Drive From The Command Prompt”
-
Jared Says:
February 14th, 2007 at 9:01 amDon’t forget CCleaner to clean out those old, invalid registry files too - and the program’s free!
-
Matt T. Says:
February 14th, 2007 at 10:37 amFor some reason launching this from the desktop (at least on XP and 2003 server) does not work. I had to add a “cd ..” to the beginning of the batch file to navigate up one directory before it would kick off.
-
cj Says:
February 15th, 2007 at 1:53 amIs there any way to do this before Windows is fully loaded, like what a chkdsk does at bootup?
-
prepostra Says:
February 15th, 2007 at 3:37 pmYou might also consider putting the batch file into a Scheduled Task so that it runs once a week or fortnight (mine runs weekly) on a Saturday Morning.
Fred Langa wrote a great article back in 2003 about this in Information Week
http://preview.tinyurl.com/2fb7jz
It gets interesting about page 3 -
Tim Fehlman Says:
February 15th, 2007 at 3:59 pmCJ,
Yes you could but I wouldn’t recommend it. Defrag can take a very long time to perform and this means that you could be stuck waiting for quite some time before you can log into your system.What I would alternately suggest is that you schedule a defrag to occur on a regular basis. See 14 Windows Command Line Tricks for a quick way to do this.
Tim
-
Alex Says:
February 23rd, 2007 at 12:28 amWhy doesn’t this work for me? I have it set to run in the My Documents file (Windows XP 32bit), but when I run the batch file it just repeats the line “D:\Documents and Settings\My Name\My Documents>defrag D: -f”. The weird thing is, it works when I run it from the “command.com” window, but not from the “cmd” window. The windows defragmenter isn’t on, and no other programs besides Firefox were running.
-
der kleine Alltag Says:
March 6th, 2007 at 8:52 amSoeben bei Daily Cup of Tech gesehen: Mittels defrag c: -f -v (für die C: Partition) kann man das Defragmentieren aus der Kommandozeile ausfühlen und erhält auch gleich einige zusätzliche informationen. Ein guter Nebeneffekt ist, dass es sich so natürlich auch in einem
-
Nick Says:
March 29th, 2007 at 3:04 pmThis is great if you have admin privileges on the PC. But what about those who don’t? There has got to be a way to schedule a defrag without buying any third party software for a non-admin user. Anybody have any ideas?
-
aLan Says:
January 20th, 2008 at 10:32 pmWhat if you could defrag a network hard drive from the command prompt? Then you could write a batch file and have outlook run it whenever it sees for example: “defragftpserver” in the subject of a text message (via POP3). The text message would be coming from your cell phone.
-aLan
