list files in subdirectory trouble!! please look at this snippet

R

RML

hey guys,

i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to output correctly, i have included
the original code, my modified code, and the output from my modifed code..
why does it list the files as "System.IO.FileInfo[]"???

thanks

--------------------------------------------------------------------------------------------------------------
ORIGINAL C# CODE
--------------------------------------------------------------------------------------------------------------

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"c:\");

// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*p*");

Console.WriteLine("Number of directories with a p: {0}",
dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles("*e*").Length);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
MY MODIFICATIONS
--------------------------------------------------------------------------------------------------------------
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"h:\");

// Get Subdirectories
DirectoryInfo[] dirs = di.GetDirectories();

Console.WriteLine("Number of directories is {0}", dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}


--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
THE OUTPUT
--------------------------------------------------------------------------------------------------------------
Number of directories is 4
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
--------------------------------------------------------------------------------------------------------------
 
C

Champika Nirosh

Do this..

string[] files = System.IO.Directory.GetFiles("D:\\WINDOWS", "*.doc");
Regards,
Nirosh.

RML said:
hey guys,

i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to output correctly, i have included
the original code, my modified code, and the output from my modifed code..
why does it list the files as "System.IO.FileInfo[]"???

thanks

-------------------------------------------------------------------------- ------------------------------------
ORIGINAL C# CODE
-------------------------------------------------------------------------- ------------------------------------

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"c:\");

// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*p*");

Console.WriteLine("Number of directories with a p: {0}",
dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles("*e*").Length);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

-------------------------------------------------------------------------- ------------------------------------
--------------------------------------------------------------------------
------------------------------------
MY MODIFICATIONS
-------------------------------------------------------------------------- ------------------------------------
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"h:\");

// Get Subdirectories
DirectoryInfo[] dirs = di.GetDirectories();

Console.WriteLine("Number of directories is {0}", dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}


-------------------------------------------------------------------------- ------------------------------------
--------------------------------------------------------------------------
------------------------------------
THE OUTPUT
-------------------------------------------------------------------------- ------------------------------------
Number of directories is 4
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
-------------------------------------------------------------------------- ------------------------------------
 
N

Nick Malik [Microsoft]

because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}

diNext.GetFiles() will return an array of files that matches the expression.
How do you want .net to create a string that "represents" a reference to an
array?

The default way to do this is to print out the type, which is what you are
getting. If you want to print out the actual contents, then you will need
an inner loop to scan through the array, get the file names, and print them
out.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
RML said:
hey guys,

i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to output correctly, i have included
the original code, my modified code, and the output from my modifed code..
why does it list the files as "System.IO.FileInfo[]"???

thanks

-------------------------------------------------------------------------- ------------------------------------
ORIGINAL C# CODE
-------------------------------------------------------------------------- ------------------------------------

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"c:\");

// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*p*");

Console.WriteLine("Number of directories with a p: {0}",
dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles("*e*").Length);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

-------------------------------------------------------------------------- ------------------------------------
--------------------------------------------------------------------------
------------------------------------
MY MODIFICATIONS
-------------------------------------------------------------------------- ------------------------------------
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"h:\");

// Get Subdirectories
DirectoryInfo[] dirs = di.GetDirectories();

Console.WriteLine("Number of directories is {0}", dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}


-------------------------------------------------------------------------- ------------------------------------
--------------------------------------------------------------------------
------------------------------------
THE OUTPUT
-------------------------------------------------------------------------- ------------------------------------
Number of directories is 4
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
-------------------------------------------------------------------------- ------------------------------------
 
C

Champika Nirosh

Nick Malik said:
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}

diNext.GetFiles() will return an array of files that matches the expression.
How do you want .net to create a string that "represents" a reference to an
array?

The default way to do this is to print out the type, which is what you are
getting. If you want to print out the actual contents, then you will need
an inner loop to scan through the array, get the file names, and print them
out.

After looking at the code he has posted ... I wonder.. there are other
better ways of doing this..

since he is not worried about recursively navigating through the
subdirectories..

//System.Collections.ArrayList files = new
System.Collections.ArrayList();
foreach (string dir in System.IO.Directory.GetDirectories("c:\\", "*p*"))
{
//files.AddRange(System.IO.Directory.GetFiles(dir, "*.txt"));

Console.WriteLine("The number of files in {0} with an e is {1}", dir,
System.IO.Directory.GetFiles(dir, "*.txt").Length);

}

may do what he need..

Nirosh.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
RML said:
hey guys,

i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to output correctly, i have included
the original code, my modified code, and the output from my modifed code..
why does it list the files as "System.IO.FileInfo[]"???

thanks

--------------------------------------------------------------------------
------------------------------------
ORIGINAL C# CODE
--------------------------------------------------------------------------
------------------------------------

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"c:\");

// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*p*");

Console.WriteLine("Number of directories with a p: {0}",
dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles("*e*").Length);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

