NobodyWannabe Posted July 31, 2008 Share Posted July 31, 2008 (edited) Greetings! The standard disclaimer applies, searched all of the forums, read the help file, new to AutoIT, not sure where to begin. The task at hand is to "inject" different text strings at the start of each line in a text file. Original file has 30 lines --- Random Text|More Random Text|Even More Random Text Random Text|More Random Text|Even More Random Text Random Text|More Random Text|Even More Random Text Random Text|More Random Text|Even More Random Text Random Text|More Random Text|Even More Random Text The injected file would look like the table below. No line space is required. A line space has been added to indicate where the values in column 4 and 5 change. The line count (column 1, 01-30) is constant, whereas, columns 4 and 5 change after 8, 8, 4, 4, 2, 2, 1, 1. I have no sample code to provide as I do not know if the solution lies in a FileWrite, IniWrite or an Array. Any help/guidance would be greatly appreciated. Thanks! 01|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 02|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 03|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 04|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 05|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 06|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 07|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 08|2008|15|FT|1|Random Text|More Random Text|Even More Random Text 09|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 10|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 11|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 12|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 13|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 14|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 15|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 16|2008|15|CF|1|Random Text|More Random Text|Even More Random Text 17|2008|15|FT|2|Random Text|More Random Text|Even More Random Text 18|2008|15|FT|2|Random Text|More Random Text|Even More Random Text 19|2008|15|FT|2|Random Text|More Random Text|Even More Random Text 20|2008|15|FT|2|Random Text|More Random Text|Even More Random Text 21|2008|15|CF|2|Random Text|More Random Text|Even More Random Text 22|2008|15|CF|2|Random Text|More Random Text|Even More Random Text 23|2008|15|CF|2|Random Text|More Random Text|Even More Random Text 24|2008|15|CF|2|Random Text|More Random Text|Even More Random Text 25|2008|15|FT|3|Random Text|More Random Text|Even More Random Text 26|2008|15|FT|3|Random Text|More Random Text|Even More Random Text 27|2008|15|CF|3|Random Text|More Random Text|Even More Random Text 28|2008|15|CF|3|Random Text|More Random Text|Even More Random Text 29|2008|15|FT|4|Random Text|More Random Text|Even More Random Text 30|2008|15|CF|4|Random Text|More Random Text|Even More Random Text Edited July 31, 2008 by NobodyWannabe Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 Do a _FileReadToArray() Then loop through the array and pre-pend you #Include <File.au3> Dim $Lines _FileReadToArray(FILEPATH, $Lines) Dim $Newlines[$Lines[0]+1] For $x= 1 to $Lines[0] Select $x Case $x <= 8 ; Modify First Group of lines $Add = "0"&$x&"|2008|15|FT|1|" $Newlines[$x] = $add & $Lines[$x] Case $x > 8 AND $x <= 16 ; Modify second Group of lines EndSelect Next I dunno that would be one way to do it Link to comment Share on other sites More sharing options...
LostUser Posted July 31, 2008 Share Posted July 31, 2008 Usually, inserting text before a line in a file requires you to re-create the entire file yourself rather than a programmatic command doing it. Most programming commands I have seen allow you to append a line to the end of a file not to the beginning of a file or line. Looking at the File commands in AutoIt pertaining to what you want to do seems to agree with the appending scenario. -FileWriteLine 'Append a line of text to the end of a previously opened text file'. It also adds a CR and LF if it is not already there. -FileWrite 'Append a line of text to the end of a previously opened text file'. It does NOT add a CR or LF to the end of the line. What I have done before (not in AutoIt) is this: Open a temp file for writing. Read each line of the file you want to modify, writing each line that is read to the temp file until you find the line of text you want. Save the line of text from the file as a string and format the string so that the old text is appended to the new. Write the new line of text to the temp file. Continue reading the original file and writing each successive line to the temp file. Once the reading is done, delete the original file and rename the temp file as the original file. You could of course use the AutoIt's INI commands which are handy. I don't remember what kind of limits there are to .ini files though as far as size/line size. I seem to remember there were some (at least in windows). Ini file commands are pretty handy because the .ini files are slightly structured so you can jump to/modify/delete different sections and valutes in the file without having to recreate the entire file. Though, it may be that .ini file commands are actually doing things behind the scenes that cause the entire contents of the file to be rewritten so that they just appear to be making changes in the middle of the file. Anyway, hope that helps. I may be wrong too...maybe there is a way to write into the middle of a file without recreating it. Good luck. Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools. Link to comment Share on other sites More sharing options...
LostUser Posted July 31, 2008 Share Posted July 31, 2008 I like Paulie's idea better. It should work faster. Of course, if the file is huge then you may need to do the temp file or something like compressing the text in memory, blah blah blah. Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools. Link to comment Share on other sites More sharing options...
NobodyWannabe Posted July 31, 2008 Author Share Posted July 31, 2008 (edited) Guys, thanks for your quick answers. An idea has come to mind. What about injecting in reverse using two files. The first file has the first five columns, the second file has the random data. Based on Paulie's response, I was able to read the Random Text into an array. No idea what to do with it now that it's in an array, though. That done, would it be easier to append the Random Text from the array to the end of the first file that contains the five columns? In doing so, would it not be a more simple resolution? Read the Random Text into an array, inject it at the end of each line of another file. To LostUser, the file is static and only 30 lines. Size, therefore, is not an issue. Nor is speed. Thanks for taking the time and sharing your thoughts and expertise. Edited July 31, 2008 by NobodyWannabe Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 (edited) Well that would be a much easier thing to do! Read them both to 2 different arrays then just merge like this Fill in the filepaths, the run, and check your desktop for the completed file #include <File.au3> Dim $Lines1, $Lines2, $String = "" _FileReadToArray(FILEPATH, $Lines1) ; this should be the "random data" original file _FileReadToArray(FILEPATH2, $Lines2) ; this should be the file with the 5 columns $Result = PrePendMerge($Lines1, $Lines2) For $x = 1 To UBound($Result) - 1 $String &= $Result[$x] & @CRLF Next $File = FileOpen(@DesktopDir & "\Completed Prepended File.txt", 2) FileWrite($File, $String) FileClose($File) Func PrePendMerge($array1, $array2) ;Add each element of $array2 in front of each element in array1 If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR Dim $NewArray[UBound($array1)] For $x = 0 To UBound($NewArray) - 1 $NewArray[$x] = $array2[$x] & $array1[$x] Next Return $NewArray EndFunc ;==>PrePendMerge EDIT: Tidied, Forgot FileClose() Edited July 31, 2008 by Paulie Link to comment Share on other sites More sharing options...
NobodyWannabe Posted July 31, 2008 Author Share Posted July 31, 2008 Paulie, Thanks a lot! Your script runs, creates the new file, however, it is empty. Your thoughts? Thanks again for the fast response, and of course, ideas on how to get this task accomplished quickly and easily. Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 (edited) Paulie,Thanks a lot! Your script runs, creates the new file, however, it is empty.Your thoughts?Thanks again for the fast response, and of course, ideas on how to get this task accomplished quickly and easily.Odd... Did you catch my edit? I accidentally left out the "Fileclose()" the first time... But i just made my own test files, and it worked flawlesslyEdit, The error could be in the _FileReadToArray() Try using arraydisplay to debug and see where the data gets lost. Edited July 31, 2008 by Paulie Link to comment Share on other sites More sharing options...
NobodyWannabe Posted July 31, 2008 Author Share Posted July 31, 2008 Paulie, I most certainly did after I replied, added it to no avail. Your's works, hmmm? I copied and pasted the edit into the original script. I'm going to copy and paste the entire script and give it a go. I'll let you know. Thanks! Link to comment Share on other sites More sharing options...
NobodyWannabe Posted July 31, 2008 Author Share Posted July 31, 2008 Paulie, It won't go. Here's what I've changed... --- #include <File.au3> Dim $Lines1, $Lines2, $String = "" _FileReadToArray("c:\random.txt", $Lines1) ; this should be the "random data" original file _FileReadToArray("c:\five_col.txt", $Lines2) ; this should be the file with the 5 columns $Result = PrePendMerge($Lines1, $Lines2) For $x = 1 To UBound($Result) - 1 $String &= $Result[$x] & @CRLF Next $File = FileOpen("c:\combined.txt", 2) FileWrite($File, $String) FileClose($File) Func PrePendMerge($array1, $array2) ;Add each element of $array2 in front of each element in array1 If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR Dim $NewArray[uBound($array1)] For $x = 0 To UBound($NewArray) - 1 $NewArray[$x] = $array2[$x] & $array1[$x] Next Return $NewArray EndFunc ;==>PrePendMerge Thanks for commenting your script, it made a world of difference. Thanks for your patience and quick help! Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 yes, mine does work. Here is my exact script: #include <File.au3> Dim $Lines1, $Lines2, $String = "" _FileReadToArray(@DesktopDir&"\Test1.txt", $Lines1) ; this should be the "random data" original file _FileReadToArray(@DesktopDir&"\Test2.txt", $Lines2) ; this should be the file with the 5 columns $Result = PrePendMerge($Lines1, $Lines2) For $x = 1 To UBound($Result) - 1 $String &= $Result[$x] & @CRLF Next $File = FileOpen(@DesktopDir & "\Completed Prepended File.txt", 2) FileWrite($File, $String) FileClose($File) Func PrePendMerge($array1, $array2) ;Add each element of $array2 in front of each element in array1 If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR Dim $NewArray[UBound($array1)] For $x = 0 To UBound($NewArray) - 1 $NewArray[$x] = $array2[$x] & $array1[$x] Next Return $NewArray EndFunc ;==>PrePendMerge These are my 2 test files, located on my desktop: Test1.txt RANDOMDATA1|RANDOMDATA12|DANDOMDATA13 RANDOMDATA1|RANDOMDATA12|DANDOMDATA13n RANDOMDATA1|RANDOMDATA12|DANDOMDATA13a RANDOMDATA1|RANDOMDATA12|DANDOMDATA13s RANDOMDATA1|RANDOMDATA12|DANDOMDATA13f RANDOMDATA1|RANDOMDATA12|DANDOMDATA13df RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d RANDOMDATA1|RANDOMDATA12|DANDOMDATA13hd RANDOMDATA1|RANDOMDATA12|DANDOMDATA13gf RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d RANDOMDATA1|RANDOMDATA12|DANDOMDATA13dfg RANDOMDATA1|RANDOMDATA12|DANDOMDATA13fg test2.txt APPEND1| APPEND2| APPEND3| APPEND4| APPEND5| APPEND6| APPEND7| APPEND8| APPEND9| APPEND0| APPEND10| APPEND22| ====================== My resulting file: As expected: APPEND1|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13 APPEND2|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13n APPEND3|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13a APPEND4|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13s APPEND5|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13f APPEND6|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13df APPEND7|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d APPEND8|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13hd APPEND9|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13gf APPEND0|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13d APPEND10|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13dfg APPEND22|RANDOMDATA1|RANDOMDATA12|DANDOMDATA13fg Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 Are both of your files the exact same length in terms of line count? (ie. no extra spaces or linebreaks or anything?) If not, you would end up with a blank file. Link to comment Share on other sites More sharing options...
NobodyWannabe Posted July 31, 2008 Author Share Posted July 31, 2008 Paulie, Indeed! One of the files had an extra carriage return. Deleted it and your script ran flawlessly. Thanks for your expertise (and hand-holding) to help me get this accomplished. Final question, you mentioned using _ArrayDisplay. I inserted that (as well as #include <Array.au3>) in various places and the ListView did not appear with _ArrayDisplay on top of EndFunc. Below EndFunc, the script blew up stating: WARNING: $NewArray: possibly used before declaration. Where might _ArrayDisplay be best inserted? This will help me, and perhaps, others in the community better understand arrays. Thanks, again! Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 Paulie, Indeed! One of the files had an extra carriage return. Deleted it and your script ran flawlessly. Thanks for your expertise (and hand-holding) to help me get this accomplished. Final question, you mentioned using _ArrayDisplay. I inserted that (as well as #include <Array.au3>) in various places and the ListView did not appear with _ArrayDisplay on top of EndFunc. Below EndFunc, the script blew up stating: WARNING: $NewArray: possibly used before declaration. Where might _ArrayDisplay be best inserted? This will help me, and perhaps, others in the community better understand arrays. Thanks, again!Haha! no problem, I should have picked up on that being the error the first time If I were to debug my code with _ArrayDisplay It would probably look like this: #include <File.au3> #include <array.au3> Dim $Lines1, $Lines2, $String = "" _FileReadToArray(@DesktopDir&"\Test1.txt", $Lines1) ; this should be the "random data" original file _FileReadToArray(@DesktopDir&"\Test2.txt", $Lines2) ; this should be the file with the 5 columns _ArrayDisplay($Lines1, "Lines1 - Post FileReadToArray") _ArrayDisplay($Lines2, "Lines2 - Post FileReadToArray") $Result = PrePendMerge($Lines1, $Lines2) _ArrayDisplay($Result, "Result of Func PrePendMerge") For $x = 1 To UBound($Result) - 1 $String &= $Result[$x] & @CRLF Next $File = FileOpen(@DesktopDir & "\Completed Prepended File.txt", 2) FileWrite($File, $String) FileClose($File) Func PrePendMerge($array1, $array2) ;Add each element of $array2 in front of each element in array1 If UBound($array1) <> UBound($array2) Then Return 0 ;ERROR Dim $NewArray[UBound($array1)] For $x = 0 To UBound($NewArray) - 1 $NewArray[$x] = $array2[$x] & $array1[$x] Next _ArrayDisplay($NewArray, "NewArray - End Of Func PrePendMerge") Return $NewArray EndFunc ;==>PrePendMerge Link to comment Share on other sites More sharing options...
NobodyWannabe Posted July 31, 2008 Author Share Posted July 31, 2008 Paulie, Thanks, again! New AutoIT user, first time poster, and it was a real treat to have you as a coach. From what I've read on the forum, arrays are very complicated. In changing how to resolve this matter in the easiest/simplest way, light was shed on arrays. Which I think is a great starting point for those new to AutoIT. And for the betterment of the AutoIT community, I might add. Thanks for sharing your expertise, and doing so quickly, with only one agenda ... to help others. All my best! Link to comment Share on other sites More sharing options...
Paulie Posted July 31, 2008 Share Posted July 31, 2008 (edited) Paulie,Thanks, again!New AutoIT user, first time poster, and it was a real treat to have you as a coach.From what I've read on the forum, arrays are very complicated. In changing how to resolve this matter in the easiest/simplest way, light was shed on arrays. Which I think is a great starting point for those new to AutoIT. And for the betterment of the AutoIT community, I might add.Thanks for sharing your expertise, and doing so quickly, with only one agenda ... to help others.All my best!Aww Thanks!I'm glad I could help! And for the record, I'm pretty sure you are one of the nicest people I've ever helped, rarely do people give such high complements . Best of luck! and should you continue to use AutoIt, please never hesitate to drop me a PM/Instant message(see my profile) if you think there is anything I might be able to help you with What can I say? I'ma sucker for flattery Edited July 31, 2008 by Paulie Link to comment Share on other sites More sharing options...
LostUser Posted August 1, 2008 Share Posted August 1, 2008 Sorry to put this post at the top again but here is a link I wanted to add because it was so well thought out ... to me anyway.This guy goes to great lengths in figuring this out and posts it all out. Not that I can understand it all but I get the concepts, and the gist of what he was doing was pretty cool.Later Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools. 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