Jump to content

Recommended Posts

Posted (edited)

Hello,

I'm trying to make a program that can look at a folder directory, find all the CSV files, and then add the data from CSV's to an array. 

The problem I seem to be running into is on the 2nd iteration (2nd file) when the script will not create an array. Could someone please help? Thanks in advance

 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <Excel.au3>
#include <MsgBoxConstants.au3>

Global $MasterArray


RefineData()


Func RefineData()


    Local $i, $filenum, $file, $csvArray, $sFilePath = @ScriptDir

    $fileList = _FileListToArrayRec($sFilePath, "*.csv", 1) ;Create and array of all .csv files within folder

    ;=====Loop through the .csv files within the folder======

    For $filenum = 1 To UBound($fileList) - 1 Step 1

        $file = $fileList[$filenum]
        $sFilePath = $sFilePath & "\" & $file


        ;=====Create array based on csv file=====

        _FileReadToArray($sFilePath, $csvArray, $FRTA_NOCOUNT, ",")


        _ArrayDisplay($csvArray,"File: " & $filenum)


        If $filenum = 1 Then

            $MasterArray = $csvArray
            _ArrayDisplay($MasterArray, "Master")

        Else
            $MasterArray = _ArrayColInsert($MasterArray, UBound($MasterArray)) ;want column added at end

            For $i = 0 To UBound($MasterArray)-1 Step 1

                $MasterArray[$i][UBound($MasterArray) - 1] = $csvArray[$i][4]
            Next
            _ArrayDisplay($MasterArray, "Master")
        EndIf


    Next




EndFunc   ;==>RefineData

 

Edited by AnonymousX
Posted
On 3/27/2018 at 9:22 PM, kaisies said:

See what sFilePath is after this line each iteration of the loop. That's your problem 


$sFilePath = $sFilePath & "\" & $file

I didn't get why you thought this was the problem at first, and even in testing I just kept checking that the file name was correct. 

 

After way to long, finally realized that I can't use this line of code I need to keep the source file path constant.

 

This is the solution..... 

$FilePath = $sFilePath & "\" & $file

 

Appreciate the help, I'd recommend being less vague next time though, for dummy's like me that need things spelled out.

 

Thanks!

Posted

Does this actually work?  You seem to overwrite your $MasterArray each time you loop through the file list.  Also you need to identify which dimension to change currently you're using 1d rather than 2d when expanding the columns, so it should be something like:

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

Global $aMasterArray

RefineData()

Func RefineData()
    Local $aCsvFile, $sFilePath = @ScriptDir
    Local $aFileList = _FileListToArrayRec($sFilePath, "*.csv", $FLTA_FILES, $FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;Create and array of all .csv files within folder
    ;=====Loop through the .csv files within the folder======
    For $i = 1 To $aFileList[0]
        ;=====Create array based on csv file=====
        _FileReadToArray($aFileList[$i], $aCsvFile, $FRTA_NOCOUNT, ",")
        If $i = 1 Then
            $aMasterArray = $aCsvFile
            _ArrayDisplay($aMasterArray, "Master")
        Else
            _ArrayColInsert($aMasterArray, UBound($aMasterArray, 2)) ;want column added at end
            For $j = 0 To UBound($aMasterArray) - 1
                $aMasterArray[$j][UBound($aMasterArray, 2) - 1] = $aCsvFile[$j][4]
            Next
            _ArrayDisplay($aMasterArray, "Master")
        EndIf
    Next
EndFunc   ;==>RefineData

 

  • 2 weeks later...
Posted
On 4/3/2018 at 8:44 PM, Subz said:

Does this actually work?  You seem to overwrite your $MasterArray each time you loop through the file list.  Also you need to identify which dimension to change currently you're using 1d rather than 2d when expanding the columns, so it should be something like:

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

Global $aMasterArray

RefineData()

Func RefineData()
    Local $aCsvFile, $sFilePath = @ScriptDir
    Local $aFileList = _FileListToArrayRec($sFilePath, "*.csv", $FLTA_FILES, $FLTAR_NORECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) ;Create and array of all .csv files within folder
    ;=====Loop through the .csv files within the folder======
    For $i = 1 To $aFileList[0]
        ;=====Create array based on csv file=====
        _FileReadToArray($aFileList[$i], $aCsvFile, $FRTA_NOCOUNT, ",")
        If $i = 1 Then
            $aMasterArray = $aCsvFile
            _ArrayDisplay($aMasterArray, "Master")
        Else
            _ArrayColInsert($aMasterArray, UBound($aMasterArray, 2)) ;want column added at end
            For $j = 0 To UBound($aMasterArray) - 1
                $aMasterArray[$j][UBound($aMasterArray, 2) - 1] = $aCsvFile[$j][4]
            Next
            _ArrayDisplay($aMasterArray, "Master")
        EndIf
    Next
EndFunc   ;==>RefineData

 

This might have been an over site in my simplified test script provided, but the root of the issue I was experiencing was that I wasn't using a constant for my directory path, as mentioned above. 

 

Appreciate the help as always man! 

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
×
×
  • Create New...