Tuesday, August 17, 2010

Visual Basic script to manipulate an element in DOCICON.XML file

'Purpose: To add/update/remove the element
'into DOCICON.XML file make the installation/uninstallation of Foxit PDF iFilter complete

blnRevert = false
strAction = ""

'*** Arguments Section ***

If wscript.arguments.count = 1 Then
if lcase(wscript.arguments(0)) = "-update" OR lcase(wscript.arguments(0)) = "/update" Then
strAction = "update"
elseif lcase(wscript.arguments(0)) = "-remove" OR lcase(wscript.arguments(0)) = "/remove" Then
strAction = "remove"
else
call OutPutUsage
wscript.quit
end if
else
call OutPutUsage
wscript.quit
end if

strFileName = "C:\Program Files\Common Files\Microsoft Shared\Web server extensions\12\Template\Xml\DOCICON.XML"

Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
objXMLDoc.async = False
blnFileExists = objXMLDoc.load(strFileName)

if blnFileExists = 0 then
OutPut strFileName + " file not found."
OutPut "Script will terminate"
wscript.quit
end if

Dim objCurrNode, objNewNode, objNewText, xmlTmpNode
Set objCurrNode = objxmlDoc.selectSingleNode("/DocIcons/ByExtension")

Dim i, blnElementExist
i = 0
blnElementExist = 0

for each xmlTmpNode in objCurrNode.ChildNodes
' OutPut xmlTmpNode.xml
strAttValue = xmlTmpNode.getAttribute("Key")

if lcase(strAttValue) = "pdf" then
blnElementExist = 1
exit for
else i = i + 1
end if
next

if strAction = "remove" then

if blnElementExist = 0 then
OutPut "Key Not found. No changes will be made."
else
objCurrNode.removeChild objCurrNode.ChildNodes(i)
objXMLDoc.save(strFileName)
OutPut "Key=PDF has been removed."
end if

else
if blnElementExist = 1 then

strAttValue = ""
strAttValue = xmlTmpNode.getAttribute("Value")

if lcase(strAttValue) "icpdf.gif" then
Set objNewNode = objxmlDoc.createNode(1,"Mapping","")
objNewNode.setAttribute "Key","pdf"
objNewNode.setAttribute "Value","icpdf.gif"
objCurrNode.replaceChild objNewNode, objCurrNode.ChildNodes(i)
Set objCurrNode = objCurrNode.lastChild
objXMLDoc.save(strFileName)
OutPut "Value for Key=pdf updated to icpdf.gif"
else OutPut "Key with correct value already exists. No changes will be made"
end if

else
Set objNewNode = objxmlDoc.createNode(1,"Mapping","")
objNewNode.setAttribute "Key","pdf"
objNewNode.setAttribute "Value","icpdf.gif"
objCurrNode.appendChild(objNewNode)
Set objCurrNode = objCurrNode.lastChild
objXMLDoc.save(strFileName)
OutPut "New element created: "
end if
end if

'OutPut objXMLDoc.xml

SUB OutPutUsage
OutPut ""
OutPut " " & wscript.scriptname & " is used to update or remove into DOCICON.XML"
OutPut ""
OutPut " Usage:"
OutPut ""
Output " " & wscript.scriptname & " [-update | -remove]"
OutPut ""

END SUB

SUB OutPut(strStringToOutPut)
wscript.echo strStringToOutPut
END SUB

So, in your bat file you do this:-
cscript.exe //nologo MOSS_DOCICON_Setting.vbs /update
or
cscript.exe //nologo MOSS_DOCICON_Setting.vbs /remove

No comments:

Post a Comment