Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
Name: tonysathre
I was bored so I wrote this defrag script that will defrag all volumes on a system. This includes HDD partitions only.
@echo off
set logfile=c:\defrag.log
:: Check for old files
set oldfiles=drives drives.log drives.txt new.txt newlist.txt final.txt
for %%i in (%oldfiles%) do if exist %%i (
del /q /f %%i > nul
)
if "%logfile%" == "" (
goto :no_log
) else ( goto :log
):log
echo Defragger started at %time% on %date% by %username% on workstation %computername% > "%logfile%"
echo. >> "%logfile%"
echo. >> "%logfile%":: Enumerate the Windows registry for valid mount points
for /f "tokens=1 delims=:" %%i in ('reg query hklm\system\mounteddevices ^| findstr \DosDevices\') do (
echo %%i >> drives
):: Remove \DosDevices\ prefix from the drive letters
for /f "tokens=3 delims=\" %%i in (drives) do (
echo %%i >> drives.txt
):: Append a semicolon ':' to each drive letter in drives.txt
for /f %%i in (drives.txt) do (
echo %%i: >> drives.log
):: Check if drive A: exist because we don't need to defrag your floppy drive ;-)
sort drives.log >> new.txt
type new.txt | findstr "A" > nul
if errorlevel 0 for /f "tokens=1 skip=1" %%i in (new.txt) do echo %%i >> newlist.txt:: Check whether the drive is a hard drive or another type of drive
for /f %%i in (newlist.txt) do fsutil fsinfo drivetype %%i | findstr "Fixed" >> final.txt:: Hide temp files so they don't get accidentally deleted before Defragger finishes ;-)
for %%i in (drives drives.log drives.txt new.txt newlist.txt final.txt) do attrib +h %%i:: Now check whether each drive needs to be defragged and continue accordingly
for /f %%i in (final.txt) do (
defrag -a %%i | findstr "should" > nul
if errorlevel 0 title Defragging %%i && echo Defragging %%i && defrag %%i >> "%logfile%"
echo.
echo.
echo Defrag on drive %%i completed at %time%
)echo Defragger completed at %time% >> "%logfile%"
echo Defragger completed successfully::Now we cleanup all the temp files
del /q /f drives > nul
del /q /f drives.txt > nul
del /q /f drives.log > nul
del /q /f new.txt > nul
del /q /f newlist.txt > nul
del /q /f final.txt > nul:: Mission Complete
echo.
echo Log file saved to %logfile%
echo Would you like to view the log file now? [y/n]
set /p choice=%1
if "%choice" == "y" notepad "%logfile%"
if "%choice%" == "yes" notepad "%logfile%"
pause
goto :eof
:no_log
:: Enumerate the Windows registry for valid mount points
for /f "tokens=1 delims=:" %%i in ('reg query hklm\system\mounteddevices ^| findstr \DosDevices\') do (
echo %%i >> drives
):: Remove \DosDevices\ prefix from the drive letters
for /f "tokens=3 delims=\" %%i in (drives) do (
echo %%i >> drives.txt
):: Append a semicolon ':' to each drive letter in drives.txt
for /f %%i in (drives.txt) do (
echo %%i: >> drives.log
):: Check if drive A: exist because we don't need to defrag your floppy drive ;-)
sort drives.log >> new.txt
type new.txt | findstr "A" > nul
if errorlevel 0 for /f "tokens=1 skip=1" %%i in (new.txt) do echo %%i >> newlist.txt:: Check whether the drive is a hard drive or another type of drive
for /f %%i in (newlist.txt) do fsutil fsinfo drivetype %%i | findstr "Fixed" >> final.txt:: Hide temp files so they don't get accidentally deleted before Defragger finishes ;-)
for %%i in (drives drives.log drives.txt new.txt newlist.txt final.txt) do attrib +h %%i:: Now check whether each drive needs to be defragged and continue accordingly
for /f %%i in (final.txt) do (
defrag -a %%i | findstr "should" > nul
if errorlevel 0 title Defragging %%i && defrag -f %%i
echo.
echo.
echo Defrag on drive %%i completed at %time%
):: Mission Complete
echo Defragger completed successfully:: Now we clean up all the temp files
del /q /f drives > nul
del /q /f drives.txt > nul
del /q /f drives.log > nul
del /q /f new.txt > nul
del /q /f newlist.txt > nul
echo Press any key to close this window
pause > nul
goto :eof::--------------[ EOF ]--------------::

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |