0

I've written a vbscript that injects a new Group and User into FileZilla Server.xml from a template - replacing group name and user name with a parameter... restart the service and hey presto this appears within filezilla server as a new account/assigned folders...

Just simple opentextfile, read until < /GROUPS > or < /USERS >, inject the code, save to a new file name. Works fine.

I've then written some code to remove these Group and User elements if it should be required... using XML DOM. It works fine, it does the job... however, when I then come to insert a new record (as above), it cannot write to the XML file as it can no longer locate < /GROUPS > or < /USERS >... I can add wscript.echo to display the lines that are being processed, these elements appear... but are not actioned, like it cannot see them.

Is there anything wrong with how the XML file is saved? It appears fine in notepad. Anythign related to UTF or ASCII ? I'm wondering whether to remove the elements akin to how I'm adding them... read until a point then skip over the lines while writing them out to another file.

Code: checking for the element (also tried Instr, and this couldn't find it after being updated by XMLDOM Save.

Do Until f1.AtEndOfStream
    strLine = f1.ReadLine
    If trim(strLine) = "</Groups>" Then
        If GroupExists="False" Then
            Set f2 = objFSO.OpenTextFile(templatepath&filetemplate,1)

. . . . .

Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 
objXMLDoc.load(filezillapath&filezilla)

Set objRoot = objXMLDoc.documentElement 
Set objLst  = objXMLDoc.selectSingleNode("//Groups/Group[@Name='" & strNewAccount & "']")
If objLst.Length > 0 Then
    objLst.parentNode.removeChild(objLst)
    objXMLDoc.Save(filezillapath&filezilla)
End If

Set objRoot = objXMLDoc.documentElement 
Set objLst  = objXMLDoc.selectSingleNode("//Users/User[@Name='" & strNewAccount & "']")
'If objLst.Length > 0 Then
    objLst.parentNode.removeChild(objLst)
    objXMLDoc.Save(filezillapath&filezilla)
'End If

Set objXMLDoc = Nothing
1
  • Check if you got parse errors after loading the XML (If objXMLDoc.parseError <> 0 Then WScript.Echo objXMLDoc.parseError.reason). For further help you need to show relevant excerpts from the file before and after the modification. Commented Mar 15, 2014 at 13:08

1 Answer 1

1

Your

Set objLst  = objXMLDoc.selectSingleNode("//Groups/Group[@Name='" & strNewAccount & "']")
If objLst.Length > 0 Then

looks fishy. According to the docs (cf. this too), selectSingleNode() returns a node or Nothing. So try

Set ndX = objXMLDoc.selectSingleNode(...)
If ndX Is Nothing Then
   message
Else
   action
End If

BTW:

  • don't use // if /root/where/ever works
  • VBScript isn't Lisp - no param list () when calling a Sub
Sign up to request clarification or add additional context in comments.

4 Comments

Not being familiar with navigating XML I googled and tried numerous methods to get the delete to work, and the code works (albeit I note your comment regarding the .Length > 0)... the elements do disappear from the XML, taking all the child nodes with it. However, its the fact that viewing this "text file" it all appears correct, but then opening it as text to process it... it cannot find specfic data with in it, even thought it is there.
@user3421379 Show, don't tell. What do the Users and Groups sections in your XML file look like before you remove the user, and what do they look like afterwards?
a very large filezilla server.xml file.
a very large filezilla server.xml file. Before and after the XML DOM Save has done its deed, the XML is well formed as it loads in to Explorer, and also into File Zilla server (after a service stop start). It's just when I come to edit the file again that nodes are not findable when I opentextfile on it to find a node when I read each line... they were findable prior to the xml dom, on an unedited filezilla server xml file. (battery on my laptop expired prior to me finishing the previous entry)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.