Jump to content
Sign in to follow this  
ksmith247

Trouble with FileReadToArray

Recommended Posts

ksmith247

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]

Share this post


Link to post
Share on other sites
weaponx

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)

Share this post


Link to post
Share on other sites
ksmith247

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]

Share this post


Link to post
Share on other sites
weaponx

I don't see anything in the code that would cause an overwrite.

Share this post


Link to post
Share on other sites
PsaltyDS

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

Share this post


Link to post
Share on other sites
weaponx

How did I miss that one?

Share this post


Link to post
Share on other sites
PsaltyDS

How did I miss that one?

Blinded by the glaring light of my brilliance, no doubt...

muttley


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

Share this post


Link to post
Share on other sites
weaponx

Blinded by the glaring light of my brilliance, no doubt...

:)

I'm still squinting. Maybe I need IR goggles to see it? muttley

Share this post


Link to post
Share on other sites
ksmith247

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]

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  

×