mechh Posted February 5, 2006 Posted February 5, 2006 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?
MHz Posted February 5, 2006 Posted February 5, 2006 Welcome mechh, Do you have the script available so it can be improved on.
greenmachine Posted February 5, 2006 Posted February 5, 2006 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.
Moderators SmOke_N Posted February 5, 2006 Moderators Posted February 5, 2006 #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 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.
mechh Posted February 5, 2006 Author Posted February 5, 2006 (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 February 5, 2006 by mechh
Moderators SmOke_N Posted February 5, 2006 Moderators Posted February 5, 2006 (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 February 5, 2006 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.
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