Jump to content

Defrag batch file


tonysathre
 Share

Recommended Posts

I was bored so I wrote this defrag script that will defrag all the volumes on your system. Enjoy!

@echo off

::==defragger.bat

set logfile=c:defrag.log

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%"

for /f "tokens=1 delims=:" %%i in ('reg query hklmsystemmounteddevices ^| findstr DosDevices') do (

echo %%i >> drives

)

for /f "tokens=3 delims=" %%i in (drives) do (

echo %%i >> drives.txt

)

for /f %%i in (drives.txt) do (

echo %%i: >> drives.log

)

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

for /f %%i in (newlist.txt) do fsutil fsinfo drivetype %%i | findstr "Fixed" >> final.txt

for %%i in (drives drives.log drives.txt new.txt newlist.txt final.txt) do attrib +h %%i

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

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

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

for /f "tokens=1 delims=:" %%i in ('reg query hklmsystemmounteddevices ^| findstr DosDevices') do (

echo %%i >> drives

)

for /f "tokens=3 delims=" %%i in (drives) do (

echo %%i >> drives.txt

)

for /f %%i in (drives.txt) do (

echo %%i: >> drives.log

)

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

for /f %%i in (newlist.txt) do fsutil fsinfo drivetype %%i | findstr "Fixed" >> final.txt

for %%i in (drives drives.log drives.txt new.txt newlist.txt final.txt) do attrib +h %%i

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%

)

echo Defragger completed successfully

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

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...