-
September 14th, 2010, 02:22 PM
#1
[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
-
September 14th, 2010, 02:43 PM
#2
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);
}
-
September 14th, 2010, 03:10 PM
#3
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.
-
September 14th, 2010, 03:50 PM
#4
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
}
-
September 14th, 2010, 03:50 PM
#5
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.
-
September 14th, 2010, 04:06 PM
#6
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.
-
September 14th, 2010, 04:14 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|