Computing.Net > Forums > Programming > Defrag batch file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Defrag batch file

Reply to Message Icon

Name: tonysathre
Date: April 26, 2007 at 23:03:00 Pacific
OS: Windows XP SP2/ VMware/ u
CPU/Ram: P4 2.53 Ghz\ 768 MB
Product: Dell Dimension 4550
Comment:

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 ]--------------::




Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Defrag batch file

programming with batch files www.computing.net/answers/programming/programming-with-batch-files/16969.html

batch file programming www.computing.net/answers/programming/batch-file-programming/17121.html

Batch file troubles www.computing.net/answers/programming/batch-file-troubles/17359.html