1

Hello I've tried a lot of researching but cant find what I need and haven't been able to successfully piece this together myself.

Each of my users have a XML file within their profile that I would like to edit. The file contains a reference to their computer name and clientname, which are out of date each time they login to a new terminal. I need to replace these with the current computername and clientname. The bit I cannot figure out how to do is how to search the XML for the computername when I only know the first few characters, then replace it.

my XML will have any entry something like this "InstalledPrinter name="\WHBCVDI0109\LabelPrinter650 (from IGEL-00E0C533943E)"

I need to search the file and replace the WHBCVDI0109 and the IGEL-00E0C533943E with the correct entries. My script successfully gets those entries I just dont know how to find and replace them in the file.

My script looks like this:

Const ForReading = 1
Const ForWriting = 2

Set oShell = CreateObject( "WScript.Shell" )

'Get Variables
user=oShell.ExpandEnvironmentStrings("%UserName%")
appdata=oShell.ExpandEnvironmentStrings("%appdata%")
strComputerName = oshell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )


'Set XML location
strfile = appdata & "\Smart Label Printer\SlpUserConfig.xml"

'Open
Set objfso = CreateObject("Scripting.FileSystemObject") 
Set filetxt = objfso.OpenTextFile(strfile, ForWriting) 


strTemp = "HKEY_LOCAL_MACHINE\SOFTWARE\Citrix\ICA\Session\ClientName"
WScript.Echo "client name is : " & oShell.RegRead(strTemp)

An pointers would be much appreciated.

1 Answers1

0

You shouldn't use the FileSystemObject and String/RegExp operations to edit an XML file. Using the canonical tool - msxml2.domdocument - is less error prone and scales much better.

See here for an example (edit text); or here for another one (edit attribute).

If you publish (the relevant parts of) your .XML file, I'm willing to add same demo code specific for your use case here.

Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • The XML file will contain many lines but the ones we are concerned with look like this \\WHBCVDI0109\LabelPrinter650 (from IGEL-00E0C533943E) \\WHBCVDI0109\LabelPrinter650 (from IGEL-00E0C533943E) – stillavbsnewbie Aug 27 '15 at 16:25
  • see the 2nd example for why (XPath expression to pinpoint the material to edit) and how to publish a simplified/keeping the basic structure version of an XML file. – Ekkehard.Horner Aug 27 '15 at 16:42