473,655 Members | 3,072 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString());
}
}
}
--------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------
THE OUTPUT
--------------------------------------------------------------------------------------------------------------
Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
--------------------------------------------------------------------------------------------------------------
Nov 16 '05 #1
8 2385
Do this..

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

"RML" <rm*@robertlyon .com.au> wrote in message
news:uz******** ******@TK2MSFTN GP10.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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString());
}
}
}
-------------------------------------------------------------------------- ------------------------------------
-------------------------------------------------------------------------- ------------------------------------ THE OUTPUT
-------------------------------------------------------------------------- ------------------------------------ Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
-------------------------------------------------------------------------- ------------------------------------

Nov 16 '05 #2
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLi ne(diNext.GetFi les("*.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******** ******@TK2MSFTN GP10.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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString());
}
}
}
-------------------------------------------------------------------------- ------------------------------------
-------------------------------------------------------------------------- ------------------------------------ THE OUTPUT
-------------------------------------------------------------------------- ------------------------------------ Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
-------------------------------------------------------------------------- ------------------------------------

Nov 16 '05 #3
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLi ne(diNext.GetFi les("*.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******** ******@TK2MSFTN GP10.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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString());
}
}
}
-------------------------------------------------------------------------- ------------------------------------
-------------------------------------------------------------------------- ------------------------------------ THE OUTPUT
-------------------------------------------------------------------------- ------------------------------------ Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
-------------------------------------------------------------------------- ------------------------------------

Nov 16 '05 #4

