Jump to content

Trouble with FileReadToArray


Recommended Posts

I am reading a list of files and paths into an array ($array). That is working. Next it should write the contents of the file identified by $array to $aFile using _FileReadToArray. The problem is I am reading the same file info 4 times (only 4 files there for testing). ArrayDisplay confirms that. I am thinking it's a simple fix of something that I am overlooking.

#include <Array.au3>
#include <File.au3>
#include <recursivefilesearch.au3>

Dim $aFile

$Server = "\\SERVER\SHARE1"
$array = RecursiveFileSearch($Server, "(?i)(\\*.*)", "", 1)
$MasterFile = "\\SERVER\SHARE2\MasterFile.csv"

For $X = 0 To UBound($array) - 1
    _FileReadToArray($array[$X], $aFile)
    _ArrayDisplay($aFile)
    FileOpen($MasterFile, 1)
    If $MasterFile = -1 Then
        MsgBox(0, "Error", "Unable to open file.")
        Exit
    EndIf
    _FileWriteFromArray($MasterFile, $aFile, 1)
    FileWrite($MasterFile, @CRLF)
    FileClose($MasterFile)
Next

Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Link to comment
Share on other sites

Maybe:

FileOpen($MasterFile, 1)
If $MasterFile = -1 Then
     MsgBox(0, "Error", "Unable to open file.")
     Exit
EndIf

For $X = 1 To $array[0]
    _FileReadToArray($array[$X], $aFile)
    _ArrayDisplay($aFile)

    _FileWriteFromArray($MasterFile, $aFile, 1)
    FileWrite($MasterFile, @CRLF)
Next
FileClose($MasterFile)
Link to comment
Share on other sites

Thanks. I found that my proble was in my data files I was reading. I changed the names of the files but not the data they contained. muttley Of course it makes more sense to not open files in a loop so as usual, the weaponx code goes in the official script.

Now I have to ask why my data keeps being overwritten? According to the helpfile from FileWriteFromArray:

; Open file and append second array
$hFile = FileOpen($sFile, 1);  1 = append
_FileWriteFromArray($hFile, $avUser, 1)
FileClose($hFile)

I thought I had that covered with

FileOpen($MasterFile, 1)

and

_FileWriteFromArray($MasterFile, $aFile, 1)

Edit: What is up with the code boxes?

Edited by ksmith247

Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Link to comment
Share on other sites

Thanks. I found that my proble was in my data files I was reading. I changed the names of the files but not the data they contained. muttley Of course it makes more sense to not open files in a loop so as usual, the weaponx code goes in the official script.

Now I have to ask why my data keeps being overwritten? According to the helpfile from FileWriteFromArray:

; Open file and append second array
$hFile = FileOpen($sFile, 1);  1 = append
_FileWriteFromArray($hFile, $avUser, 1)
FileClose($hFile)

I thought I had that covered with

FileOpen($MasterFile, 1)

and

_FileWriteFromArray($MasterFile, $aFile, 1)

Edit: What is up with the code boxes?

Once you open the file with flags, they will only apply if you use the returned handle for further operations. So your code should be more like:
$hFile = FileOpen($MasterFile, 1)
_FileWriteFromArray($hFile, $aFile, 1)

If you use $MasterFile as the string file name to open and then use that same string also for _FileWriteFromArray(), it will overwrite (or fail because the file is already open for write when it tries to open it again).

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I bow to you.. PsaltyDS and your mad muttley skills

Thanks again for the info guys.

Support bacteria; it's the only culture most people have.LxP's Learning to Script with AutoIt 3 - Excellent starting placeVolly's Links Page - Links to cool and useful scriptsAutoIt Wrappers - Valuater's AutoIt Wrappers post. Lots of good stuff.Support AutoIt - Make a donation here; I did.[size="2"]#include <Guinness.pint>[/size]

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...