.Net Framework 1.1 - listing directories containing full stop (period)

  • Thread starter Thread starter stainless
  • Start date Start date
S

stainless

Given I have a folder root called C:\Users\, I want to get a list of
all the sub-folders directly under this C:\Users| folder that contain
a full stop (.), but only at the first sub-folder level.

Using the example folder structures below:

C:\Users\philip.marlowe\monkeynuts
C:\Users\philip.marlowe\zebra.stripes
C:\Users\Jethro\popular.front
C:\Users\john.smith\monkeynuts
C:\Users\john.smith\cabbage
C:\Users\boris.johnson\
C:\Users\femor
C:\Users\full.stops.found

My search should retrieve:

C:\Users\philip.marlowe\
C:\Users\john.smith\
C:\Users\boris.johnson\
C:\Users\full.stops.found

This is only the sub-folders immediately below C:\Users\ that contain
the full stop

I am struggling with the code for this even though I know it should be
simple. Any help appreciated.

Cheers

Mark
 
Given I have a folder root called C:\Users\, I want to get a list of
all the sub-folders directly under this C:\Users| folder that contain
a full stop (.), but only at the first sub-folder level.

Using the example folder structures below:

C:\Users\philip.marlowe\monkeynuts
C:\Users\philip.marlowe\zebra.stripes
C:\Users\Jethro\popular.front
C:\Users\john.smith\monkeynuts
C:\Users\john.smith\cabbage
C:\Users\boris.johnson\
C:\Users\femor
C:\Users\full.stops.found

My search should retrieve:

C:\Users\philip.marlowe\
C:\Users\john.smith\
C:\Users\boris.johnson\
C:\Users\full.stops.found

This is only the sub-folders immediately below C:\Users\ that contain
the full stop

I am struggling with the code for this even though I know it should be
simple. Any help appreciated.

Cheers

Mark

The File and Directory Classes have a overload that can be very
usefull that accepts file/directory filtering. For example:

string[] subDirs = System.IO.Directory.GetDirectories(@"C:\Users\",
"*.*");
 
string[] subDirs = System.IO.Directory.GetDirectories(@"C:\Users\",
"*.*");
I did try that but "*.*" returns every sub directory. I think the
issue is something to do with the "." itself. It looks like this acts
as a special "select all" value (I found some info on the web
regarding DOT being a special metacharacter). I may not have fully
understood how it is being treated but it is certainly retrieving
every folder name
 
string[] subDirs = System.IO.Directory.GetDirectories(@"C:\Users\",
"*.*");
I did try that but "*.*" returns every sub directory. I think the
issue is something to do with the "." itself. It looks like this acts
as a special "select all" value (I found some info on the web
regarding DOT being a special metacharacter). I may not have fully
understood how it is being treated but it is certainly retrieving
every folder name

I'm sure there are better solutions but this works...

string dirPath = @"c:\users";
string[] dirs = Directory.GetDirectories(dirPath);
foreach (string d in dirs)
{
if (d.IndexOf('.') > -1)
{
// do something with the directory...
Debug.WriteLine(d);
}
}

// Anders
 
string[] subDirs = System.IO.Directory.GetDirectories(@"C:\Users\",
"*.*");

I did try that but "*.*" returns every sub directory. I think the
issue is something to do with the "." itself. It looks like this acts
as a special "select all" value (I found some info on the web
regarding DOT being a special metacharacter). I may not have fully
understood how it is being treated but it is certainly retrieving
every folder name

Right, didnt test he code. My bad. Anders code will do the trick.
 
I'm sure there are better solutions but this works...

string dirPath = @"c:\users";
string[] dirs = Directory.GetDirectories(dirPath);
foreach (string d in dirs)
{
if (d.IndexOf('.') > -1)
{
// do something with the directory...
Debug.WriteLine(d);
}
}

A LINQ version would be :

string dirPath = @"C:\users";
IList<string> dirs = (from dir in Directory.GetDirectories(dirPath)
where dir.Contains(".")
select dir).ToList();

regards
A.G.
 
I'm sure there are better solutions but this works...

string dirPath = @"c:\users";
string[] dirs = Directory.GetDirectories(dirPath);
foreach (string d in dirs)
{
if (d.IndexOf('.')> -1)
{
// do something with the directory...
Debug.WriteLine(d);
}
}

A LINQ version would be :

string dirPath = @"C:\users";
IList<string> dirs = (from dir in Directory.GetDirectories(dirPath)
where dir.Contains(".")
select dir).ToList();

But .NET 1.1 does not have LINQ.

:-)

Arne
 
Back
Top