9 posts in this topic
If we give the below line in our script, we can expand the variables in double quotes to its values.
AutoItSetOption ("ExpandVarStrings", 1) Like,
Local $b=4 MsgBox(0,"$b$","See subject") But coming to variables, they are not getting expanded.
Local $a=[1,2,3] Local $b=4 _ArrayDisplay($a) MsgBox(0,"$a$$b$","See subject") Could anyone please help me on this.
I am currently doing a project involving keeping some parameters in an internal 2d array. I am reading part of that to and from an ini file.
As part of the reading I need to put the variables from the ini file into the 2D array. I have found _arrayinsert(), but that creates additional lines.
Bascially I would like something as this:
 a1 a2 a3 a4  b1 b2 b3 b4  c1 c2 c3 c4 And reading setting for "2" being d1 d2 d3 d4 I should now have
 a1 a2 a3 a4  d1 d2 d3 d4  c1 c2 c3 c4 Is there a built-in UDF or similar to do something like that already ? Or did I missing something real obvious ?
I have rolled my own code, which works. But of course it could be improved.
Thanks for all comments, happy to learn from you all !
Func __ReplaceStringInFile($szFileName, $szSearchString, $szReplaceString, $fCaseness = 0, $fOccurance = 1) Local $iRetVal = 0 Local $nCount, $sEndsWith ; Check if file is readonly .. If StringInStr(FileGetAttrib($szFileName), "R") Then Return SetError(6, 0, -1) ;=============================================================================== ;== Read the file into an array ;=============================================================================== Local $hFile = FileOpen($szFileName, $FO_READ) If $hFile = -1 Then Return SetError(1, 0, -1) Local $s_TotFile = FileRead($hFile, FileGetSize($szFileName)) If StringRight($s_TotFile, 2) = @CRLF Then $sEndsWith = @CRLF ElseIf StringRight($s_TotFile, 1) = @CR Then $sEndsWith = @CR ElseIf StringRight($s_TotFile, 1) = @LF Then $sEndsWith = @LF Else $sEndsWith = "" EndIf Local $aFileLines = StringSplit(StringStripCR($s_TotFile), @LF) FileClose($hFile) ;=============================================================================== ;== Open the output file in write mode ;=============================================================================== Local $iEncoding = FileGetEncoding($szFileName) Local $hWriteHandle = FileOpen($szFileName, $iEncoding + $FO_OVERWRITE) If $hWriteHandle = -1 Then Return SetError(2, 0, -1) ;=============================================================================== ;== Loop through the array and search for $szSearchString ;=============================================================================== For $nCount = 1 To $aFileLines If StringInStr($aFileLines[$nCount], $szSearchString, $fCaseness) Then $iRetVal = $iRetVal + 1 If $fOccurance = $iRetVal Then $aFileLines[$nCount] = StringReplace($aFileLines[$nCount], $szSearchString, $szReplaceString, 1 - $fOccurance, $fCaseness) ExitLoop EndIf EndIf Next ;=============================================================================== ;== Write the lines back to original file. ;=============================================================================== FileClose($hWriteHandle) Local $Tempfile = _TempFile() FileWrite($Tempfile, _ArrayToString($aFileLines, @LF, 1)) FileMove($Tempfile, $szFileName, 1) Return $iRetVal EndFunc ;==>__ReplaceStringInFile Yes, I know guinness improved the ReplaceStringInFile function in terms of speed and efficiency but hear me out. I made some changes to the original myself that increased the speed of the writing and more importantly for me change the occurrence option so the function would only replace that specific occurrence of string.
I not saying use my code. What I am suggesting is to add extra functionally to the ReplaceStringInFile so there is a option to replace a specific occurrence.
I could not think of a way to do this with the new one guinness made so above is just an example of how I did it with the old one.
I have also attached the file.au3 file.
I know there are many threads on this argument, but i'm searching the fastest method to replace some text in a file:
Local $hFile = "Test.txt" $sData = "FIRSTNAME Lastname - BAT _ TIME1" & @CRLF & _ "FIRSTNAME Lastname - BAT _ TIME2" & @CRLF & _ "FIRSTNAME Lastname - BAT _ TIME3" FileWrite($hFile, $sData) FileClose($hFile) $hFileOpen = FileOpen($hFile) $ReadData = FileRead($hFileOpen) $sNewFile = StringRegExpReplace($ReadData, '(?i:' & "TIME" & ')', "COOL") ;~ $sNewFile = StringRegExpReplace($sData, "(-)(.*)(_)", "Another Text") FileClose($hFile) FileOpen($hFile, 2) FileWrite($hFile, $sNewFile) FileClose($hFile)
1) This is the fastest method, using StringRegExpReplace?
2) How to combine 2 or more StringRegExpReplace with different pattern ( i'm totally new to this )?
I have 2 basic question about hex editing
- I have several texts with a common problem, in stead of having CRLF (carriage return+line feed) only had 0x0A (LF), how do I add 0D behind every 0x0A?
- Its possible to edit only the beginning of a file (speaking in large files that should't be loaded in memory) and save the changes?