UltraDefrag command line (console) interface can be used to automate defragmentation via batch scripts and scheduled tasks. Also it can be used instead of the graphical interface by those who prefer command line environment.
Also UltraDefrag uses its command line interface to handle its context menu entries in Windows Explorer. Right click any object and select Defragment with UltraDefrag to rejoin its clusters. To stop defragmentation hit Ctrl+C or close the command prompt. To configure the context menu handler click Settings > Options in UltraDefrag GUI or edit the following file manually: {installation folder}\conf\options.lua.
List of space separated paths which need to be defragmented. Both absolute and relative paths are supported, as well as wildcards: the question mark (?) matches any one character, while the asterisk (*) - any zero or more characters. Thus *.* matches any file with an extension while a single asterisk (*) matches anything (with or without an extension). All paths including spaces must be enclosed by double quotes:
:: defragment drives d: and e:
udefrag d: e:
:: defragment the contents of the USERPROFILE folder
udefrag "%USERPROFILE%\*"
:: defragment everything inside of the '..\Test Files' folder
udefrag "..\Test Files\*"
:: defragment some individual file
udefrag C:\WINDOWS\WindowsUpdate.log
:: defragment them all at once
udefrag d: e: "%USERPROFILE%\*" "..\Test Files\*" C:\WINDOWS\WindowsUpdate.log
:: rejoin clusters of the WINDOWS folder
:: (its contents will be left untouched)
udefrag C:\WINDOWS
If command line is empty the program displays help.
@echo off
:: save debugging information for troubleshooting
set UD_LOG_FILE_PATH=C:\Windows\Temp\udefrag.log
:: terminate the processing after 6 hours and 30 minutes
set UD_TIME_LIMIT=6h 30m
:: exclude temporary stuff
set UD_EX_FILTER=*system volume information*;*temp*;*tmp*;*recycle*
:: exclude archives as well
set UD_EX_FILTER=%UD_EX_FILTER%;*.zip;*.7z;*.rar
:: eliminate only fragments below 20 MB in size
set UD_FRAGMENT_SIZE_THRESHOLD=20MB
:: list all drives available for defragmentation
udefrag -la
:: check all available drives for errors
for /F "tokens=1 skip=8" %%D in ('udefrag -la') do chkdsk %%D
:: analyze all fixed drives
udefrag -a --all-fixed
:: defragment everything on drive c:
udefrag c:
:: defragment small audio files on drive d:
set UD_IN_FILTER=*.mp3
set UD_FILE_SIZE_THRESHOLD=20MB
udefrag "D:\My Docs\Music\*"
:: defragment large video files having at least 10 fragments
set UD_IN_FILTER=*.avi;*.mkv
set UD_FILE_SIZE_THRESHOLD=
set UD_FRAGMENTS_THRESHOLD=10
udefrag "D:\My Docs\Movies\*"
:: optimize master file tables on drives c: and d:
udefrag --optimize-mft c: d:
:: reset filters used above
set UD_IN_FILTER=
set UD_FRAGMENTS_THRESHOLD=
:: sort files on drive g: by creation time in descending order
set UD_SORTING=C_TIME
set UD_SORTING_ORDER=DESC
udefrag -o g:
:: defragment drive h: only if its fragmentation level is above 10%
set UD_FRAGMENTATION_THRESHOLD=10
udefrag h:
:: defragment all fixed disks except of c:, d:, g: and h:
set UD_EX_FILTER=%UD_EX_FILTER%;C:\*;D:\*;G:\*;H:\*
udefrag --all-fixed
:: defragment a disk with no drive letter assigned,
:: mounted to C:\Games, using the diskpart utility
:: first of all, assign a drive letter to the disk
echo select volume c:\games > script
echo assign letter=t >> script
diskpart /s script
:: then, defragment the disk
udefrag t:
:: after all, remove the assigned letter
echo select volume c:\games > script
echo remove letter=t >> script
diskpart /s script
:: hibernate the computer using a special
:: command line tool shipped with UltraDefrag
hibernate4win now
:: shut the computer down if hibernation failed
if errorlevel 1 shutdown -s -t 00