Jump to content

'Read all txt files in a directory' to array


myspacee
 Share

Recommended Posts

Hello,

i've a lot of .txt files in a server shared directory. (>500)
All files contain a single line. (eg: Juventus-Milan 0-0)

I need to read all files and put obtained list to an array. Make this :
 

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>

;~ ///////////////////////////////////////////////////
;~ //       define vars
;~ ///////////////////////////////////////////////////
Global $hGUI, $hInput, $hList, $sPartialData, $asKeyWords[1000]
$my_data_source = "\\server\shareddirectory\"
Local $sFileName = "", $iResult = 0
Local $sData

;~ ///////////////////////////////////////////////////
;~ //       read file by file
;~ ///////////////////////////////////////////////////
 Local $hSearch = FileFindFirstFile($my_data_source & "*.*")

 ; Check if the search was successful, if not display a message and return False.
 If $hSearch = -1 Then
     MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
     Return False
 EndIf


;~ ///////////////////////////////////////////////////
;~ //       read file by file
;~ ///////////////////////////////////////////////////
 While 1
   $sFileName = FileFindNextFile($hSearch)
   If @error Then ExitLoop

   Local $aInput = FileReadToArray($my_data_source & $sFileName)
   $aInput = _ArrayUnique($aInput)

   For $i = 1 to UBound($aInput) -1
      $asKeyWords[$i] = $aInput[$i]
      $sData &= $asKeyWords[$i] & "|"
   Next


 WEnd
 
 FileClose($hSearch)

$hList = StringSplit($sData, "|")
$hList = _ArrayUnique($hList)
;~ _ArrayDisplay($hList,"final array")

Can correct it and suggest if this is the best approach ?

Thank you,
m.

Link to comment
Share on other sites

  • Developers

Something like this? :  (untested)

#include <Array.au3>
#include <File.au3>
#include <MsgBoxConstants.au3>
#include <GUIConstantsEx.au3>
#include <GuiListBox.au3>

;~ ///////////////////////////////////////////////////
;~ //       define vars
;~ ///////////////////////////////////////////////////
Global $asKeyWords[1000]
Global $i
Global $my_data_source = "\\server\shareddirectory\"
Global $sFileName = ""

;~ ///////////////////////////////////////////////////
;~ //       read file by file
;~ ///////////////////////////////////////////////////
Global $hSearch = FileFindFirstFile($my_data_source & "*.*")

; Check if the search was successful, if not display a message and return False.
If $hSearch = -1 Then
    MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
    Exit
EndIf


;~ ///////////////////////////////////////////////////
;~ //       read file by file
;~ ///////////////////////////////////////////////////
While 1
    $sFileName = FileFindNextFile($hSearch)
    If @error Then ExitLoop
    ; read line one as each file only contains 1 and add it into the array
    $asKeyWords[$i] = FileRead($my_data_source & $sFileName, 1)
    $i += 1
WEnd

FileClose($hSearch)

$asKeyWords = _ArrayUnique($asKeyWords)
_ArrayDisplay($asKeyWords, "final array")

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

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...