"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:es******** ************@co mcast.com...
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLi ne(diNext.GetFi les("*.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.Collecti ons.ArrayList files = new
System.Collecti ons.ArrayList() ;
foreach (string dir in System.IO.Direc tory.GetDirecto ries("c:\\", "*p*"))
{
//files.AddRange( System.IO.Direc tory.GetFiles(d ir, "*.txt"));

Console.WriteLi ne("The number of files in {0} with an e is {1}", dir,
System.IO.Direc tory.GetFiles(d ir, "*.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******** ******@TK2MSFTN GP10.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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is {1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString());
}
}
}


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


--------------------------------------------------------------------------
------------------------------------
THE OUTPUT


--------------------------------------------------------------------------
------------------------------------
Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]


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


Nov 16 '05 #5

"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:es******** ************@co mcast.com...
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLi ne(diNext.GetFi les("*.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.Collecti ons.ArrayList files = new
System.Collecti ons.ArrayList() ;
foreach (string dir in System.IO.Direc tory.GetDirecto ries("c:\\", "*p*"))
{
//files.AddRange( System.IO.Direc tory.GetFiles(d ir, "*.txt"));

Console.WriteLi ne("The number of files in {0} with an e is {1}", dir,
System.IO.Direc tory.GetFiles(d ir, "*.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******** ******@TK2MSFTN GP10.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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is {1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString());
}
}
}


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


--------------------------------------------------------------------------
------------------------------------
THE OUTPUT


--------------------------------------------------------------------------
------------------------------------
Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]


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


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******** ********@tk2msf tngp13.phx.gbl. ..

"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:es******** ************@co mcast.com...
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLi ne(diNext.GetFi les("*.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.Collecti ons.ArrayList files = new
System.Collecti ons.ArrayList() ;
foreach (string dir in System.IO.Direc tory.GetDirecto ries("c:\\",

"*p*")) {
//files.AddRange( System.IO.Direc tory.GetFiles(d ir, "*.txt"));

Console.WriteLi ne("The number of files in {0} with an e is {1}", dir,
System.IO.Direc tory.GetFiles(d ir, "*.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******** ******@TK2MSFTN GP10.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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString()); }
}
}


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


--------------------------------------------------------------------------
------------------------------------
THE OUTPUT


--------------------------------------------------------------------------
------------------------------------
Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]


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



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******** ********@tk2msf tngp13.phx.gbl. ..

"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:es******** ************@co mcast.com...
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLi ne(diNext.GetFi les("*.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.Collecti ons.ArrayList files = new
System.Collecti ons.ArrayList() ;
foreach (string dir in System.IO.Direc tory.GetDirecto ries("c:\\",

"*p*")) {
//files.AddRange( System.IO.Direc tory.GetFiles(d ir, "*.txt"));

Console.WriteLi ne("The number of files in {0} with an e is {1}", dir,
System.IO.Direc tory.GetFiles(d ir, "*.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******** ******@TK2MSFTN GP10.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.File Info[]"???

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.GetDirectori es("*p*");

Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with an e is
{1}", diNext,
diNext.GetFiles ("*e*").Length) ;
}
}
catch (Exception e)
{
Console.WriteLi ne("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.GetDirectori es();

Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
}
}
catch (Exception e)
{
Console.WriteLi ne("The process failed: {0}", e.ToString()); }
}
}


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


--------------------------------------------------------------------------
------------------------------------
THE OUTPUT


--------------------------------------------------------------------------
------------------------------------
Number of directories is 4
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]
System.IO.FileI nfo[]


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



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*******@hotm ail.nospam.com> wrote in message
news:4Y******** ************@co mcast.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******** ********@tk2msf tngp13.phx.gbl. ..

"Nick Malik [Microsoft]" <ni*******@hotm ail.nospam.com> wrote in message
news:es******** ************@co mcast.com...
because this line is not good:

foreach (DirectoryInfo diNext in dirs)
{
Console.WriteLi ne(diNext.GetFi les("*.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.Collecti ons.ArrayList files = new
System.Collecti ons.ArrayList() ;
foreach (string dir in System.IO.Direc tory.GetDirecto ries("c:\\",

"*p*"))
{
//files.AddRange( System.IO.Direc tory.GetFiles(d ir, "*.txt"));

Console.WriteLi ne("The number of files in {0} with an e is {1}", dir,
System.IO.Direc tory.GetFiles(d ir, "*.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******** ******@TK2MSFTN GP10.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.File Info[]"???
>
> 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.GetDirectori es("*p*");
>
> Console.WriteLi ne("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.WriteLi ne("The number of files in {0} with
an e
is
> {1}", diNext,
> diNext.GetFiles ("*e*").Length) ;
> }
> }
> catch (Exception e)
> {
> Console.WriteLi ne("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.GetDirectori es();
>
> Console.WriteLi ne("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.WriteLi ne(diNext.GetFi les("*.doc"));
> }
> }
> catch (Exception e)
> {
> Console.WriteLi ne("The process failed: {0}", e.ToString()); > }
> }
> }
>
>


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


--------------------------------------------------------------------------
------------------------------------
> THE OUTPUT


--------------------------------------------------------------------------
------------------------------------
> Number of directories is 4
> System.IO.FileI nfo[]
> System.IO.FileI nfo[]
> System.IO.FileI nfo[]
> System.IO.FileI nfo[]


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



Nov 16 '05 #9

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

Similar topics

1
2977
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 are on the TiVo ? The remote addresses are: http://(my tivo ip)/ChannelLogo/ and http://(my tivo ip)/images/
2
2585
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 BIT ANSI X3.66 CRC checksum files */ #include <stdio.h> #include "crc.h"
1
340
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 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
3
1895
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 StringBuilder();
2
1677
by: mistral | last post by:
what code encoded in this snippet? <script language=JavaScript>function dc(x){var...
1
2567
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 list files in a folder? Below is the current code: Dim fs, f, fc Set fs = CreateObject("Scripting.FileSystemObject") Set f = fs.GetFolder(Whichfolder) Set fc = f.files We were using Scripting.FileSystemObject for uploading files to...
12
1717
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 set //fp is pre-defined for(;!fp.eof();) { string linestr;
3
2619
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 want those files to be able to view in client browser. when i tested using server computer it succeed. but from client computer i got nothing. I checked the html sources and the files is directly loaded from "d:/
6
1640
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 file, then checks if those emails already exist in a sever file, lastly those emails which pass the first two test are verifyed whether they are true email boxes that exist in a remote server. I installed the Net::validMX module in my server and...
20
47748
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 <iostream> int main()
0
8380
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8296
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8710
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4299
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2721
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1598
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.