1

以下のようなフォルダ構造があります。ディレクトリとその中のファイルを保存したい....配列に。

ディレクトリを配列に保存しましたが、ファイル(0)、ファイル(1)などのときに直接呼び出すことができないため、そのファイルを配列に保存しているかどうかはわかりません.

各ファイルを 2 次元配列に格納することは可能ですか?

ここで、files(j)(k) j はディレクトリ、k はファイルです。?

さて、私が解決したかったのは、各ファイルをループしてその内容を比較することです...おそらく解決する1つの方法は、2次元配列を行うことです。

間違っている場合は修正してください。ありがとう。

parent                'I stored it as an array
|
|---dirA              'dir(0)
|    |---fileA.txt           'file(0)
|    |---fileB.txt           'file(1)
|
|---dirB              'dir(1) 
|    |---file2A.txt          'file(0)
|    |---file2B.txt          'file(1)
|
|---dirC              'dir(2)
     |---file3A.txt          'file(0)

これが私のコードです:

Option Explicit On
Imports System 
Imports Scripting
Imports System.IO
Imports System.Text.RegularExpressions

Public Class Form1

Public Shared directory(), files(), FILE_NAME1, FILE_NAME2 As String
Public Shared count As Long
Public Shared counter1, counter2 As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
Public Shared dirsize, filesize, i, j As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim parentinfo As New DirectoryInfo("C:\parent")

    Dim dirsize As Integer = System.IO.Directory.GetDirectories("C:\parent").Length
    'ComboBox1.Items.Add(dirsize) --> This is for me to check for the no.of folders in the parent

    ReDim directory(dirsize)

    '------------------------------------------------------
    'Access the Parent Folder and stores the directory in an array[]
    '------------------------------------------------------

    For Each dir As FileSystemInfo In parentinfo.GetFileSystemInfos()
        directory(i) = dir.Name
        i += 1
        ReDim Preserve directory(i)
    Next dir

    '------------------------------------------------------
    ' In this part, I will Re-iterate in each folder and access all the files inside it
    '------------------------------------------------------
    For k = 0 To dirsize - 1

        Dim childinfo As New DirectoryInfo("C:\parent\" & directory(k))

        'Count the no.of files contained in each folder
        Dim location = New DirectoryInfo("C:\parent\" & directory(k))
        Dim filesize = location.GetFiles("*", SearchOption.AllDirectories)

        'Initialize and re-define files
        ReDim files(filesize.Length)
        Dim fs As Integer = 0

        'Re-iterate in each file inside the sub-folder and store it as an array
        For Each data As FileSystemInfo In childinfo.GetFileSystemInfos()

            '------------------------------------------------------
                                   'This is where I am storing the file as an array
            files(fs) = data.Name  'I am reluctant here if this is storing as an array

            'ComboBox1.Items.Add(files(fs)) --> This is for me to output array of files

            lst.Items.Add(files(fs))

            '------------------------------------------------------

            fs += 1
            ReDim Preserve files(fs)
        Next data

    Next

    '---------------------------------------------------------------
    'This is for me to loop over  the parent folder and its files, but it is 
    'throwing an error at runtime
    'My purpose for this is to compare the contents of each file
    'I haven't included my code for comparing contents because it will be very long
    '---------------------------------------------------------------

    For c = 0 To dirsize - 1 'number of directories in the parent folder

        Dim direct As String = "C:\parent\" & directory(c)
        Dim DirectoryEntries As String()
        Dim NumberOfFiles As Double

        DirectoryEntries = System.IO.Directory.GetFileSystemEntries(direct)

        NumberOfFiles = UBound(DirectoryEntries)

        For b = 0 To NumberOfFiles 'number of files in the subfolder (for example: dirA)

            'this should where i compare file1 and file2
            'in this part it throws an error-->Index was outside the bounds of the array

            FILE_NAME1 = "C:\parent\" & directory(c) & "\" & files(b) 

        Next

    Next

End Sub
End Class
4

1 に答える 1