473,320 Members | 1,979 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,320 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 3607
"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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.