Jump to content

Recommended Posts

Posted

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)

 

Posted

The simplest way IS the current _ReplaceStringInFile() , which uses a StringReplace along with all the needed error checkings and options
Using a regex would not make it simpler  :)

Posted

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)

Posted

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 ?

Posted

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...