pintas Posted June 23, 2009 Share Posted June 23, 2009 (edited) I'm trying to edit this XML: <?xml version="1.0" encoding="utf-8"?> <preferences> <section id="106785699278404793101245697028"> <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value> </section> <section id="568005212148609391421245697028"> <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value> </section> <section id="689115594670726692631245697028"> <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value> </section> </preferences> And i want to replace the paths 'C:\Doc.doc' with '@ScriptDir & "\App\unite\fileSharing.us"' between <value...> and </value>, but the section ids always differ and the xml gets bigger everytime i run the program. How can i replace all the paths? I can only change the first path... $sNewData = @ScriptDir & "\App\unite\fileSharing.us" $sXMLFile = @ScriptDir & "\App\profile\widgets\widgets.dat" $sXML = FileRead($sXMLFile) $avData = _StringBetween($sXML, '<value id="path to widget data" xml:space="preserve">', '</value>') $sXML = StringReplace($sXML, '<value id="path to widget data" xml:space="preserve">' & $avData[0] & '</value>', '<value id="path to widget data" xml:space="preserve">' & $sNewData & '</value>') $hXMLFile = FileOpen($sXMLFile, 2) ; 2 = Overwrite FileWrite($hXMLFile, $sXML) Help please? Edited June 23, 2009 by pintas Link to comment Share on other sites More sharing options...
Xenobiologist Posted June 23, 2009 Share Posted June 23, 2009 Hi, why search for the part before and behind? Couldn't you just use this pattern? >(\w.+?)</value> with the function StringRegReplace ? Mega Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
pintas Posted June 23, 2009 Author Share Posted June 23, 2009 (edited) But wouldn't the problem remain that way? I couldn't find any help in the help file, could you post a small example please? Oh, and btw, the path C:\Doc.doc is a variable... :S I just want to replace all the strings between <value...> and </value> with the same string (@ScriptDir & "\App\unite\fileSharing.us") Thanks for your reply Edited June 23, 2009 by pintas Link to comment Share on other sites More sharing options...
Xenobiologist Posted June 23, 2009 Share Posted June 23, 2009 Global $newStr = StringReplace(@ScriptDir & "\App\unite\fileSharing.us", '\', '\\') Global $str = '<?xml version="1.0" encoding="utf-8"?>' & @CRLF & _ '<preferences>' & @CRLF & _ ' <section id="106785699278404793101245697028">' & @CRLF & _ ' <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>' & @CRLF & _ ' </section>' & @CRLF & _ ' <section id="568005212148609391421245697028">' & @CRLF & _ ' <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>' & @CRLF & _ ' </section>' & @CRLF & _ ' <section id="689115594670726692631245697028">' & @CRLF & _ ' <value id="path to widget data" xml:space="preserve">C:\Doc.doc</value>' & @CRLF & _ ' </section>' & @CRLF & _ '</preferences>' ConsoleWrite(StringRegExpReplace($str, '">(\w.+?)</value>', $newStr) & @CRLF) Scripts & functions Organize Includes Let Scite organize the include files Yahtzee The game "Yahtzee" (Kniffel, DiceLion) LoginWrapper Secure scripts by adding a query (authentication) _RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...) Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc. MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times Link to comment Share on other sites More sharing options...
pintas Posted June 23, 2009 Author Share Posted June 23, 2009 Thank you. That is a big help but as i said in the first post, the xml grows everytime i launch the program so, there will be more than three replacements. There can be hundreds, and that's where my problem lies... How do i change all the document? Anyway to use a 'For' or someway to find and replace? Thanks again. Link to comment Share on other sites More sharing options...
99ojo Posted June 23, 2009 Share Posted June 23, 2009 Thank you. That is a big help but as i said in the first post, the xml grows everytime i launch the program so, there will be more than three replacements. There can be hundreds, and that's where my problem lies...How do i change all the document? Anyway to use a 'For' or someway to find and replace?Thanks again.Hi,doesn't a simple _ReplaceStringInFile the job you want?;-))Stefan Link to comment Share on other sites More sharing options...
pintas Posted June 23, 2009 Author Share Posted June 23, 2009 (edited) LOL! You know... it probably does it Why do we humans tend to complicate so much? Thanks man. I'll give it a try and come right back to post the (successfull) results. Edited June 23, 2009 by pintas Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted June 23, 2009 Moderators Share Posted June 23, 2009 (edited) Thank you. That is a big help but as i said in the first post, the xml grows everytime i launch the program so, there will be more than three replacements. There can be hundreds, and that's where my problem lies... How do i change all the document? Anyway to use a 'For' or someway to find and replace? Thanks again.Global $sNewData = StringReplace(@ScriptDir & "\App\unite\fileSharing.us", "\", "\\") ; Have to escape back slashes for regexreplace Global $sXMLFile = @ScriptDir & "\App\profile\widgets\widgets.dat" Global $sXML = FileRead($sXMLFile) Global $sStart = '<value id="path to widget data" xml:space="preserve">' Global $sEnd = "</value>" $sXML = StringRegExpReplace($sXML, "(?i)(" & $sStart & ")(.*?)(" & $sEnd & ")", "$1" & $sNewData & "$3") ; Will replace all matches in file FileClose(FileOpen($sXMLFile, 2)) FileWrite($sXMLFile, $sXML) Edit: Didn't even read xeno's! ... his is pretty much the same as mine, just a different approach. Edited June 23, 2009 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
pintas Posted June 23, 2009 Author Share Posted June 23, 2009 (edited) Done it! $path = @ScriptDir & "\App" $filename = @ScriptDir & "\App\profile\widgets\widgets.dat" $find = IniRead(@ScriptDir & '\Data\pathupdate.ini', "PathUpdate", "Old","") $replace = $path FileWrite($filename, $path) $retval = _ReplaceStringInFile($filename,$find,$replace) Thanks guys for your help. This one is solved! Edited June 23, 2009 by pintas Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now