CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Hybrid View

  1. #1
    Join Date
    Jun 2008
    Posts
    154

    [RESOLVED] C# search directory for file

    I've done this before with EXACT file names.. but I don't know how to do it with partial file names.

    example

    say I want to find the file "Dirk"

    the actual file is...

    DirkWeapon_Dagger

    so basically find the first word of Dirk.

    here is my uncompleted method... then I ran into logic problems

    Code:
            private void LookForFile(string weaponName)
            {
                int stringLength = weaponName.Length;
                //add the addresses to the string array
                string[] files = Directory.GetFiles(GameRef.GetDocumentsDirectory + @"\My Documents\Visual Studio 2008\Projects\RPGBXL\RPGBXL\Content\Items\Weapon");
    
                foreach (string file in files)
                {
                    FileInfo f = new FileInfo(file);
                    if (f.Extension == ".xml")
                    {
                        //parses out that huge Directory address!
                        string filename = Regex.Match(file, @"[^\\]*$").Value;
                        string thisfile = filename.Remove(stringLength);
                        FilesToLoad.Add(filename);
                        if (thisfile == weaponName)
                        {
                        }
                    }
                }
            }
    Last edited by bixel; September 14th, 2010 at 02:40 PM. Reason: Updated method to make more sense

  2. #2
    Join Date
    Jan 2007
    Posts
    491

    Re: C# search directory for file

    Code:
    DirectoryInfo dir = new DirectoryInfo("MY_DIR_PATH");
    FileInfo[] files = dir.GetFiles();
    foreach(FileInfo f in files)
    {
          if(f.Name.StartsWith("dirk")) //or f.Name.Contains("dirk"), if it's not necessarily in the beginning of the file name
               filesToLoad.Add(f.Name);
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    154

    Re: C# search directory for file

    Thank you so much! And a lot less code!!

    now I just need to have it filter for differences between

    Dagger

    and

    Dagger of Doom, since searching for Dagger always returns Dagger of Doom 1st

    I guess it would need to use some filter... I'll come back and post the solution if I find it.
    Last edited by bixel; September 14th, 2010 at 03:23 PM.

  4. #4
    Join Date
    Jan 2007
    Posts
    491

    Re: C# search directory for file

    you could sort the files by their name.
    here's a quick example:
    Code:
                DirectoryInfo dir = new DirectoryInfo(@"C:\");
                var files = from f in dir.GetFiles()
                            orderby f.Name
                            select f;
                foreach (FileInfo f in files)
                    MessageBox.Show(f.Name);
    edit: acually, biged's solution is much better (and shorter)... sorry for confusing you.

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# search directory for file

    Directory.GetFiles also takes a search pattern, so...

    Code:
    string[] fileNames = Diectory.GetFiles("Dirk*.*");
    foreach(string filename in fileNames)
    {
        //do stuff
    }

  6. #6
    Join Date
    Jun 2008
    Posts
    154

    Re: C# search directory for file

    Hrmmm.. I switched up the Naming scheme so that when the Weapon gets saved out it looks like this

    Dagger-Weapon_Dagger
    Dagger of Doom-Weapon_Dagger

    now looking for "Dagger" loads perfectly fine as it considers Dagger- as one word
    however looking for "Dagger of Doom" always fails since it considers Dagger of Doom- as several words..

    I don't know regular expressions very well so I don't know what this does >>
    string[] fileNames = Directory.GetFiles("Dirk*.*");

    Oh I solved it - I sort of faked it out I guess.

    I changed the file saving scheme to this Weapon_Dagger_ + Name of Weapon + .xml, since this is how I wanted it anyway.

    so example: Weapon_Dagger_daggerofdoom.xml

    Code:
            private void LookForFile(string weaponName)
            {
                string directory = GameRef.GetDocumentsDirectory + @"\My Documents\Visual Studio 2008\Projects\RPGBXL\RPGBXL\Content\Items\Weapon\";
                string weapon = weaponName.Replace(" ", "");
                weapon = weapon.ToLower();
    
                DirectoryInfo dir = new DirectoryInfo(directory);
                FileInfo[] files = dir.GetFiles();
    
                foreach (FileInfo file in files)
                {
                    if (file.Name.Contains(weapon)) //or f.Name.Contains("dirk"), if it's not necessarily in the beginning of the file name
                    {
                        string thisWeapon = directory + file.Name;
                        ReadWeapon(thisWeapon);
                    }
                }
            }
    works just fine!
    Last edited by bixel; September 14th, 2010 at 04:36 PM.

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: C# search directory for file

    It is not a regular expression. The asterisk is a wildcard, i.e., it will match *anything*. The only required bit is that the file starts with "Dirk".

    http://msdn.microsoft.com/en-us/library/07wt70x2.aspx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured