Left 4 Dead 2

Left 4 Dead 2

Not enough ratings
VTFEdit "Open with" context menu entries script
By Urik
Batch scripts to add/remove context menu entries for VTFEdit
   
Award
Favorite
Favorited
Unfavorite
Description
A batch script to add/remove custom "Open in VTFEdit" context menu entry

  • Must be run as administrator
  • Looks for vtfedit.exe in default installation folder, and also in same folder script is located
  • If no installation is found, will ask for custom folder
  • asks which extensions to add for (default dds,jpg,png,psd,gta)
  • removal script for removing the entries


Pastebin


open_in_vtfedit_add[pastebin.com]
open_in_vtfedit_remove[pastebin.com]
open_in_vtfedit_add.bat (raw paste data)

@echo off
setlocal enabledelayedexpansion
title Add VTFEdit context menu entries

set default_extensions=dds,jpg,png,psd,tga

:check_elevation
net session >nul 2>&1
if not %errorlevel%==0 echo  & echo Error: Must run as administrator. & pause>nul & goto :eof

cd /d "%~dp0"

set foundcount=0
:check_default_location
if exist "%programfiles%\Nem's Tools\VTFEdit\vtfedit.exe" CALL :LogFoundFolder "%programfiles%\Nem's Tools\VTFEdit\VTFEdit.exe"

:check sourcesdk binaries
if exist "..\..\script_files\binaries\vtfedit.exe" ( cd..\.. & CALL :LogFoundFolder "!cd!\script_files\binaries\VTFEdit.exe")

:check nearby
if exist "%~dp0vtfedit.exe" ( CALL :LogFoundFolder "%~dp0VTFEdit.exe")

if !foundcount!==0 echo Could not auto-find any installations. & goto :hintcustomfolder
if !foundcount!==1 goto :ConfirmFolder
FOR /L %%F IN (1,1,%foundcount%) DO ( if ""!folderidlist!=="" ( set folderidlist=%%F) else ( set folderidlist=!folderidlist!,%%F))

:SelectFoundFolder
echo.
echo please choose folder ^( !folderidlist! or custom^)
FOR /L %%F IN (1,1,%foundcount%) DO ( echo %%F !foundpath%%F!)
echo.
set folderid=
set /p folderid="enter number !folderidlist! or word ^"custom^" = "
if not "!folderid!"=="" (
set "folderid=!folderid: =!"
for %%A in (!folderid!) do ( if /i "%%~A"=="custom" goto :hintcustomfolder)
for /f "delims=1234567890" %%A in ('echo !folderid!') do echo ERROR: !folderid! - only numbers are allowed^^! & goto :SelectFoundFolder
for %%A in (%folderidlist%) do if "!folderid!"=="%%A" (
set "installpath=!foundpath%%A!"
echo. & echo Selected folder: !installpath!
goto :hintextensions
)
echo ERROR: !folderid! is NOT a valid option^^!
)
goto :SelectFoundFolder

:hintcustomfolder
echo.
echo Please enter VTFEdit folder
echo.
:AskCustomFolder
set installpath=
set /p installpath="Enter VTFEdit.exe folder "
for %%P in (%installpath%) do goto :next
goto :AskCustomFolder
:next
set installpath=%installpath:"=%
if "%installpath:~-1%"=="\" set "installpath=%installpath:~0,-1%"
if "%installpath:~-1%"=="/" set "installpath=%installpath:~0,-1%"
for %%P in (%installpath%) do (
if exist "%installpath%" (
if not "%%~xP"=="" if /i "%%~nxP"=="VTFEdit.exe" ( CALL :SetCustomFolder "%installpath%" & goto :ConfirmFolder)
if exist "%installpath%\VTFEdit.exe" ( CALL :SetCustomFolder "%installpath%\VTFEdit.exe" & goto :ConfirmFolder)
)
)
echo  & echo Error^: Could not find installation in provided folder. & echo. & goto :AskCustomFolder

:hintextensions
echo.
echo Please enter file extensions to add, divided by comma, lowercase, no dots or just press enter for default list
echo.
:askextensions
set /p extensions="What extensions to add for (default %default_extensions%): "
if not "!extensions!"=="" (
set "extensions=!extensions: =!"
for /f "tokens=1 delims=abcdefghijklmnopqrstuvwxyz1234567890," %%A in ('echo !extensions!') do (
set "extensions=%default_extensions%"
echo Warning: input incorrect, will use default setting ^(%default_extensions%^)
pause
)
) else ( set "extensions=%default_extensions%" )

:: exec for all extensions
for %%E in (%extensions%) do CALL :SetKeys %%E

:: results
echo.
echo -------------------------------------------------------
if "!processedcount!"=="" ( echo No entries added ) else ( echo !processedcount! entries added: !processed! )
echo -------------------------------------------------------
echo.
pause & goto :eof