--------------------------------------------------------------------------
------------------------------------
MY MODIFICATIONS
--------------------------------------------------------------------------
------------------------------------
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"h:\");

// Get Subdirectories
DirectoryInfo[] dirs = di.GetDirectories();

Console.WriteLine("Number of directories is {0}", dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

--------------------------------------------------------------------------
------------------------------------
THE OUTPUT
--------------------------------------------------------------------------
------------------------------------
Number of directories is 4
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[] [/QUOTE]
 
N

Nick Malik [Microsoft]

Hello Champika,

I believe that the OP's code did not reflect his intent. He complained
about not getting the names of the files, which leads me to believe that he
actually wants to see the names of the files. Your code will get him to the
place he is at, more succinctly, but I do not believe that it is the place
he wants to be at.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Champika Nirosh said:
Nick Malik said:
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}

diNext.GetFiles() will return an array of files that matches the expression.
How do you want .net to create a string that "represents" a reference to an
array?

The default way to do this is to print out the type, which is what you are
getting. If you want to print out the actual contents, then you will need
an inner loop to scan through the array, get the file names, and print them
out.

After looking at the code he has posted ... I wonder.. there are other
better ways of doing this..

since he is not worried about recursively navigating through the
subdirectories..

//System.Collections.ArrayList files = new
System.Collections.ArrayList();
foreach (string dir in System.IO.Directory.GetDirectories("c:\\", "*p*"))
{
//files.AddRange(System.IO.Directory.GetFiles(dir, "*.txt"));

Console.WriteLine("The number of files in {0} with an e is {1}", dir,
System.IO.Directory.GetFiles(dir, "*.txt").Length);

}

may do what he need..

Nirosh.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
RML said:
hey guys,

i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th directory that
end with .doc. i cant seem to get it to output correctly, i have included
the original code, my modified code, and the output from my modifed code..
why does it list the files as "System.IO.FileInfo[]"???

thanks

--------------------------------------------------------------------------
--------------------------------------------------------------------------
------------------------------------

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"c:\");

// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*p*");

Console.WriteLine("Number of directories with a p: {0}",
dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine("The number of files in {0} with an
e
is
{1}", diNext,
diNext.GetFiles("*e*").Length);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
------------------------------------
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"h:\");

// Get Subdirectories
DirectoryInfo[] dirs = di.GetDirectories();

Console.WriteLine("Number of directories is {0}", dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
------------------------------------
Number of directories is 4
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
 
C

Champika Nirosh

mm.... 50:50 let's not worry....

anyway both option are there so the choice indeed is belonged to him..

Nirosh.

Nick Malik said:
Hello Champika,

I believe that the OP's code did not reflect his intent. He complained
about not getting the names of the files, which leads me to believe that he
actually wants to see the names of the files. Your code will get him to the
place he is at, more succinctly, but I do not believe that it is the place
he wants to be at.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Champika Nirosh said:
Nick Malik said:
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}

diNext.GetFiles() will return an array of files that matches the expression.
How do you want .net to create a string that "represents" a reference
to
an
array?

The default way to do this is to print out the type, which is what
you
are
getting. If you want to print out the actual contents, then you will need
an inner loop to scan through the array, get the file names, and print them
out.

After looking at the code he has posted ... I wonder.. there are other
better ways of doing this..

since he is not worried about recursively navigating through the
subdirectories..

//System.Collections.ArrayList files = new
System.Collections.ArrayList();
foreach (string dir in System.IO.Directory.GetDirectories("c:\\", "*p*"))
{
//files.AddRange(System.IO.Directory.GetFiles(dir, "*.txt"));

Console.WriteLine("The number of files in {0} with an e is {1}", dir,
System.IO.Directory.GetFiles(dir, "*.txt").Length);

}

may do what he need..

Nirosh.
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
hey guys,

i am looking at this piece of code that lists numbers of files in a
directory. i want to convert it so it lists the files in th
directory
that
end with .doc. i cant seem to get it to output correctly, i have included
the original code, my modified code, and the output from my modifed code..
why does it list the files as "System.IO.FileInfo[]"???

thanks

--------------------------------------------------------------------------
--------------------------------------------------------------------------
------------------------------------

using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"c:\");

// Get only subdirectories that contain the letter "p."
DirectoryInfo[] dirs = di.GetDirectories("*p*");

Console.WriteLine("Number of directories with a p: {0}",
dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine("The number of files in {0} with
an
e
is
{1}", diNext,
diNext.GetFiles("*e*").Length);
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
------------------------------------
using System;
using System.IO;

class Test
{
public static void Main()
{
try
{
DirectoryInfo di = new DirectoryInfo(@"h:\");

// Get Subdirectories
DirectoryInfo[] dirs = di.GetDirectories();

Console.WriteLine("Number of directories is {0}",
dirs.Length);

// Count all the files in each subdirectory that contain the
letter "e."
foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLine(diNext.GetFiles("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
}
}

--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
------------------------------------
Number of directories is 4
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
System.IO.FileInfo[]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top