473,396 Members | 2,024 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,396 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
Nov 15 '05 #1
2 1442
"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
Nov 15 '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

Nov 15 '05 #3

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

Similar topics

3
by: David T. Ashley | last post by:
Hi, PHP allows the "library" which can contain an arbitrary number of include files. Can I put all the include files right in this directory, or should I create subdirectories? My question...
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: 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. ...
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
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,...
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
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...
0
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,...
0
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...

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.