Link to home
Start Free TrialLog in
Avatar of Dodger42
Dodger42

asked on

In file text substitution

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?

Thanks!!!!
Avatar of r_a_j_e_s_h
r_a_j_e_s_h

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 ...


Regards
Rajesh
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.
Take a look at this , you can search based on different file criteria etc using this :

http://vbnet.mvps.org/index.html?code/fileapi/recursive.htm
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);
                  
                  f.EnableRaisingEvents=true;
                  
                  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!
Avatar of Dodger42

ASKER

1. Is the file always in the same directory?  
Yes!

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!
Sorry, my C is average, but will give that a shot GGalbo! I am more familiar with vb.net ;)

ASKER CERTIFIED SOLUTION
Avatar of ggalbo
ggalbo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial