Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- @title Auto GCF Defrag script
- :: Purpose:
- :: To provide a quick and painless way to defrag your grid cache/valve pak files.
- ::
- :: Authors:
- :: i-ghost
- ::
- :: Variables:
- :: #TODO: Why are these listed here?
- :: _last_time - last run of script, written to registry. Initially the date, but merged with Time2.
- :: Time2 - last run of script, written to registry. initially the time, but merged with _last_time.
- :: _steamapps - Steamapps directory
- :: _bin - Location of Steam binary
- :: _shut - Shutdown toggle, written to registry
- :: _kill - Kill Steam toggle, written to registry
- :: _steamuser - Steam username
- :: _last - Last Community name used
- :: _pid - Process ID of Steam
- :: _pid2 - Process ID of Steam chat logger
- :: Time3 - Scipt end time
- :: _agg - Force defrag toggle, written to registry
- :: _vpk - VPK defrag toggle, written to registry
- ::
- :: Theory:
- :: Takes registry values, applies defaults if not present. Builds menu based on preferences.
- :: Builds a path based on steamapps location.
- :: Checks for presence of hlextract in %path%. Will not continue if not found.
- :: On start - if steam kill is enabled, will kill steam by PID. If chat log pid was present, will kill that first.
- :: Then timeout for a pause, to allow any mounted gcfs to unmount. Then a for loop for the defrag.
- :: Multiple if statements for handling vpk/forced defrag/both.
- :: vpk defrag is a recursive for loop starting at the %_steamapps%/common directory.
- :: If shutdown enabled, call the shutdown function and end here.
- :: If not enabled, check if steam kill enabled. If yes, launch steam via %_bin%. If not, do nothing.
- :: In all cases the vars are written to the registry.
- echo Getting registry values...
- setlocal
- :: These values will only need to be obtained once, so we put them here
- :: Get the Steam app dir from registry
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\Valve\Steam" /v SteamPath 2^>nul') do set _steamapps=%%B\steamapps
- if not defined _steamapps ( echo Steam not found & echo. & pause )
- :: Convert forward slashes to backslashes
- set _steamapps=%_steamapps:/=\%
- :: Get Steam username
- for /f "tokens=2,3 delims= " %%a in ('findstr /C:AutoLoginUser "%_steamapps%\..\config\SteamAppData.vdf"') do set _steamuser=%%~a
- :: Fallback to the Community name if above fails
- if not defined _steamuser (
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\Valve\Steam" /v LastGameNameUsed 2^>nul') do set _last=%%B
- )
- :: Get our steam PIDs
- call :getsteam
- :: Get steam binary location
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\Valve\Steam" /v SteamExe 2^>nul') do set _bin=%%B
- :: Read in last run date and preferences
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v LastRun 2^>nul') do set _last_time=%%B
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v Shutdown 2^>nul') do set _shut=%%B
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v SteamKill 2^>nul') do set _kill=%%B
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v Aggressive 2^>nul') do set _agg=%%B
- for /F "tokens=2* delims= " %%A in ('REG QUERY "HKCU\Software\i-ghost\autogcf" /v DoVPKs 2^>nul') do set _vpk=%%B
- :: Add to the user's path.
- set path=%path%;%_steamapps%
- ::set path=%path%;%cd%
- :: Unnecessary as %cd% is already part of %path%
- :_menu
- :: sane default/first run handling
- if not defined _shut set _shut=No&&call :write
- if not defined _kill set _kill=No&&call :write
- if not defined _agg set _agg=No&&call :write
- if not defined _vpk set _vpk=No&&call :write
- set _ok=
- cls
- echo ______________________________________________________________________
- echo.
- echo Steamapps dir is : [%_steamapps%]
- if defined _steamuser ( echo Username : [%_steamuser%] )
- if defined _last ( echo Community name : [%_last%] )
- if defined _last_time ( echo Last run was at : [%_last_time%] )
- echo.
- echo Options:
- echo Force defrag : [%_agg%] - f - Forces defrag on all files
- echo Defrag VPKs : [%_vpk%] - v - Defrag VPK files
- echo Shutdown PC : [%_shut%] - s - Shutdown PC after defrag
- echo Shutdown Steam : [%_kill%] - k - Shutdown Steam and relaunch after defrag
- if /I [%_kill%] == [Yes] (
- if defined _pid (
- echo Steam PID is : [%_pid%]
- ) else echo.& echo Steam isn't running at the moment... If it is, type k to get the PID
- if defined _pid2 ( echo Chat Log PID is : [%_pid2%] )
- )
- echo.
- (hlextract 2>&1) | FinD /I "hllib" >NUL
- if errorlevel 1 (
- echo
- echo hlextract.exe not found. Please place it in the same directory
- echo as this script or in %_steamapps%
- echo.
- echo Press Enter to exit
- set /p _ok=______________________________________________________________________
- endlocal
- exit /b 1
- ) else (
- echo hlextract.exe found
- echo.
- )
- echo Press Enter to begin defrag
- set /p _ok=______________________________________________________________________
- :: Shutdown toggle
- if /I [%_ok%] == [s] if /I [%_shut%] == [no] set _shut=Yes&& call :write&& goto :_menu
- if /I [%_ok%] == [s] if /I [%_shut%] == [yes] set _shut=No&& call :write&& goto :_menu
- :: Steam kill toggle
- if /I [%_ok%] == [k] if /I [%_kill%] == [no] set _kill=Yes&& call :write&& call :getsteam&& goto :_menu
- if /I [%_ok%] == [k] if /I [%_kill%] == [yes] set _kill=No&& call :write&& goto :_menu
- :: Agressive defrag toggle
- if /I [%_ok%] == [f] if /I [%_agg%] == [no] set _agg=Yes&& call :write&& goto :_menu
- if /I [%_ok%] == [f] if /I [%_agg%] == [yes] set _agg=No&& call :write&& goto :_menu
- :: VPK toggle
- if /I [%_ok%] == [v] if /I [%_vpk%] == [no] set _vpk=Yes&& call :write&& goto :_menu
- if /I [%_ok%] == [v] if /I [%_vpk%] == [yes] set _vpk=No&& call :write&& goto :_menu
- if /I [%_ok%] NEQ [] goto :_menu
- if /I [%_shut%] == [yes] title Auto GCF Defrag script - Will shutdown after defrag has completed
- echo.
- echo Defragment started. Press CTRL + C to interrupt (not recommended).
- echo ______________________________________________________________________
- echo.
- ::Get start time of script.
- call :time Time2 _last_time
- ::cd %_steamapps%
- if [%_kill%] == [Yes] (
- if defined _pid2 taskkill /PID %_pid2% /F >nul
- if defined _pid2 timeout /t 5 /nobreak >nul
- if defined _pid taskkill /PID %_pid% /F >nul
- )
- :: Wait for gcfs to be unmounted
- :: Force defrag option
- if /I [%_agg%] == [Yes] (
- for %%A in (%_steamapps%\*.gcf) do "HLExtract.exe" -f -r -p "%%A"
- if /I [%_vpk%] == [Yes] (
- echo.
- echo GCFs done. Proceeding to VPKs.
- echo.
- for /R %_steamapps%\common %%A in (*.vpk) do "HLExtract.exe" -f -r -p "%%A"
- )
- ) else (
- :: Regular defrag
- for %%A in (%_steamapps%\*.gcf) do "HLExtract.exe" -f -p "%%A"
- if /I [%_vpk%] == [Yes] (
- echo.
- echo GCFs done. Proceeding to VPKs.
- echo.
- for /R %_steamapps%\common\ %%A in (*.vpk) do "HLExtract.exe" -f -p "%%A"
- )
- )
- echo.
- echo ______________________________________________________________________
- echo.
- if /I [%_shut%] == [yes] call :_shutdown && endlocal && exit /b 0
- call :time Time3
- for /F "tokens=1-3 delims=:.," %%A in ("%Time%") do set Time3=%%A:%%B:%%C
- :: Steam handling
- if /I [%_kill%] == [Yes] (
- echo Defrag completed at [%Time3%] - press Enter to start Steam
- ) else (
- echo Defrag completed at [%Time3%] - press Enter to exit
- )
- echo
- set _last_time=%Time2%/%_last_time%
- reg add "HKCU\Software\i-ghost\autogcf" /v LastRun /t REG_SZ /d "%_last_time%" /f >nul
- set /p _ok=______________________________________________________________________
- if /I [%_kill%] == [Yes] (start %_bin% -silent)
- endlocal
- exit /b 0
- :write
- reg add "HKCU\Software\i-ghost\autogcf" /v Shutdown /t REG_SZ /d "%_shut%" /f >nul
- reg add "HKCU\Software\i-ghost\autogcf" /v SteamKill /t REG_SZ /d "%_kill%" /f >nul
- reg add "HKCU\Software\i-ghost\autogcf" /v Aggressive /t REG_SZ /d "%_agg%" /f >nul
- reg add "HKCU\Software\i-ghost\autogcf" /v DoVPKs /t REG_SZ /d "%_vpk%" /f >nul
- goto :EOF
- :time
- :: First param is set to time, second is set to date and is optional
- for /F "tokens=1-3 delims=:.," %%A in ("%Time%") do set %1=%%A:%%B:%%C
- if [%2] NEQ [] (
- for %%A in (%Date%) DO (
- for /F "tokens=1-3 delims=/-" %%B in ("%%~A") DO (
- SET %2=%%C.%%B.%%D
- )
- )
- )
- goto :EOF
- :getsteam
- :: Miscellaneous
- ::for /F "tokens=2* delims= " %%A in ('REG QUERY "HKLM\Software\Valve\Steam" /v SteamPID 2^>nul') do set _pid=%%B
- ::set /A _pid=%_pid% + 0
- :: Old method
- :: Stored in registry, prefer to get it via cmd
- 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
- 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
- if /I [%_pid%] == [No] set _pid=
- if /I [%_pid2%] == [tasks] set _pid2=
- goto :EOF
- :_shutdown
- call :time Time3
- echo Defrag completed at [%Time3%] - Shutting down in 10 seconds
- echo Press Enter to abort
- echo
- set _last_time=%Time2%/%_last_time%
- reg add "HKCU\Software\i-ghost\autogcf" /v LastRun /t REG_SZ /d "%_last_time%" /f >nul
- shutdown /s /t 12 /c "GCF defrag completed - Shutting down in 10 seconds"
- set /p _ok=______________________________________________________________________
- shutdown /a
- goto :EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement