In the previous post I covered how you could create a basic package for 7-zip to deploy out to users, however this does not do some basic things such as register file type associations in windows… I have since created a vbscript “wrapper” that does the following:

  • sets the install directory
  • runs the 7-zip silent install
  • checks the install returned OK
  • reads the file types to associate with 7-zip
  • removed any associations that already exist for those file types
  • create the new association with 7-zip
  • create the folder icon and 7-zip file open actions
  • returns 0 if succeeded – useful if deploying via SCCM

The code should be posted below and includes comments for each of the sections. The code is a little rough around the edges but does work on the handful of systems (XP, x86) I have tested it on. Note that the icon for file association will not appear until after a reboot but it does work immediately after running.

Code:

on error resume next
Set objShell = CreateObject("WScript.Shell")

installpath = "C:\Program Files\7-Zip\"
install = "7z457.exe /S /D=" & chr(34) & installpath & chr(34)

'run the install command
returned = objShell.run(install,1,true)

'continue to file association of completed install
if returned = 0 then

	'specify the extensions to register with the program
	extensionlist = ".7z,.iso,.zip"

	'create an array with the extenstions from above
	extensionarray = split(extensionlist,",",-1,1)
	err.clear

	'for each extension do
	For Each CurrentExt In extensionarray
		'delete current file handler
		deletefiletype(CurrentExt)
		'add 7zip association
		objShell.RegWrite "HKCR\" & CurrentExt & "\" ,"7-Zip.7z"
	Next

	'now create the actuall association
	objShell.RegWrite "HKCR\7-Zip.7z\" , "7z Archive"

	'create icon
	iconpath = installpath & "7z.dll,0"
	objShell.RegWrite "HKCR\7-Zip.7z\DefaultIcon\" , iconpath

	'create shell action
	opencmd = chr(34) & installpath & "7zFM.exe" & chr(34) & " " & chr(34) & "%1" & chr(34)
	objShell.RegWrite "HKCR\7-Zip.7z\shell\open\command\", opencmd

	'check if err.number has been set, if so return failed
	if err.number = 0 then
		exitcode = 0
	else
		exitcode = 1
	end if
else
	exitcode = 1
end if

'exit and return code
wscript.quit(exitcode)

sub deletefiletype(filetype)
	'set variables for registry access with WMI
	Const HKEY_CLASSES_ROOT = &H80000000
	computer = "."

	'create connection to registry
	Set objReg = GetObject("winmgmts:\\" & computer & "\root\default:StdRegProv")

	'read the key that exists
	objReg.EnumKey HKEY_CLASSES_ROOT,filetype,SubKeyArray

	'if key has subkeys then recurse
	If IsArray(SubKeyArray) Then
		For Each SubKey In SubKeyArray
			deletefiletype filetype & "\" & subkey
		Next
	End If

	'otherwise delete the key
	objReg.DeleteKey HKEY_CLASSES_ROOT,filetype
end sub

wscript.quit