ViciousXUSMC Posted October 6, 2015 Share Posted October 6, 2015 I was working on something last night and decided to use StringRegExpReplace() for a config file, I never noticed that you cant just "overwrite" the file with the update so easily it required a few more pieces of code to work properly.Is this the simplest way (what I used) and while I searched for it and did not find it do we have or will we have a RegEx equivalent for _ReplaceStringInFile()?$sFile = FileRead(@ScriptDir & "\test.txt") $hFile = FileOpen(@ScriptDir & "\test.txt", 2) $sNewContent = StringRegExpReplace($sFile, "(test)", "new$1") FileWrite($hFile, $sNewContent) FileClose($hFile) Link to comment Share on other sites More sharing options...
mikell Posted October 6, 2015 Share Posted October 6, 2015 The simplest way IS the current _ReplaceStringInFile() , which uses a StringReplace along with all the needed error checkings and optionsUsing a regex would not make it simpler Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted October 6, 2015 Author Share Posted October 6, 2015 What I am asking is if you need regex, is what I posted above account for the simplest way to accomplish the same task but with regex capability. My config file did not have a static string, it has varying length and values so I needed regex to ensure capture/replacement (IP Address + NIC Adapter) Link to comment Share on other sites More sharing options...
mikell Posted October 6, 2015 Share Posted October 6, 2015 Ah, now I understand In this case, yes your code seems to be the most proper way... read file, then regex, then write file, could you do it otherwise ? Link to comment Share on other sites More sharing options...
ViciousXUSMC Posted October 6, 2015 Author Share Posted October 6, 2015 Just making sure there was not a function I did not know about or some tip/trick.I did not like the idea of having to run FileRead() and FileOpen() feels redundant. Link to comment Share on other sites More sharing options...
mikell Posted October 6, 2015 Share Posted October 6, 2015 It's not redundant !Did you have a look at the _ReplaceStringInFile func in File.au3 ? if you open a file in read mode (as FileRead does) you must close it and reopen to write inside it 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