• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1165
  • Last Modified:

Script for Defraggler

I would like to create a script that would run the Defraggler Program

Defrag ALL Drives

Not From within Defraggler.

Any suggestions?

Joseph Salazar
Joseph Salazar
5 Solutions
Haresh NikumbhSr. Tech leadCommented:
Steven HarrisPresidentCommented:
If you want to keep it simple and flexible, create a .bat for CMD.

To perform on a specific volume:

defrag <volume> /U

Open in new window

To perform on all volumes:

defrag /C /U

Open in new window

 /A      Perform analysis on the specified volumes.
 /C      Perform the operation on all volumes.
 /E      Perform the operation on all volumes except those specified.
 /H      Run the operation at normal priority (default is low).
 /M      Run the operation on each volume in parallel in the background.
 /T      Track an operation already in progress on the specified volume.
 /U      Print the progress of the operation on the screen.
 /V      Print verbose output containing the fragmentation statistics.
 /X      Perform free space consolidation on the specified volumes.
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Are you talking about the Defraggler utility by Piriform, or about the in-built Windows defrag.exe?



It comes as an installer which installs both the Windows GUI and the command line versions of the program.

Advanced use of DEFRAGGLER.EXE, including how to use a custom *.INI file to store program settings and how to run it from a USB Flash Drive:
To create an INI file rather than have it use the Windows Registry: Settings menu > Options > Advanced > "Save Settings to INI file".

Command line options for DF.EXE:
You cannot defrag more than one drive at a time:

The most useful command line switch if using a batch file to "defraggle" one drive after another would be the /L switch to list volumes that it can "defraggle".  You should then be able to parse that information and set the drive letters as variables to use in a command that is repeated, or ones that run sequentially (consecutively).  I will look at it later if this is what is being asked about.
If you were talking about Piriform Defraggler, then here is an example batch file that should work, but I have only tested it in Windows XP and have only allowed it to echo what it will do rather than actually run it on all the drives
@echo off
SetLocal EnableDelayedExpansion

REM Change this path if it is different on your computer.
set PROG=C:\Program Files\Defraggler\df.exe

REM Checks to make sure that the program is where the above path says it is.
if not exist "%PROG%" echo Defraggler Command Line Program Not Found. & pause & goto :EOF

REM Run the program with the /L switch to list accessible drives and exclude
REM the first few informational lines of what it would display.
REM For each line found, use only the first of the space-separated fields.
REM This will be the drive letter with the colon after it.
REM This  FOR  command is a loop that processes one line at a time, and
REM therefore one drive letter at a time before going on to the next.
REM Everything inside the brackets is the loop.  When there are no more lines
REM from the results of the program's /L switch, it finishes.

for /f "skip=3 tokens=1 delims= " %%A in ('call "%PROG%" /L') do (
    set DRIVE=%%A
    echo Defraggling !DRIVE!
    echo call "%PROG%" !DRIVE! /QD
    echo Finished Defraggling !DRIVE!
    echo Press any key to continue with next drive, or Ctrl + C to exit ...
    pause > nul
echo Press any key to quit ...
pause > nul

Open in new window

You can remove all the lines that begin with  REM  as they are only comments or "REMarks" for your information.

Note: As it is, this is only going to go through the steps involved with calling DF.EXE and will show the command on the screen.  It will not execute the command until you remove the   echo   at the start of the line:
echo call "%PROG%" !DRIVE! /QD

Additionally, you should also delete these lines inside the brackets for the process to continue without human intervention:

    echo Press any key to continue with next drive, or Ctrl + C to exit ...
    pause > nul

I have used the "Quick Defrag(gle)" switch  /QD  for this example.

Warning:  I have absolutely no idea what will happen if the C: Drive is Defragg(l)ed while the batch file that called it is actually on that drive.  I would guess that it will just treat the batch file as an unmovable file, but if it interrupts the processing it could mess up the sequential flow of the batch file.

I would suggest that you run this on a test computer, or else make sure that the computer you use it on is well backed up.  I cannot accept responsibility for the program messing your computer up, although I have used this program in the Windows GUI mode without any issues.
Joseph SalazarVice President - Senior IT ConsultantAuthor Commented:
Thank you everyone

I will be using all of these,

Thank you Cjoego
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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