473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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[]
--------------------------------------------------------------------------------------------------------------
Nov 16 '05 #1
8 2356
Do this..

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

"RML" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
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[]
-------------------------------------------------------------------------- ------------------------------------

Nov 16 '05 #2
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" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
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[]
-------------------------------------------------------------------------- ------------------------------------

Nov 16 '05 #3
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" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
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[]
-------------------------------------------------------------------------- ------------------------------------

Nov 16 '05 #4

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:es********************@comcast.com...
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" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
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[]


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


Nov 16 '05 #5

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:es********************@comcast.com...
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" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
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[]


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


Nov 16 '05 #6
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" <te**@test.lk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:es********************@comcast.com...
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" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
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[]


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



Nov 16 '05 #7
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" <te**@test.lk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:es********************@comcast.com...
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" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
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[]


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



Nov 16 '05 #8
mm.... 50:50 let's not worry....

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

Nirosh.

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:4Y********************@comcast.com...
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" <te**@test.lk> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

"Nick Malik [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:es********************@comcast.com...
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" <rm*@robertlyon.com.au> wrote in message
news:uz**************@TK2MSFTNGP10.phx.gbl...
> 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[]


--------------------------------------------------------------------------
------------------------------------
>
>



Nov 16 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: m|sf|t | last post by:
All, Recently our TiVos were upgraded to include a webserver. Is it possible for me to run a PHP script from my machine and have it somehow retrieve a listing of all the files in subdirectory that...
2
by: Kavitha Rao | last post by:
Hi, I am getting the following errors while trying to run this snippet in Microsoft Visual C++.Can't seem to print the crc value stored. /* +++Date last modified: 05-Jul-1997 */ /* Crc - 32...
1
by: RML | last post by:
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...
3
by: kalamantina | last post by:
How to rewrite this snippet if you must implement IDisposable private static void OpenConnection() { string connectionString = GetConnectionString(); StringBuilder errorMessages = new...
2
by: mistral | last post by:
what code encoded in this snippet? <script language=JavaScript>function dc(x){var...
1
by: mmohanra | last post by:
I am trying to list files in a folder in a remote server. I am using Scripting.FileSystemObject. This seems to be really slow. After a long time the connection times out. Is there any other way to...
12
by: pedagani | last post by:
Dear comp.lang.c++, Could you make this snippet more efficient? As you see I have too many variables introduced in the code. //Read set of integers from a file on line by line basis in a STL...
3
by: froditus | last post by:
Hello everyone, is it possible to list files from directory other than in apache web directory? my web folder is placed on c:/ and i put my files in windows directory "d:/files/images/". I...
6
by: virtualweb | last post by:
Hi all. I am a self-taught beginner Perl programmer. Run into this snippet of code which works as an Email List Cleaner. The script first checks the sintax of a list of emails saved in an input.txt...
20
by: Netwatcher | last post by:
Hello, I've been trying to get a list of files from given directory in C++ (with win32) but i encountered some weird problems //test snippet #include <windows.h> #include <stdio.h> #include...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.