Hi Gregster,
Thanks for taking the time to look at it. It's probably obvious, but I've only been using AHK for a couple of months now and have no previous programming experience, so I apologize for stupid questions/mistakes.
I did find another way to accomplish what I need for this task, however I would really like to understand how to use xpath correctly. I corrected a few things... I think. The variable feed was wrong, like you said. I copied that line from the example without understanding that it was a variable.
I didn't understand what you meant exactly by wrong order of parameters. I looked the reference included with xpath, this thread and my code for about 6 hours yesterday trying different things. Hopefully I didn't screw it up more.
Here's my current solution that works for me right now, but I'd like to use xpath for future projects. Maybe it will be useful to others for small xml files without similar entries.
Code:
#NoTrayIcon
Loop, %A_AppData%\Pandion\Profiles\settings.xml, , 1 ; Recurse into subfolders.
{
FileRead, incomingfile, %A_LoopFileFullPath%
StringReplace, outgoingfile, incomingfile, <history_store>false</history_store>, <history_store>true</history_store>,
FileDelete, %A_LoopFileFullPath%
FileAppend, %outgoingfile%, %A_LoopFileFullPath%
Here's my current xpath script that's broken
Code:
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#include xpath.ahk
Loop, %A_AppData%\Pandion\Profiles\settings.xml, , 1 ; Recurse into subfolders.
{
xpath_load(xmlfile, %A_LoopFileFullPath%)
xpath(xmlfile, "settings/history_store/text() ", "true")
MsgBox, 4, , Filename = %A_LoopFileFullPath%`n`nContinue?
IfMsgBox, No
break
xpath_save(%A_LoopFileFullPath%, xmlfile) ; write XML
}
And here's the error
---------------------------
PandionUserUpdateXpath.ahk
---------------------------
Error: The following variable name contains an illegal character:
"\\server\RedirectedFolders\user\Application Data\Pandion\Profiles\admin@chat.domain.local\settings.xml"
The current thread will exit.
Line#
448: }
449: StringReplace,doc,xml,,,,,All
450: NumPut(0, doc := " " . doc, 0, "UChar")
451: Return,true
452: }
011: Loop,%A_AppData%\Pandion\Profiles\settings.xml,,1
013: {
---> 014: xpath_load(xmlfile, %A_LoopFileFullPath%)
015: xpath(xmlfile, "settings/history_store/text() ", "true")
019: MsgBox,4,,Filename = %A_LoopFileFullPath%
Continue?
020: IfMsgBox,No
021: Break
024: xpath_save(%A_LoopFileFullPath%, xmlfile)
026: }
026: Exit
---------------------------
OK
---------------------------
If I take both of the % out of line 14 I get this. It looks like it gets past the loading file portion, but I'm not sure that it's storing it in the variable xmlfile. If I view variables at the point where it displays the dialog box I get this.
Global Variables (alphabetical)
--------------------------------------------------
0[1 of 3]: 0
ErrorLevel[1 of 3]: 0
xmlfile[4143 of 4556]: ...
Here's the output from AHK
Code:
Script lines most recently executed (oldest first). Press [F5] to refresh. The seconds elapsed between a line and the one after it is in parentheses to the right (if not 0). The bottommost line's elapsed time is the number of seconds since it executed.
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
444: pre = %pre%%tag%/
445: xml = %xml%<%pre%:: %ex%
446: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
418: if (A_Index == 2 && InStr(A_LoopField, "?xml") == 1)
420: if (InStr(A_LoopField, "?") == 1)
422: if (InStr(A_LoopField, "![CDATA[") == 1)
431: if (!pos := RegExMatch(A_LoopField, "^\/?(?:\w+:)?\w+", tag))
437: StringMid,ex,A_LoopField,pos + StrLen(tag)
438: if InStr(tag, "/") = 1
439: xml = %xml%</%pre%%ex%
440: StringGetPos,pos,pre,/,R2
441: StringLeft,pre,pre,pos + 1
442: }
447: }
448: }
449: StringReplace,doc,xml,,,,,All
450: NumPut(0, doc := " " . doc, 0, "UChar")
451: Return,true
015: xpath(xmlfile, "settings/history_store/text() ", "true")
035: if step contains %scb%
037: sid := scb . &doc . ":"
038: if (InStr(step, "select:") == 1)
042: if (InStr(step, "/") == 1)
045: StringGetPos,p,sc,%sid%
046: if ErrorLevel = 1
047: t = /
049: step = %t%/%step%
050: }
053: step := RegExReplace(step, "(?<=^|/)(?:[^\[/@]+/\.{2}|\.)(?:/|$)|^.+(?=//)")
055: if (str == 1 || stsl == 1)
068: if (InStr(step, "|"))
074: if (InStr(step, "//") == 1)
075: StringTrimLeft,step,step,2
076: re = 1
077: rew = 1
078: xp = /(?:(?:\w+:)?\w+)*/
079: }
081: NumPut(160, doc, 0, "UChar")
084: Loop,Parse,step,/
086: s = %A_LoopField%
087: if (InStr(s, "*") == 1)
092: if (InStr(s, "@") == 1)
096: StringGetPos,p,s,[
097: if ErrorLevel = 0
147: if (InStr(s, "()"))
152: xp = %xp%%s%/
153: }
086: s = %A_LoopField%
087: if (InStr(s, "*") == 1)
092: if (InStr(s, "@") == 1)
096: StringGetPos,p,s,[
097: if ErrorLevel = 0
147: if (InStr(s, "()"))
152: xp = %xp%%s%/
153: }
086: s = %A_LoopField%
087: if (InStr(s, "*") == 1)
092: if (InStr(s, "@") == 1)
096: StringGetPos,p,s,[
097: if ErrorLevel = 0
147: if (InStr(s, "()"))
148: fn = %fn%+%s%
149: Continue
155: if (xp == "" || xp == "/")
159: StringTrimLeft,xp,xp,1
160: StringTrimRight,ax,ax,1
162: StringTrimRight,nw,nw,1
164: ct = 0
165: os = 0
166: Loop
168: if re
169: os := RegExMatch(doc, "<" . xp . ":: ", "", 1 + os), osx := RegExMatch(doc, "</" . xp . ">", rem, os) + StrLen(rem)
175: if os = 0
176: Break
300: if setb <>
321: if (InStr(fn, "+count()"))
324: nsid := scb . &doc . ":"
325: if nsid <> %sid%
328: NumPut(0, doc, 0, "UChar")
329: StringTrimRight,res,res,1
330: if (InStr(fn, "+rawsrc()"))
338: Return,RegExReplace(res, "S)(?<=<)(\/)?(?:(\w+)\/)+(?(1)|:: )", "$1$2")
019: MsgBox,4,,Filename = %A_LoopFileFullPath% Contents = %xmlfile%
Continue? (47.72)
020: IfMsgBox,No
024: xpath_save(%A_LoopFileFullPath%, xmlfile) (17.67)
Press [F5] to refresh.