Sign in to follow this  
Followers 0
mechh

reading lines & replace tokens

6 posts in this topic

Hi people im new to autoit and still learning this very easy yet powerful language.

part of the script im writing requires reading a file that "may" contain LOTS of white spaces inbetween string tokens which then will be stringreplaced if token is found.

Im using filereadline -> if string found -> stringreplace -> filewriteline.

This has the problem that if 2 string tokens im searching for are in the same line then 1 will be ignored.

maybe this can be done by a while loop? can anyone shed some experience on this thanks in advance.

another way maybe clear all white spaces in the files with @CRLF , write it to a temp file before doing the above?

Share this post


Link to post
Share on other sites



Welcome mechh,

Do you have the script available so it can be improved on.

Share this post


Link to post
Share on other sites

There's a function called StringStripWS (string strip white space) that can do the white-space stripping you need.

It takes in a string and a flag. The flag values can be: 1 (strip leading white space), 2 (strip trailing white space), 4 (strip double or more spaces between words), 8 (strip all spaces). 8 over-rides all other flags.

Share this post


Link to post
Share on other sites

#include <file.au3>
Local $FilePath = FileOpenDialog('Find File To Parse', @ScriptDir, 'All Files (*.*)')
Local $OutFile = StringTrimRight($FilePath, 4) & '_OutFile.txt'; will put the outfile in the same directory as the infile
Local $nArray = ''
Local $StringOne = 'string to replace one'
Local $StringTwo = 'string to replace two'
Local $ReplaceOne = 'replace string one'
Local $ReplaceTwo = 'replace string two'
_FileReadToArray($FilePath, $nArray)

FindReplaceStrings($nArray, $OutFile, $StringOne, $StringTwo, $ReplaceOne, $ReplaceTwo); if your strings and replace strings get larger, try doing an array with them

Func FindReplaceStrings(ByRef $nArray, $o_OutFile, $s_StringOne, $s_StringTwo, $r_ReplaceOne, $r_ReplaceTwo)
    For $i = 1 To UBound($nArray) - 1
        If StringInStr($nArray[$i], $s_StringOne) Then
            $nArray[$i] = StringReplace($nArray[$i], $s_StringOne, $r_ReplaceOne)
        EndIf
        If StringInStr($nArray[$i], $s_StringTwo) Then
            $nArray[$i] = StringReplace($nArray[$i], $s_StringTwo, $r_ReplaceTwo)
        EndIf
        FileWriteLine($o_OutFile, $nArray[$i])
    Next
EndFunc


[center]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.[/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Hi guys thanks for the replies~

This is a simplified version with a fixed file link. using an array.

#include <file.au3>
Dim $aRecords
If Not _FileReadToArray("409.xml",$aRecords) Then
   MsgBox(4096,"Error", " Error reading file to Array error:" & @error)
   Exit
EndIf
For $x = 1 to $aRecords[0]
;Msgbox(0,'Record:' & $x, $aRecords[$x])
    If StringInStr($aRecords[$x], "string1") Then
    $aRecords[$x] = StringReplace($aRecords[$x], "string1, "newstring1")
    Endif
    If StringInStr($aRecords[$x], "string2") Then
    $aRecords[$x] = StringReplace($aRecords[$x], "string2","newstring2")
    Endif
; more strings to find here. etc etc
Next

_FileWriteFromArray("409out.xml",$aRecords,1)

The problem is that _FileReadToArray reads a line at a time. eg, if string1 and string2 etc are all in line 28 separated by white spaces.

Line28: string1   string2                   string3string4 string5  string6 etc etc etc

Thus, if i read $aRecords[$x] stringreplace will only replace the first occurance in the line and not the remaining strings which maybe in the for loop. It looks like a simple while loop solution but i need someone to englighten me on this , thanks in advance!

Edited by mechh

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Stringreplace will replace 'all' instances/occurances as I put it up there. I don't know how that is simplified from how I put it though.

Edit:

I made mine a UDF, so you didn't have to mess with it, and only fill out the strings you wanted to replace up top, and the replace string you wanted to replace it with. Plus you can use it over and over.

Edit2:

P.S. Your _FileWriteFromArray() is wrong. Just use the FileWriteLine() like I have it.

Edited by SmOke_N

[center]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.[/center]

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0