Advertisement
Guest User

gcf_defrag

a guest
Aug 8th, 2011
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.14 KB | None | 0 0
  1. @echo off
  2. @title Auto GCF Defrag script
  3. :: Purpose:
  4. :: To provide a quick and painless way to defrag your grid cache/valve pak files.
  5. ::
  6. :: Authors:
  7. :: i-ghost
  8. ::
  9. :: Variables:
  10. :: #TODO: Why are these listed here?
  11. :: _last_time - last run of script, written to registry. Initially the date, but merged with Time2.
  12. :: Time2 - last run of script, written to registry. initially the time, but merged with _last_time.
  13. :: _steamapps - Steamapps directory
  14. :: _bin - Location of Steam binary
  15. :: _shut - Shutdown toggle, written to registry
  16. :: _kill - Kill Steam toggle, written to registry
  17. :: _steamuser - Steam username
  18. :: _last - Last Community name used
  19. :: _pid - Process ID of Steam
  20. :: _pid2 - Process ID of Steam chat logger
  21. :: Time3 - Scipt end time
  22. :: _agg - Force defrag toggle, written to registry
  23. :: _vpk - VPK defrag toggle, written to registry
  24. ::
  25. :: Theory:
  26. :: Takes registry values, applies defaults if not present. Builds menu based on preferences.
  27. :: Builds a path based on steamapps location.
  28. :: Checks for presence of hlextract in %path%. Will not continue if not found.
  29. :: On start - if steam kill is enabled, will kill steam by PID. If chat log pid was present, will kill that first.
  30. :: Then timeout for a pause, to allow any mounted gcfs to unmount. Then a for loop for the defrag.
  31. :: Multiple if statements for handling vpk/forced defrag/both.
  32. :: vpk defrag is a recursive for loop starting at the %_steamapps%/common directory.
  33. :: If shutdown enabled, call the shutdown function and end here.
  34. :: If not enabled, check if steam kill enabled. If yes, launch steam via %_bin%. If not, do nothing.
  35. :: In all cases the vars are written to the registry.
  36. echo Getting registry values...
  37. setlocal
  38. :: These values will only need to be obtained once, so we put them here
  39. :: Get the Steam app dir from registry
  40. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\Valve\Steam" /v SteamPath 2^>nul') do set _steamapps=%%B\steamapps
  41. if not defined _steamapps ( echo Steam not found & echo. & pause )
  42. :: Convert forward slashes to backslashes
  43. set _steamapps=%_steamapps:/=\%
  44. :: Get Steam username
  45. for /f "tokens=2,3 delims= " %%a in ('findstr /C:AutoLoginUser "%_steamapps%\..\config\SteamAppData.vdf"') do set _steamuser=%%~a
  46. :: Fallback to the Community name if above fails
  47. if not defined _steamuser (
  48. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\Valve\Steam" /v LastGameNameUsed 2^>nul') do set _last=%%B
  49. )
  50. :: Get our steam PIDs
  51. call :getsteam
  52. :: Get steam binary location
  53. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\Valve\Steam" /v SteamExe 2^>nul') do set _bin=%%B
  54. :: Read in last run date and preferences
  55. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v LastRun 2^>nul') do set _last_time=%%B
  56. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v Shutdown 2^>nul') do set _shut=%%B
  57. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v SteamKill 2^>nul') do set _kill=%%B
  58. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v Aggressive 2^>nul') do set _agg=%%B
  59. for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v DoVPKs 2^>nul') do set _vpk=%%B
  60. :: Add to the user's path.
  61. set path=%path%;%_steamapps%
  62. ::set path=%path%;%cd%
  63. :: Unnecessary as %cd% is already part of %path%
  64. :_menu
  65. :: sane default/first run handling
  66. if not defined _shut set _shut=No&&call :write
  67. if not defined _kill set _kill=No&&call :write
  68. if not defined _agg set _agg=No&&call :write
  69. if not defined _vpk set _vpk=No&&call :write
  70. set _ok=
  71. cls
  72. echo ______________________________________________________________________
  73. echo.
  74. echo Steamapps dir is : [%_steamapps%]
  75. if defined _steamuser ( echo Username : [%_steamuser%] )
  76. if defined _last ( echo Community name : [%_last%] )
  77. if defined _last_time ( echo Last run was at : [%_last_time%] )
  78. echo.
  79. echo Options:
  80. echo Force defrag : [%_agg%] - f - Forces defrag on all files
  81. echo Defrag VPKs : [%_vpk%] - v - Defrag VPK files
  82. echo Shutdown PC : [%_shut%] - s - Shutdown PC after defrag
  83. echo Shutdown Steam : [%_kill%] - k - Shutdown Steam and relaunch after defrag
  84. if /I [%_kill%] == [Yes] (
  85. if defined _pid (
  86. echo Steam PID is : [%_pid%]
  87. ) else echo.& echo Steam isn't running at the moment... If it is, type k to get the PID
  88. if defined _pid2 ( echo Chat Log PID is : [%_pid2%] )
  89. )
  90. echo.
  91. (hlextract 2>&1) | FinD /I "hllib" >NUL
  92. if errorlevel 1 (
  93. echo 
  94. echo hlextract.exe not found. Please place it in the same directory
  95. echo as this script or in %_steamapps%
  96. echo.
  97. echo Press Enter to exit
  98. set /p _ok=______________________________________________________________________
  99. endlocal
  100. exit /b 1
  101. ) else (
  102. echo hlextract.exe found
  103. echo.
  104. )
  105. echo Press Enter to begin defrag
  106. set /p _ok=______________________________________________________________________
  107. :: Shutdown toggle
  108. if /I [%_ok%] == [s] if /I [%_shut%] == [no] set _shut=Yes&& call :write&& goto :_menu
  109. if /I [%_ok%] == [s] if /I [%_shut%] == [yes] set _shut=No&& call :write&& goto :_menu
  110. :: Steam kill toggle
  111. if /I [%_ok%] == [k] if /I [%_kill%] == [no] set _kill=Yes&& call :write&& call :getsteam&& goto :_menu
  112. if /I [%_ok%] == [k] if /I [%_kill%] == [yes] set _kill=No&& call :write&& goto :_menu
  113. :: Agressive defrag toggle
  114. if /I [%_ok%] == [f] if /I [%_agg%] == [no] set _agg=Yes&& call :write&& goto :_menu
  115. if /I [%_ok%] == [f] if /I [%_agg%] == [yes] set _agg=No&& call :write&& goto :_menu
  116. :: VPK toggle
  117. if /I [%_ok%] == [v] if /I [%_vpk%] == [no] set _vpk=Yes&& call :write&& goto :_menu
  118. if /I [%_ok%] == [v] if /I [%_vpk%] == [yes] set _vpk=No&& call :write&& goto :_menu
  119. if /I [%_ok%] NEQ [] goto :_menu
  120. if /I [%_shut%] == [yes] title Auto GCF Defrag script - Will shutdown after defrag has completed
  121. echo.
  122. echo Defragment started. Press CTRL + C to interrupt (not recommended).
  123. echo ______________________________________________________________________
  124. echo.
  125. ::Get start time of script.
  126. call :time Time2 _last_time
  127. ::cd %_steamapps%
  128. if [%_kill%] == [Yes] (
  129. if defined _pid2 taskkill /PID %_pid2% /F >nul
  130. if defined _pid2 timeout /t 5 /nobreak >nul
  131. if defined _pid taskkill /PID %_pid% /F >nul
  132. )
  133. :: Wait for gcfs to be unmounted
  134.  
  135. :: Force defrag option
  136. if /I [%_agg%] == [Yes] (
  137. for %%A in (%_steamapps%\*.gcf) do "HLExtract.exe" -f -r -p "%%A"
  138. if /I [%_vpk%] == [Yes] (
  139. echo.
  140. echo GCFs done. Proceeding to VPKs.
  141. echo.
  142. for /R %_steamapps%\common %%A in (*.vpk) do "HLExtract.exe" -f -r -p "%%A"
  143. )
  144. ) else (
  145. :: Regular defrag
  146. for %%A in (%_steamapps%\*.gcf) do "HLExtract.exe" -f -p "%%A"
  147. if /I [%_vpk%] == [Yes] (
  148. echo.
  149. echo GCFs done. Proceeding to VPKs.
  150. echo.
  151. for /R %_steamapps%\common\ %%A in (*.vpk) do "HLExtract.exe" -f -p "%%A"
  152. )
  153. )
  154. echo.
  155. echo ______________________________________________________________________
  156. echo.
  157. if /I [%_shut%] == [yes] call :_shutdown && endlocal && exit /b 0
  158. call :time Time3
  159. for /F "tokens=1-3 delims=:.," %%A in ("%Time%") do set Time3=%%A:%%B:%%C
  160. :: Steam handling
  161. if /I [%_kill%] == [Yes] (
  162. echo Defrag completed at [%Time3%] - press Enter to start Steam
  163. ) else (
  164. echo Defrag completed at [%Time3%] - press Enter to exit
  165. )
  166. echo   
  167. set _last_time=%Time2%/%_last_time%
  168. reg add "HKCU\Software\i-ghost\autogcf" /v LastRun /t REG_SZ /d "%_last_time%" /f >nul
  169. set /p _ok=______________________________________________________________________
  170. if /I [%_kill%] == [Yes] (start %_bin% -silent)
  171. endlocal
  172. exit /b 0
  173.  
  174. :write
  175. reg add "HKCU\Software\i-ghost\autogcf" /v Shutdown /t REG_SZ /d "%_shut%" /f >nul
  176. reg add "HKCU\Software\i-ghost\autogcf" /v SteamKill /t REG_SZ /d "%_kill%" /f >nul
  177. reg add "HKCU\Software\i-ghost\autogcf" /v Aggressive /t REG_SZ /d "%_agg%" /f >nul
  178. reg add "HKCU\Software\i-ghost\autogcf" /v DoVPKs /t REG_SZ /d "%_vpk%" /f >nul
  179. goto :EOF
  180.  
  181. :time
  182. :: First param is set to time, second is set to date and is optional
  183. for /F "tokens=1-3 delims=:.," %%A in ("%Time%") do set %1=%%A:%%B:%%C
  184. if [%2] NEQ [] (
  185. for %%A in (%Date%) DO (
  186. for /F "tokens=1-3 delims=/-" %%B in ("%%~A") DO (
  187. SET %2=%%C.%%B.%%D
  188. )
  189. )
  190. )
  191. goto :EOF
  192.  
  193. :getsteam
  194. :: Miscellaneous
  195. ::for /F "tokens=2* delims= " %%A in ('REG QUERY "HKLM\Software\Valve\Steam" /v SteamPID 2^>nul') do set _pid=%%B
  196. ::set /A _pid=%_pid% + 0
  197. :: Old method
  198. :: Stored in registry, prefer to get it via cmd
  199. for /F "tokens=2,3 delims= " %%A in ('tasklist /FI "imagename eq Chat Log.exe" /FI "Status eq running" /NH 2^>nul') do set _pid2=%%B
  200. for /F "tokens=1,2 delims= " %%A in ('tasklist /FI "imagename eq Steam.exe" /FI "Status eq running" /NH 2^>nul') do set _pid=%%B
  201. if /I [%_pid%] == [No] set _pid=
  202. if /I [%_pid2%] == [tasks] set _pid2=
  203. goto :EOF
  204.  
  205. :_shutdown
  206. call :time Time3
  207. echo Defrag completed at [%Time3%] - Shutting down in 10 seconds
  208. echo Press Enter to abort
  209. echo   
  210. set _last_time=%Time2%/%_last_time%
  211. reg add "HKCU\Software\i-ghost\autogcf" /v LastRun /t REG_SZ /d "%_last_time%" /f >nul
  212. shutdown /s /t 12 /c "GCF defrag completed - Shutting down in 10 seconds"
  213. set /p _ok=______________________________________________________________________
  214. shutdown /a
  215. goto :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement