JimC Posted March 17, 2008 Share Posted March 17, 2008 I have text files that I need written to existing files I first read them into an array. The problem is that i determine what line to start writing to then write out one line at a time. Is there a function that you can write an array to a file by indicating the starting line. Second ? is that I need to apend an array to one line is there a way to accomplish this ? Thanks in advance for any help you can provide. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 17, 2008 Moderators Share Posted March 17, 2008 (edited) Quick and dirty (Not tested):Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1) Local $sStringStore, $sRead, $sStartStore, $aSplit, $iCC, $nUBound = $nEndArray, $sEndStore If $nEndArray = -1 Then $nUBound = UBound($nEndArray) - 1 Switch $nLineFile Case -1 For $iCC = $nStartArray To $nUBound $sStringStore &= $avArray & @CRLF Next Return FileWrite($hFile, StringTrimRight($sStringStore, 2)) Case Else If $nLineFile > 1 Then $sRead = FileRead($hFile) $aSplit = StringSplit(StringStripCR($sRead), @LF) For $iCC = 1 To $nLineFile - 1 $sStartStore &= $aSplit[$iCC] & @CRLF Next For $iCC = $nLineFile To $aSplit[0] $sEndStore &= $aSplit[$iCC] & @CRLF Next $sEndStore = StringTrimRight($sEndStore, 2) Else FileClose(FileOpen($hFile, 2)) EndIf For $iCC = $nStartArray To $nUBound $sStringStore &= $avArray[$iCC] & @CRLF Next Return FileWrite($hFile, $sStartStore & StringTrimRight($sStringStore, 2) & $sEndStore) EndSwitch Return SetError(1, 0, 0) EndFunc Edit: Just realized I didn't write the rest of the file out after the insert Fixed above. Edited March 17, 2008 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. Link to comment Share on other sites More sharing options...
JimC Posted March 17, 2008 Author Share Posted March 17, 2008 Quick and dirty (Not tested):Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1) Local $sStringStore, $sRead, $sStartStore, $aSplit, $iCC, $nUBound = $nEndArray, $sEndStore If $nEndArray = -1 Then $nUBound = UBound($nEndArray) - 1 Switch $nLineFile Case -1 For $iCC = $nStartArray To $nUBound $sStringStore &= $avArray & @CRLF Next Return FileWrite($hFile, StringTrimRight($sStringStore, 2)) Case Else If $nLineFile > 1 Then $sRead = FileRead($hFile) $aSplit = StringSplit(StringStripCR($sRead), @LF) For $iCC = 1 To $nLineFile - 1 $sStartStore &= $aSplit[$iCC] & @CRLF Next For $iCC = $nLineFile To $aSplit[0] $sEndStore &= $aSplit[$iCC] & @CRLF Next $sEndStore = StringTrimRight($sEndStore, 2) Else FileClose(FileOpen($hFile, 2)) EndIf For $iCC = $nStartArray To $nUBound $sStringStore &= $avArray[$iCC] & @CRLF Next Return FileWrite($hFile, $sStartStore & StringTrimRight($sStringStore, 2) & $sEndStore) EndSwitch Return SetError(1, 0, 0) EndFunc Edit: Just realized I didn't write the rest of the file out after the insert Fixed above. Can you give a brief explanation of each argument ? Thank You !!1 Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 17, 2008 Moderators Share Posted March 17, 2008 Can you give a brief explanation of each argument ?Thank You !!1Sorry, thought they were self explanitory:Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1)$hFile is the file location/name$avArray is the array you want to add to the file$nStartArray is where you want to start adding from the array you just sent$nEndArray is where you want to end in the array ... -1 = the end of the array$nLineFile is the line you want to insert the array at in the file 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. Link to comment Share on other sites More sharing options...
JimC Posted March 17, 2008 Author Share Posted March 17, 2008 I am feeding the following: file to add array array to read to write to file item in array to start from I am feeding 1 feeding -1 for end of array I am feeding a variable which is the starting line number to start the write unfortunately it is not working as of yet. I will continue to mess around with it. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted March 17, 2008 Moderators Share Posted March 17, 2008 I am feeding the following: file to add array array to read to write to file item in array to start from I am feeding 1 feeding -1 for end of array I am feeding a variable which is the starting line number to start the write unfortunately it is not working as of yet. I will continue to mess around with it.Had my Ubound pointing at a string and not an array, this has been tested now:Global $aArray, $hMyFile = "mytest.txt" $aArray = StringSplit("def,ghi", ",") _FileWriteToLineFromArray($hMyFile, $aArray, 1, -1, 2) Func _FileWriteToLineFromArray($hFile, $avArray, $nStartArray = 0, $nEndArray = -1, $nLineFile = -1) Local $sStringStore, $sRead, $sStartStore, $aSplit, $iCC, $nUBound = $nEndArray, $sEndStore If $nEndArray = -1 Then $nUBound = UBound($avArray) - 1 Switch $nLineFile Case -1 For $iCC = $nStartArray To $nUBound $sStringStore &= $avArray & @CRLF Next Return FileWrite($hFile, StringTrimRight($sStringStore, 2)) Case Else If $nLineFile > 1 Then $sRead = FileRead($hFile) $aSplit = StringSplit(StringStripCR($sRead), @LF) For $iCC = 1 To $nLineFile - 1 $sStartStore &= $aSplit[$iCC] & @CRLF Next For $iCC = $nLineFile To $aSplit[0] $sEndStore &= $aSplit[$iCC] & @CRLF Next $sEndStore = StringTrimRight($sEndStore, 2) EndIf For $iCC = $nStartArray To $nUBound $sStringStore &= $avArray[$iCC] & @CRLF Next If $sEndStore = "" Then $sStringStore = StringTrimRight($sStringStore, 2) FileClose(FileOpen($hFile, 2)) Return FileWrite($hFile, $sStartStore & $sStringStore & $sEndStore) EndSwitch Return SetError(1, 0, 0) 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. Link to comment Share on other sites More sharing options...
JimC Posted March 17, 2008 Author Share Posted March 17, 2008 Holy cow. That is much faster than doing one line at a time using for .. next. Thank you for your help !!!! Awesome job. 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