Jump to content
Sign in to follow this  
williamk

File Write Help

Recommended Posts

williamk

Hi,

Want to take a file "presize.txt", write it's lines to an array, modify the lines, then rewrite the lines to a new file. As a basic start I have the below code. It should bascially read all the lines and write them to a new file. It runs without error but the PresizeImport.txt file is empty. What am I doing wrong?

#include <file.au3>

#include <array.au3>

$outfile = FileOpen("PresizeImport.txt",1)

Dim $aRecords

If Not _FileReadToArray("C:\presize.txt",$aRecords) Then

MsgBox(4096,"Error", " Error reading log to Array error:" & @error)

Exit

EndIf

If $outfile = -1 Then

MsgBox(0, "Error", "Unable to open file.")

Exit

EndIf

For $x = 1 to $aRecords[0]

FileWrite($outfile, $aRecords[$x] & @CRLF)

Next

FileClose($outfile)

Share this post


Link to post
Share on other sites
funkey

You use _FileReadToArray, but why don't you use _FileWriteFromArray after changing the lines? :idea:


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
williamk

You use _FileReadToArray, but why don't you use _FileWriteFromArray after changing the lines? :idea:

OK so I changed the FileWrite to a _FileWriteFromArray. Runs fine but still noting in my output file.

Share this post


Link to post
Share on other sites
funkey

#include <file.au3>
#include <array.au3>

$infile = "C:\presize.txt"
$outfile = @ScriptDir & "\PresizeImport.txt"

Dim $aRecords
_FileReadToArray($infile, $aRecords)
If @error Then
 MsgBox(4096, "Error", " Error reading log to Array. Error:" & @error)
 Exit
EndIf

_FileWriteFromArray($outfile, $aRecords, 1)
If @error Then
 MsgBox(4096, "Error", " Error writing log from Array. Error:" & @error)
 Exit
EndIf
ShellExecute($outfile)


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
williamk

OK that works, but then how do I edit each line in the array before I write it. For example, one thing I need to do is strip all the white space at the end of each line and also add a pipe delimeter at the end of each line.

#include <file.au3>
#include <array.au3>

$infile = "C:\presize.txt"
$outfile = @ScriptDir & "\PresizeImport.txt"

Dim $aRecords
_FileReadToArray($infile, $aRecords)
If @error Then
 MsgBox(4096, "Error", " Error reading log to Array. Error:" & @error)
 Exit
EndIf

_FileWriteFromArray($outfile, $aRecords, 1)
If @error Then
 MsgBox(4096, "Error", " Error writing log from Array. Error:" & @error)
 Exit
EndIf
ShellExecute($outfile)

Share this post


Link to post
Share on other sites
williamk

OK funkey,

So I started to edit the lines of each array element and writing them to my output file. What I am basically doing is disecting each line and adding pipe delimeters so the file can be imported into SQL. See code below for how I am doing it. Problem I see is that each file line is quite long and I need to add about 40 delimeters. This is going to make for a L O N G line of code, which I believe autoit will be unable to parse. If I remember right, there is a line limit length. Is there a way around that?

#include <file.au3>
#include <array.au3>

   
$infile = "C:\presize.txt"
$outfile = "C:\PresizeImport.txt"
If FileExists($outfile) Then
 FileDelete ($outfile)  
EndIf
FileOpen($outfile,1)
Dim $FL
_FileReadToArray($infile, $FL)
If @error Then
 MsgBox(4096, "Error", " Error reading log to Array. Error:" & @error)
 Exit
EndIf

If @error Then
 MsgBox(4096, "Error", " Error writing log from Array. Error:" & @error)
 Exit
EndIf

For $x = 1 to $FL[0]
FileWriteLine($outfile, StringLeft($FL[$x],1) & "|" & StringMid($FL[$x],2,2) & "|" & StringMid($FL[$x],4,4) & "|" & StringMid($FL[$x],7,4) & "|" & StringMid($FL[$x],13,2) & "|" & @CRLF)
Next
FileClose($outfile)
;ShellExecute($outfile)

#include <file.au3>
#include <array.au3>

$infile = "C:\presize.txt"
$outfile = @ScriptDir & "\PresizeImport.txt"

Dim $aRecords
_FileReadToArray($infile, $aRecords)
If @error Then
 MsgBox(4096, "Error", " Error reading log to Array. Error:" & @error)
 Exit
EndIf

_FileWriteFromArray($outfile, $aRecords, 1)
If @error Then
 MsgBox(4096, "Error", " Error writing log from Array. Error:" & @error)
 Exit
EndIf
ShellExecute($outfile)

Share this post


Link to post
Share on other sites
funkey

Try this way:

#include <file.au3>
#include <array.au3>
#Include <String.au3>

$infile = "C:\presize.txt"
$outfile = "C:\PresizeImport.txt"

If FileExists($outfile) Then
 FileDelete ($outfile)  
EndIf

Dim $FL
_FileReadToArray($infile, $FL)
If @error Then
 MsgBox(4096, "Error", " Error reading log to Array. Error:" & @error)
 Exit
EndIf

Local $aPos[6] = [2, 4, 8, 12, 16, 20] ;Enter here the positions of the pipe to be inserted

For $y = 1 to $FL[0]
 For $x = UBound($aPos) -1 To 0 Step -1
  $FL[$y] = _StringInsert($FL[$y], "|", $aPos[$x])
 Next
Next

_FileWriteFromArray($outfile, $FL, 1)
If @error Then
 MsgBox(4096, "Error", " Error writing log from Array. Error:" & @error)
 Exit
EndIf


ShellExecute($outfile)

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

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  

×