473,385 Members | 2,274 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,385 software developers and data experts.

Reading the first file from a directory.

Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file system
queue gets all that large (any more than a thousand or so messages in
the queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list
of files, so that I don't read them from disk every time. However,
there's a fair amount of effort involved there, because my program
reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig
Mar 23 '06 #1
4 1466

Sounds like what you need is a variation of the dir command switches.

From the command line, you can do:

dir /od

To sort by the data timestamp and then you could pick off just the first
file. Maybe DirectoryInfo will let you sort by date timestamp.
Craig Vermeer wrote:
Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file system
queue gets all that large (any more than a thousand or so messages in
the queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list
of files, so that I don't read them from disk every time. However,
there's a fair amount of effort involved there, because my program
reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig

--
Get the new
http://www.you-read-it-here-first.com
content from Texeme Textcasting Technology
Mar 23 '06 #2
John Bailo wrote:

Sounds like what you need is a variation of the dir command switches.

From the command line, you can do:
You could use this:

http://www.shanebauer.com/Weblog/Per...888a11433.aspx

I'm assuming you are doing a FIFO queue so getting the earliest date
would work for you.

dir /od

To sort by the data timestamp and then you could pick off just the first
file. Maybe DirectoryInfo will let you sort by date timestamp.
Craig Vermeer wrote:
Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file
system queue gets all that large (any more than a thousand or so
messages in the queue).

I know why. It's because my code to grab a single message off the
queue looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get
the first file in the directory.

One approach I can think of is to do some in-memory caching of the
list of files, so that I don't read them from disk every time.
However, there's a fair amount of effort involved there, because my
program reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig


--
Get the new
http://www.you-read-it-here-first.com
content from Texeme Textcasting Technology
Mar 23 '06 #3
Hi Craig,

have you considered having a service that uses FileSystemWatcher to maintain
a listing of the files in your directory?
The list could be as simple as a text file in that directory through to any
level of sophistication you might like to best fit your overall applciation
architecture.
It would at least allow you to go directly to a single point for your next
file.

Good Luck
Gerard
"Craig Vermeer" wrote:
Hi All,

I have a program that's using the file system as a queuing mechanism,
and it's consuming an inordinate amount of CPU time when the file system
queue gets all that large (any more than a thousand or so messages in
the queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it
wants to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list
of files, so that I don't read them from disk every time. However,
there's a fair amount of effort involved there, because my program
reading the messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know
of a very efficient way to just pull a single file from a directory?

Thanks,
Craig

Mar 23 '06 #4
I would be inclined to use a combination of a FileSystemWatcher component
and a Queue object.

At application startup, start the FileSystemWatcher to wtach for new files
in your target folder, then use the GetFiles method and enqueue each
filename to the Queue object.

When your application wants to get a message from the queue simply

- Check the length of the Queue object (the number of entries)

- Dequeue the first entry into a string (filename)

- Check for the existence of the file

- Process it if it exists

The check for the existence of the file will cater for the potential for the
same filename to get put in the Queue object twice if the FileSystemWatcher
happens to fire bewteen the time you start it and the time you execute the
GetFiles method.

In my view it is best to do it that way round because if you do it the other
way round the is the potential for a file to be missed of it is created
between executing the GetFiles method and starting the FileSystemWatcher.
"Craig Vermeer" <ve******@sentdotcom.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Hi All,

I have a program that's using the file system as a queuing mechanism, and
it's consuming an inordinate amount of CPU time when the file system queue
gets all that large (any more than a thousand or so messages in the
queue).

I know why. It's because my code to grab a single message off the queue
looks something like this :

//m_folder is a DirectoryInfo object
FileInfo file = m_folder.GetFiles("*.xml")[0];

So, it's pulling info about every file in the queue _every time_ it wants
to get a message from the queue.

Where I'm struggling, though, is finding a more efficient way to get the
first file in the directory.

One approach I can think of is to do some in-memory caching of the list of
files, so that I don't read them from disk every time. However, there's a
fair amount of effort involved there, because my program reading the
messages from the queue is multithreaded.

So, before I go down that route I thought I'd ask around. Anyone know of
a very efficient way to just pull a single file from a directory?

Thanks,
Craig

Mar 24 '06 #5

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

Similar topics

6
by: bart_nessux | last post by:
Hello, I have some Macbinary files on a PC. I want to recursively read these files and remove the first 128 bytes of the files if they contain the macbinary header info. I know how to read...
1
by: Joel Goldstick | last post by:
I wanted to write a simple page to let me choose a directory and then list the files in it. The end goal was to make an easy way to copy all the file names in a directory. I tested with Opera7,...
1
by: Michael Palmer | last post by:
I'm reading xml from SQL Server 2K with VB.net using an XSD schema file and SQLXML 3.0. I have the below code working fine, but I'd like to change the code from reading the schema file from a...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
4
by: Jason Kumpf | last post by:
OK I've been staring at this code all day and still with everything I have tried I cannot figure out two problems I am having. Once is why the space limit for the directory I create in the code...
2
by: MSDN | last post by:
Hello, I have the following code below that does not work for me because the directory has millions of files. foreach (string ofile in System.IO.Directory.GetFiles(strMyDir)) { ...
2
by: =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?= | last post by:
Hi; I am trying to test reading a html file over http. So in IIS I create a virtual directory and then in my NTFS permissions I set that directory to only allow read from Domain Users. (I also...
3
by: Joakim Hove | last post by:
Hello, I am reading a filename from the user - the loop is typically like this: 1. Read a directory from the user. 2. Read several filenames from the user. The filenames read in 2. above...
4
by: Laharl | last post by:
My Operating Systems professor has assigned homework that basically boils down to implementing ls -lra, but with a different output format. In other words, list the files and subdirectories (and a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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...

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.