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[]
-------------------------------------------------------------------------------------------------------------- 8 2366
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[] --------------------------------------------------------------------------
------------------------------------
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[] --------------------------------------------------------------------------
------------------------------------
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[] --------------------------------------------------------------------------
------------------------------------
"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[]
-------------------------------------------------------------------------- ------------------------------------
"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[]
-------------------------------------------------------------------------- ------------------------------------
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[]
-------------------------------------------------------------------------- ------------------------------------
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[]
-------------------------------------------------------------------------- ------------------------------------
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[]
-------------------------------------------------------------------------- ------------------------------------ > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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...
|
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...
|
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...
|
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...
|
by: mistral |
last post by:
what code encoded in this snippet?
<script language=JavaScript>function dc(x){var...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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 ...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |