In file text substitution

Posted on 2005-03-25
Medium Priority
Last Modified: 2013-11-18
I am a novice programmer, but trying to learn how to solve a requirement. I have a tool whose config file contains a staticly map to a file. Unfortunately that file changes regularly (including filename, and possibly extension) and I need the tool to reference the new file. I want to create some code to search a directory for the most recently created file, which then takes that filename, and substitutes the static entry in the tool's configuration file.

I have been searching a little and have absolutely no idea where to begin. I only have tools to work with Visual Studio.Net.

Does anybody have any suggestions?

Question by:Dodger42

Expert Comment

ID: 13634980
u can write a simple c program to find the list of files in the directory.. u can also get the last modified time for that file. u have to get the latest modified file name first.. u have to use "findfirst" & "findnext" command.

then the things are easy. u just open ur config file and replace it ...

LVL 22

Expert Comment

ID: 13635285
1. Is the file always in the same directory?  
2. If the name and/or extension of the file changes, what would a program look for to identify it?  Might "old" versions of the file still exist?
3. If presented with more than one possibility, would program choose the one with latest altered date/time?

The code to do this is fairly simple if the above answers are available.
LVL 23

Expert Comment

ID: 13637046
Take a look at this , you can search based on different file criteria etc using this :

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


Expert Comment

ID: 13638213
This is very easy to do in DotNet.  Here's a c# example:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;

public static void Main()
                  System.IO.FileSystemWatcher f=new System.IO.FileSystemWatcher("c:\\test");
                  f.NotifyFilter=NotifyFilters.FileName | NotifyFilters.Attributes | NotifyFilters.LastAccess | NotifyFilters.LastWrite | NotifyFilters.Security | NotifyFilters.Size;

                  f.Created+=new System.IO.FileSystemEventHandler(Message);
                  f.Deleted+=new System.IO.FileSystemEventHandler(Message);
                  Application.Run(new Form1());


            public static void Message(object o, System.IO.FileSystemEventArgs e)
                  MessageBox.Show(e.Name.ToString() + " " + e.ChangeType.ToString());

// c:\test is not being monitored for new files/deleted files, among other things.  Play with it.  Good luck!

Author Comment

ID: 13638690
1. Is the file always in the same directory?  

2. If the name and/or extension of the file changes, what would a program look for to identify it?  Might "old" versions of the file still exist?
Filename will change incrementally, their are two different extension possibilities. Need to filter based on those two possibilities.

3. If presented with more than one possibility, would program choose the one with latest altered date/time?
The latest file with the permitted extension would always be selected.

Thanks Heaps!

Author Comment

ID: 13638692
Sorry, my C is average, but will give that a shot GGalbo! I am more familiar with vb.net ;)


Accepted Solution

ggalbo earned 500 total points
ID: 13639629
The syntax for the c# version I gave you and VB.net version is very similar.  Really, the biggest difference is how to hook into the event (keep in mind that the events and delegates are already written, we just have to write the handler.  In the above example, I called mine Message).  
In c# I did this by f.Created+=new System.IO.FileSystemEventHandler(Message);

I believe in VB.net you do this by using the addressof operator: AddHandler f.Created AddressOf Message

Everything else is a question of deleting semicolons and brackets, and the "Using" at the top of the program should be changed to "Imports"
Of course, all of this is moot, since this is all in .NET....you can simply use a c# class from your VB.net app...but since you want to play with it, I thought I'd give some hints on how to convert it. :)

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
Simple Linear Regression

627 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question