473,394 Members | 1,840 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,394 software developers and data experts.

Number of Specific Files in A Directory

I am working on some code that will be used in a Windows Service that will
monitor specific files in a queue.

I would like to get an integer value of the amount of specfic files in a
directory. Now I have been using this code to get what I need.

private DirectoryInfo di ;
di = new DirectoryInfo( @"c:\temp" ) ;
int files_in_directory_value = di.GetFileSystemInfos( "*.abc" ).Length ;

Now the issue I beleive that I will face is that the directory I will be
checking at times can have 40K+ files that match the search string. Seeing
that GetFileSystemInfos returns an array of all the files that I am
searching on I am concerned about potential memory usage of this structure
and just general decrease in performance. I am sure everyone would agree
that just getting the integer value would be better.

Any thoughts on other framework functions to get this info?.
Amy
Jul 21 '05 #1
2 3612
"Ignacio Machin" <ignacio.machin_AT_dot.state.fl.us> wrote:
Hi Ami,

Have you consider use a FileSystemWatcher?
You can use a counter and increment it on the Created event and
decrement it on the Deleted event.


Personally, I wouldn't do this, after my troubles with FileSystemWatcher
tonight. It looks like it can fire events multiple times, even though the
filtered condition on the file(s) in question has not changed. It's not
predictable enough for counting files, without additional code to get
around the underlying Win32 flakiness. Even then I wouldn't trust it. I've
got a kludge in my code that seems to work, but as this misbehavior is not
documented, it's really just my guess at how to make it work.

Anyway, it wouldn't be a robust way of doing what Ami wants, because it
would not know if the file count was correct. What if there were files
already in the directory when the FileSystemWatcher was created? They'd
never be counted.

Ami: My first thought, assuming there isn't something in the .NET framework
to just count the files, would be to use interop to call the Win32 API and
count the files individually.

--
harry
Jul 21 '05 #2
Ami

System performance is going to be incredibly poor with 40k files in the same
directory. Opening a file is going to take for ever. You should probably
be using a message queue here. However, assuming you are stuck with it ...

I agree with Harry that FileSystemWatcher is unreliable, and it doesn't
account for the files that were present when the program started up.

Try:

string path = @"C:\MyFiles";
string pattern = "*.xml";
string[] files = Directory.GetFiles(path, pattern);
int numberOfFiles = files.Length;

Another problem you may get is that you'll occasionally try to process files
that are still being written. The safe way out of this is to insist that
the file writer write each file with the wrong type - .tmp, say - then
renames the file to the correct file after closing it. Otherwise you'll
have to develop a strategy for dealing with files you can't open because
they are still being written.

Nick
"Amy L." <am**@paxemail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
I am working on some code that will be used in a Windows Service that will
monitor specific files in a queue.

I would like to get an integer value of the amount of specfic files in a
directory. Now I have been using this code to get what I need.

private DirectoryInfo di ;
di = new DirectoryInfo( @"c:\temp" ) ;
int files_in_directory_value = di.GetFileSystemInfos( "*.abc" ).Length ;

Now the issue I beleive that I will face is that the directory I will be
checking at times can have 40K+ files that match the search string. Seeing that GetFileSystemInfos returns an array of all the files that I am
searching on I am concerned about potential memory usage of this structure
and just general decrease in performance. I am sure everyone would agree
that just getting the integer value would be better.

Any thoughts on other framework functions to get this info?.
Amy

Jul 21 '05 #3

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

Similar topics

4
by: Bart Plessers \(artabel\) | last post by:
Hello, I have an asp script that lists the files in a directory: CurrentPATH = "c:\temp\" Set oFSO = CreateObject("Scripting.FileSystemObject") Set oFolder = oFSO.GetFolder(CurrentPATH) Set...
2
by: 73blazer | last post by:
Hello, I'm writing some C++ code, and I need to be able to find the number of files in a given directory. Is it possible under AIX4.3.3 with C++ 3.6.4? I cannot seem to locate anything of this...
6
by: E Pease | last post by:
I have been trying to edit the Explorer example by Dev Ashish I found on www.msvp.org (I think). I have been all over the net so I am not quite sure where the file came from. I like the way it...
2
by: Amy L. | last post by:
I am working on some code that will be used in a Windows Service that will monitor specific files in a queue. I would like to get an integer value of the amount of specfic files in a directory. ...
1
by: supster | last post by:
EDIT: I no longer need only specific file types in a directory, just the total number of files! Is there a more efficient way other than this: string files = Directory.GetFiles(path); count =...
7
by: matvdl | last post by:
I have migrated my asp application to asp.net some time ago - but I am still having some difficulties in understanding the best way to mange some tasks. I currently have a page that loads a aspx...
1
by: sasikumar | last post by:
i have created a new folder inside project(say update for storing update files for software) how can i get total number of files in that folder.what will be full path for accessing that...
6
by: flavourofbru | last post by:
Hi, I have a small question. How do I compute the number of files(which are specific such as .doc) in a directory in C++. For example, if I have 10 files of type .doc format in a directory,...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
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 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.