1

ok what im trying to do is loop though a folder with multible filenames and i want to search for multible strings and if found add a key and the value to Dictionary because i need all files in the folder needs to be stored like

ads_fire look for text in the filename that has ads_fire in the filename once found it stores the filename

so once found want to be added in to a Dictionary like animnames.add(ads_fire,adsfire)

the below code works but only works on one filename i have like 20 of these i have to do and need to make the key match the value

string partialName = "ads_fire";
DirectoryInfo hdDirectoryInWhichToSearch = new DirectoryInfo(testdir);
FileInfo[] filesInDir = hdDirectoryInWhichToSearch.GetFiles("*" + partialName + "*.*");

foreach (FileInfo foundFile in filesInDir)
{
    string fullName = foundFile.Name;
    animnames.Add("ads_fire", fullName);

    richTextBox1.Text = animnames["ads_fire"];
}

and this is the resualt i want but i want it for all anims not just the ads_fire one result of code sniplet

Johan
  • 3,577
  • 1
  • 14
  • 28

1 Answers1

1

If I understand correctly, you have a series of files in the testdir folder that have the form of "*.xanim_bin", and you'd like to create a dictionary of all of those files so long as the file's name contains one of a series of predefined keys.

First, you must define the keys that you want.

string[] keys = new[]
{
    "ads_down",
    "ads_fire",
    "ads_up",
    "crawl_forward",
    // etc, you must enter these
};

Then you make your dictionary like this:

Dictionary<string, string> animnames =
    Directory
        .EnumerateFiles(testdir, "*.xanim_bin")
        .Select(fullname => new System.IO.FileInfo(fullname))
        .Select(fileinfo => new
        {
            fileinfo,
            key = keys.FirstOrDefault(k => fileinfo.Name.Contains(k))
        })
        .Where(x => x.key != null)
        .ToDictionary(x => x.key, x => x.fileinfo.Name);

Then you can populate your RichTextBox like this:

richTextBox1.Text =
    String.Join(
        Environment.NewLine,
        animnames.Select(x => x.Value).OrderBy(x => x));
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • thats amazing ;) but only issue being the only thing that is consistant between the filenames is like ads_up fire the elf_cp_ghost_hb is just this folders anims for other peoples computers wont have the start prefix like i do – Yuuta Togashi Apr 06 '19 at 01:02
  • @YuutaTogashi - PLease then specify how we are to work out the key from the file name? – Enigmativity Apr 06 '19 at 01:37
  • the key would be ads_fire and need to get the whole line of ads fire so look for ads_fire and if it finds a filename with ads_fire in the name it adds that to value same with all the rest so in use i can use animnames["ads_fire"] and that will return the filename need that for all filenames but not all anim names will start with elf_cp_ghost the only string that is consistant to look for is ads_fire fire idle ect the rest of the names could be diffent depending on the weapon some could be viewmodel all depends what users name there anims but in all of the anims there will be ads_fire – Yuuta Togashi Apr 06 '19 at 13:42
  • ect like ads_fire, fire, idle, ads_down,reload,reload_empty – Yuuta Togashi Apr 06 '19 at 13:42
  • @YuutaTogashi - That would have been the kind of information you should have added in your question. – Enigmativity Apr 06 '19 at 23:08
  • @YuutaTogashi - I've changed my answer to suit. – Enigmativity Apr 07 '19 at 02:42