Nov 27, 2008

Posted by in Tech Tips | 7 Comments

Advanced package for 7-zip

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
  1. Thank you for the script, it worked like a charm for us!

  2. Can’t make it work. I save it as a .js file and then run it. I receive a Line 1 Char 4 Error: Expected ‘;’

  3. Hello K.S
    Its a VBscript file so try changing the file extension to .vbs

    Let me know how it goes.

    Thanks,
    Ben

  4. Hi Ben and thank you for your immediate response.

    It stops at Line 5 Char 34 (Expected end of statement / Code 800A0401)

    Thank you, Kostas

  5. Hello,
    Try re-downloading it now.
    The HTML upload mangles all the & signs and replaced them with the HTML code for &

    Sorry I can’t test this now so let me know if there is anything else!

    Ben

  6. You are the one!

    Thank yo, it worked like a charm.

    p.s. My guess was that it was html code’s issue but since I am no script writing guru, I could not fix that.

    Thank you again, you rock 🙂

    Kostas

  7. Thank You a lot for a association with 7zip

Leave a Reply

Your email address will not be published. Required fields are marked *