Hi guys,

I have a program im writing that needs to look for a file in a certain directory then when it find the file add the contents to a database.

I have the program working which does this however the filename changes depending on when it was created. The filename structure is as follows:

info_(datetime)

Where datetime changes depending on when it was created. So i basically need some code to look for the first file in a directory that start with info_ and it into a variable and then for that file execute a set of commands (i have this code already) then delete the file from the directory and move onto the next file. However new files will be dropped into the directory all the time so it needs to be in a loop. Any help wld be much apreciated

Dani AI

Generated

A concise, practical pattern to finish what started and to expand on ’s helpful suggestions.

The two common pitfalls are partial/locked files (the producer is still writing) and ambiguous ordering (what “first” means). The StreamReader constructor needs a filename string rather than a FileInfo object, so pass the file path. Event-driven processing with FileSystemWatcher is efficient, but always combine it with a periodic scan so no files are missed.

A robust workflow:

  • Discover matching files (e.g. glob "info_*") and sort by a clear key (LastWriteTime or CreationTime) to define “first.”
  • Try to obtain exclusive access (open with no sharing) with a short retry loop; if it cannot be opened, the file is likely still being written—retry later.
  • Process the file and only after a successful DB commit move the file to a processed archive (or an error folder on failure). Avoid immediate deletion until success is confirmed.

Example (C#) showing those ideas:

var incoming = @"C:\temp";
Directory.CreateDirectory(Path.Combine(incoming, "processed"));

foreach (var path in Directory.EnumerateFiles(incoming, "info_*")
                              .OrderBy(f => File.GetLastWriteTimeUtc(f)))
{
    FileStream fs = null;
    bool ok = false;
    for (int i = 0; i < 10; i++)
    {
        try { fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.None); ok = true; break; }
        catch (IOException) { Thread.Sleep(200); }
    }
    if (!ok) continue; // skip files still being written

    using (fs)
    using (var reader = new StreamReader(fs))
    {
        var payload = reader.ReadToEnd();
        // insert payload into DB, then on success:
        File.Move(path, Path.Combine(incoming, "processed", Path.GetFileName(path)));
    }
}

Additional notes: ask the producer to write to a temp name and rename when complete (atomic handoff). Use an error folder for files that fail processing. Combine FileSystemWatcher events with periodic scans for reliability.

Recommended Answers

All 3 Replies

Take a look at FileSystemWatcher (a component in the tool box).
It will detect when new files have been added to a directory.

Otherwise, you can use something like this is a timed loop:

DirectoryInfo dirinfo = new DirectoryInfo("C:\\temp");
   foreach (FileInfo fi in dirinfo.GetFiles("info_*"))
   {
         // do something with file
         fi.Delete();
   }

cool thats very useful,

trying the example you give i then need to open the file and read the lines:

StreamReader objReader = new StreamReader(fi);

But it gives errors:

Error	1	The best overloaded method match for 'System.IO.StreamReader.StreamReader(string)' has some invalid arguments	C:\Users\chris\Documents\work\uni\year3\project\visualproject\ConsoleApplication1\ConsoleApplication1\reports.cs	41	46	ConsoleApplication1
Error	2	Argument '1': cannot convert from 'System.IO.FileInfo' to 'string'	C:\Users\chris\Documents\work\uni\year3\project\visualproject\ConsoleApplication1\ConsoleApplication1\reports.cs	41	63	ConsoleApplication1

I think streamreader is expecting a string what can i do to correct this?

fi.FullName

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.