:SetKeys
reg add "HKCR\SystemFileAssociations\.%1\shell\VTFEdit.open" /f /v "" /t REG_SZ /d "Open in VTFEdit">nul 2>&1
reg add "HKCR\SystemFileAssociations\.%1\shell\VTFEdit.open" /f /v "Icon" /t REG_SZ /d "\"%installpath%\",0">nul 2>&1
reg add "HKCR/SystemFileAssociations/.%1/shell/VTFEdit.open/command" /f /v "" /t REG_SZ /d "\"%installpath%\" \"%%1\"">nul 2>&1
if !errorlevel!==0 CALL :LogFileType %1
goto :eof

:LogFileType
if "!processedcount!"=="" ( set "processedcount=1" ) else ( set /a "processedcount=!processedcount!+1" )
if "!processed!"=="" ( set "processed=%1" ) else ( set "processed=!processed!,%1" )
goto :eof

:LogFoundFolder
set /a foundcount=!foundcount!+1
echo.
echo VTFEdit installation found: "%~1"
set "foundpath!foundcount!=%~1"
goto :eof

:SetCustomFolder
echo.
echo VTFEdit custom installation: "%~1" & echo.
set "foundpath1=%~1"
goto :eof

:ConfirmFolder
set proceed=
set /p "proceed=Use this installation (Y/N)?"
if not "!proceed!"=="" (
set "proceed=!proceed: =!"
for /f "delims=yYnN" %%A in ('echo !proceed!') do goto :ConfirmFolder
)
if /i !proceed!==y ( set "installpath=%foundpath1%" & goto :hintextensions)
if /i !proceed!==n ( echo. & goto :hintcustomfolder)
goto :ConfirmFolder
open_in_vtfedit_remove.bat (raw paste data)

@echo off
setlocal enabledelayedexpansion
title Remove VTFEdit context menu entries

set default_extensions=jpg,jpeg,png,bmp,tga,tiff,psd,gif,eps,webp,bpg,cd5,cpt,kra,mdp,pdn,psp,sai,xcf

:check_elevation
net session >nul 2>&1
if not %errorlevel%==0 echo  & echo Error: Must run as administrator. & pause>nul & goto :eof

:: exec for all extensions
for %%E in (%default_extensions%) do CALL :CheckKey %%E

if "%found%"=="" ( echo  & echo No entries to remove^^! Exiting.& pause>nul & goto :eof) else ( echo Found entries for^: %found%)

:hintextensions
echo.
echo Please enter file extensions to remove, divided by comma, lowercase, no dots or just press enter for remove ALL
echo.
:askextensions
set /p extensions="What extensions to delete for (default %found%): "
if not "!extensions!"=="" (
set "extensions=!extensions: =!"
for /f "tokens=1 delims=abcdefghijklmnopqrstuvwxyz1234567890," %%A in ('echo !extensions!') do (
set "extensions=%default_extensions%"
echo Warning: input incorrect, will use default setting ^(%found%^)
pause
)
) else ( set "extensions=%found%" )

:: exec for all extensions
for %%E in (%extensions%) do CALL :DelKey %%E

:: results
echo.
echo -------------------------------------------------------
if "!processedcount!"=="" ( echo No entries to remove ) else ( echo !processedcount! entries removed: !processed! )
echo -------------------------------------------------------
echo.
pause & goto :eof

:CheckKey
reg query "HKCR\SystemFileAssociations\.%1\shell\VTFEdit.open">nul 2>&1 && CALL :LogExtFound %1
goto :eof

:DelKey
reg delete "HKCR\SystemFileAssociations\.%1\shell\VTFEdit.open" /f>nul 2>&1 && CALL :LogExtRemoved %1
goto :eof

:LogExtRemoved
if "!processedcount!"=="" ( set "processedcount=1" ) else ( set /a "processedcount=!processedcount!+1" )
if "!processed!"=="" ( set "processed=%1" ) else ( set "processed=!processed!,%1" )
goto :eof

:LogExtFound
if "!foundcount!"=="" ( set "foundcount=1" ) else ( set /a "foundcount=!foundcount!+1" )
if "!found!"=="" ( set "found=%1" ) else ( set "found=!found!,%1" )
goto :eof
Additional notes
You don't have to read this but it's merely extra notes just in case.

The script does not limit user input in terms of what file extensions to add entries for.
So if you add them for extensions that aren't in %default_extensions% variable of open_in_vtfedit_remove script, then you'll have to enter those extensions when running remove_open_in_vtfedit, otherwise it won't remove them by default selection.
You can also modify the default_extensions variable inside the removal script to include those extra extensions.

Finally, as last resort, you can always do it yourself, by opening regedit, going to
HKEY_CLASSES_ROOT\SystemFileAssociations
navigating to given extension key and deleting VTFEdit.open subkey manually.
Example:
HKEY_CLASSES_ROOT/SystemFileAssociations/.jpg/Shell/VTFEdit.open
- that would be the entry for jpg extension