-
May 7th, 2021, 11:47 AM
#1
Thread Starter
Lively Member
Create a List for multiple entries
I need to create a List to store multiple values like following example:
I need to loop through all filenames in a folder.
I compare the filename against keywords. When the keyword exists in the filename we want to store
both the keyword and the filename to a List, for example:
"this is a beautiful filename.doc" <-> keyword "beautiful"
we store filename and keyword beautiful
"this is also a beautiful next filename.doc" <-> keyword "beautiful" and keyword "next"
we store filename and keyword beautiful and next
I am kind of lost because later on I want to return for example all filenames that match the keyword "beautiful"
Code:
ReadOnly values As Dictionary(Of String, String) = New Dictionary(Of String, String)()
-
May 7th, 2021, 12:05 PM
#2
Re: Create a List for multiple entries
Well, you're on the right track. What I'd use is a Dictionary(of String, List(of String))
For each key, there would be a List(of String) holding all the filenames.
My usual boring signature: Nothing
-
May 7th, 2021, 12:16 PM
#3
Thread Starter
Lively Member
Re: Create a List for multiple entries
Originally Posted by Shaggy Hiker
Well, you're on the right track. What I'd use is a Dictionary(of String, List(of String))
For each key, there would be a List(of String) holding all the filenames.
I created as you suggested:
Code:
ReadOnly files As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))()
If Not files.ContainsKey(sCountry) Then
files.Add(sCountry, New List(Of String))
files(sCountry).Add(sFile)
Else
files(sCountry).Add(sFile)
End If
If I want to retrieve all files with a certain country code?
Last edited by clausowitz; May 7th, 2021 at 12:41 PM.
-
May 7th, 2021, 02:16 PM
#4
Re: Create a List for multiple entries
You should check if the country code exists in the dictionary, if so then get the value (i.e. List):
Code:
Dim countryCode = "USA"
If (files.ContainsKey(countryCode)) Then
Dim files = files(countryCode)
Console.WriteLine(String.Join(", ", files.ToArray()))
End If
-
May 7th, 2021, 03:16 PM
#5
Thread Starter
Lively Member
Re: Create a List for multiple entries
Originally Posted by dday9
You should check if the country code exists in the dictionary, if so then get the value (i.e. List):
Code:
Dim countryCode = "USA"
If (files.ContainsKey(countryCode)) Then
Dim files = files(countryCode)
Console.WriteLine(String.Join(", ", files.ToArray()))
End If
I tried following:
Code:
For Each kvp In files
If kvp.Key = sCountries Then
For Each Str In kvp.Value
Next
End If
Next
Which works better?
-
May 7th, 2021, 03:21 PM
#6
Re: Create a List for multiple entries
Try this...
Code:
Dim values As New Dictionary(Of List(Of String), List(Of String))
Dim keywords() As String = {"beautiful", "next"}
Dim lines() As String = {"this is a beautiful filename.doc", _
"this is also a beautiful next filename.doc"}
For Each line As String In lines
Dim foundKeywords As New List(Of String)
For Each keyword As String In keywords
If line.Contains(keyword) Then
foundKeywords.Add(keyword)
End If
Next
Dim found As Boolean = False
For Each kvp As KeyValuePair(Of List(Of String), List(Of String)) In values
If kvp.Key.SequenceEqual(foundKeywords) Then
kvp.Value.Add(line)
found = True
Exit For
End If
Next
If foundKeywords.Count > 0 AndAlso Not found Then
values.Add(foundKeywords, New List(Of String)(New String() {line}))
End If
Next
***CODE EDITED***
Last edited by .paul.; May 7th, 2021 at 03:37 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 7th, 2021, 09:05 PM
#7
Re: Create a List for multiple entries
Originally Posted by clausowitz
I tried following:
Code:
For Each kvp In files
If kvp.Key = sCountries Then
For Each Str In kvp.Value
Next
End If
Next
Which works better?
Definitely not that. It defeats the purpose of a Dictionary, which is fast lookup.
-
May 7th, 2021, 09:31 PM
#8
Re: Create a List for multiple entries
Originally Posted by jmcilhinney
Definitely not that. It defeats the purpose of a Dictionary, which is fast lookup.
But the OP wants a List(of String) as the key...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 7th, 2021, 09:58 PM
#9
Re: Create a List for multiple entries
Originally Posted by .paul.
But the OP wants a List(of String) as the key...
I don't see that. There's no reason that a single file path can't be added to multiple values if that file contains multiple keywords. The OP said that they want to get all the files containing a specific keyword, so it would seem that each key would be a single keyword.
-
May 7th, 2021, 10:17 PM
#10
Re: Create a List for multiple entries
Originally Posted by jmcilhinney
I don't see that. There's no reason that a single file path can't be added to multiple values if that file contains multiple keywords. The OP said that they want to get all the files containing a specific keyword, so it would seem that each key would be a single keyword.
Yes. That sounds like a better plan. A Dictionary(of String, List(of String))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
May 8th, 2021, 03:08 AM
#11
Re: Create a List for multiple entries
I would search the folder first for Files that contain any of the keywords and count later. I don't know how many Files you
have in the folder.
might be quicker that way, first the search and then the count, or it won't make much diffrence
here to find with keywords
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim folderToSearch = "d:\check"
Dim stringToFind = New List(Of String) From {"Result", "Test1", "Test2"}
Dim stringInfiles = (New DirectoryInfo(folderToSearch)).GetFiles().Where(Function(file) stringToFind.Any(Function(condition) file.Name.IndexOf(condition) > -1)).ToList()
stringInfiles.ForEach(Sub(file) ListBox1.Items.Add(file.Name))
End Sub
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 8th, 2021, 04:08 AM
#12
Thread Starter
Lively Member
Re: Create a List for multiple entries
Let me give some clarification for what I do.
I have an archive of about 4000 documents. With the code I loop through all the filenames and search for country names.
When I find a country that I want to save I store the name of the country if it doesn't already exists in the list and add the filename that contains the country name.
Therefor I created a dictionary:
Code:
Public files As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))()
sFile = "my first filename USA.doc"
sCountry = "USA"
If Not files.ContainsKey(sCountry) Then
files.Add(sCountry, New List(Of String))
files(sCountry).Add(sFile)
Else
files(sCountry).Add(sFile)
End If
After this I select a country name from a Listview and open a form in Dialog.
Code:
Public Overloads Function ShowDialog(ByVal sCountry As String) As String
sCountries = sCountry
MyBase.ShowDialog()
End Function
then I select all the records from a database that contains details of all documents and show them in a Listview
Code:
For Each kvp In files
'find all records for the chosen country
If kvp.Key = sCountries Then
For Each Str In kvp.Value
Dim row As DataRow
For Each row In dt1.Rows
If Str = row.Item("Document Name") Then
i += 1
sName = row.Item("Document Name")
sType = row.Item("Document Type")
sPath = row.Item("Document Location")
iId = row.Item("Id")
ListView1.Items.Add(New ListViewItem(New String() {"", sName.ToString, sPath.ToString, iId.ToString}, docImage))
End If
Next
Next
End If
Next
Although it is functioning I am curious to know if there is a more effective way to do this
-
May 8th, 2021, 07:45 AM
#13
Re: Create a List for multiple entries
I use a Powershell script to:
1) read all the Filenames is a Folder
2) create a .csv File from that
3) read that .csv File to a Database Table
4) perform SQL Query for my search
I don't know if that is an option for you
here a Image of what the Database table looks like after Import
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 9th, 2021, 06:22 AM
#14
Thread Starter
Lively Member
Re: Create a List for multiple entries
Chris,
does this also store which strings were found?
When retrieving I want to be able to select all files that contain "Test1"
-
May 9th, 2021, 06:23 AM
#15
Thread Starter
Lively Member
Re: Create a List for multiple entries
Chris,
does this also store which strings were found?
When retrieving I want to be able to select all files that contain "Test1"
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim folderToSearch = "d:\check"
Dim stringToFind = New List(Of String) From {"Result", "Test1", "Test2"}
Dim stringInfiles = (New DirectoryInfo(folderToSearch)).GetFiles().Where(Function(file) stringToFind.Any(Function(condition) file.Name.IndexOf(condition) > -1)).ToList()
stringInfiles.ForEach(Sub(file) ListBox1.Items.Add(file.Name))
End Sub
-
May 9th, 2021, 11:03 AM
#16
Re: Create a List for multiple entries
Originally Posted by clausowitz
Chris,
does this also store which strings were found?
When retrieving I want to be able to select all files that contain "Test1"
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim folderToSearch = "d:\check"
Dim stringToFind = New List(Of String) From {"Result", "Test1", "Test2"}
Dim stringInfiles = (New DirectoryInfo(folderToSearch)).GetFiles().Where(Function(file) stringToFind.Any(Function(condition) file.Name.IndexOf(condition) > -1)).ToList()
stringInfiles.ForEach(Sub(file) ListBox1.Items.Add(file.Name))
End Sub
don't know what you mean, you see the found Files in the Listbox
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 9th, 2021, 11:17 AM
#17
Thread Starter
Lively Member
Re: Create a List for multiple entries
What I mean is.
Your Listbox1 contains all files that matched one of the criteria (Test1, Test2 etc.)
How can I see which criteria was found in what file?
-
May 9th, 2021, 11:21 AM
#18
Re: Create a List for multiple entries
Originally Posted by clausowitz
What I mean is.
Your Listbox1 contains all files that matched one of the criteria (Test1, Test2 etc.)
How can I see which criteria was found in what file?
that's what I meant by Count afterwords.
it would find files with one or all three search strings
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 9th, 2021, 04:01 PM
#19
Re: Create a List for multiple entries
Originally Posted by clausowitz
When retrieving I want to be able to select all files that contain "Test1"
If you want to store and retrieve a chunk of data based on a keyword, then a Dictionary is a good choice. You had one set up in Post #12.
However, to retrieve values you do not iterate through the key value pairs. Dictionaries leverage a hash table which makes all look-ups O(1) operations instead of O(n) at worst when iterating. To fetch data it's simply value = dictionary(key).
So your current code:
Code:
For Each kvp In files
'find all records for the chosen country
If kvp.Key = sCountries Then
For Each Str As String In kvp.Value
' If the datatable happens to contain a record for this filename (Str)
' then display the record data in the ListView
Next
End If
Next
becomes:
Code:
Public files As Dictionary(Of String, List(Of String)) = New Dictionary(Of String, List(Of String))()
sFile = "my first filename USA.doc"
sCountry = "USA"
If Not files.ContainsKey(sCountry) Then
files.Add(sCountry, New List(Of String))
files(sCountry).Add(sFile)
Else
files(sCountry).Add(sFile)
End If
Code:
Dim countryFileNames As List(Of String) = files(sCountry)
For Each fileName In countryFileNames
' If the datatable happens to contain a record for this filename
' then display the record data in the ListView
Next
and of course those first two lines can be simplified into one so as to reduce clutter:
For Each fileName In files(sCountry)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|