-1

So i am trying to do a match between filenames and a SQL dataset.

E.g. Recordset contains a array of 1000,1001,1002,1003

and my filelist contains a array of "c:\temp\100.txt","c:\temp\1000.txt","c:\temp\1001.txt","c:\temp\2002.txt","c:\temp\100.txt","c:\temp\1000-1.txt"

So my aim is to get a result of :

c:\temp\1000.txt c:\temp\1001.txt c:\temp\1000-1.txt

How do i accomplish that?

I've tried something like this

     private static String[] GetImageList()
    {
        return Directory.GetFiles("C:\\tempbilledeimport\\", "*.*", SearchOption.AllDirectories);

    }

    private static List<string> SQLRecordset()
    {
        List<string> items = new List<string>();

        string queryString = "select item from inventory where WebUseOnWeb=1";
        using (SqlConnection connection = new SqlConnection(DatabaseConnection.SqlConnectionStringCompany))
        {
            SqlCommand command = new SqlCommand(queryString, connection);
            connection.Open();
            SqlDataReader reader = command.ExecuteReader();

            // Call Read before accessing data.
            items.Add(reader[0].ToString());
            reader.Close();
        }

        return items;
    }

But how do i compare them?

  • 1
    I don't understand your question honestly – Marco Salerno Jul 14 '20 at 15:35
  • 1
    [Find and extract a number from a string](https://stackoverflow.com/questions/4734116/find-and-extract-a-number-from-a-string) or [How to extract number from string c#](https://stackoverflow.com/questions/11499159/how-to-extract-number-from-string-c-sharp) –  Jul 14 '20 at 16:10
  • Does this answer your question? [Find and extract a number from a string](https://stackoverflow.com/questions/4734116/find-and-extract-a-number-from-a-string) – karel Jul 14 '20 at 16:28

2 Answers2

1

Create a HashSet of integers containing the file names from which you replace c:\temp\ and .txt by empty strings then convert them to integers.

You can now check if each of the integers you got has a match in the HashSet.

I can see that you have a 1000-1.txt file. Not sure how you want to handle that. Is it a match?

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • Yes - my first attempt was using "string contains" method. Since 1000-1.txt is a match to item 1000. But as i read this, this will cause me trouble since a 10000.txt would be a match for item 1000. I need to "strip" the "-x" from the filelist to get a "full" item number to compare with... – user2517737 Jul 14 '20 at 15:27
  • 1
    I did not mean to do a match this way. You strip c:\temp\, then strip anything after a dash. If no dash found, just strip .txt. At this point you can convert what's left to an int and add it to a HashSet. – Tarik Jul 14 '20 at 15:30
1

Iterate through the array of files, strip them down to the filename itself without extensions (removing everything after the first period). Remove the hyphen and all text to the right of it (if present). Check if the list contains the filename, if it does, add the full file name to another list.

string[] files = GetImageList();
foreach (string file in files)
{
    FileInfo f = new FileInfo(file);
    //remove extension
    string fileName = f.Name.Substring(0,f.Name.LastIndexOf(".");
    //remove hyphen if present
    if (fileName.Contains("-"))
       fileName = fileName.Substring(0, fileName.LastIndexOf("-"));
    //check if it exists in the List.
    if (items.Contains(fileName))
        {
            //add f.FullName to new list.
            //or whatever else you need to do.
        }
}
Mikael
  • 1,002
  • 1
  • 11
  • 22