I am trying to write a program that can sense if Any changes was made to the file:
"C:\\Folder1\\One\\File5.txt"

If Any changes was made, then this file will be copied to destination:
"C:\\Folder1\\Two\\File5.txt"

So what happens is that the destinationfile will be "Overwrited".
I have started out som code but are not sure how it is possible to sense any changes made to a file.

if(              ...\\One\\File5.txt will make any changes              )
{
System::IO::File::Copy(
"C:\\Folder1\\One\\File5.txt", 
"C:\\Folder1\\Two\\File5.txt"
}

Recommended Answers

All 9 Replies

When you first start your program, and every time you modify the file, remember its last modification date (you can get that with the stat() functions). Then just periodically check to see if the date has changed.

On Windows, you can use FindFirstChangeNotification() to create a notification object on a file or directory and use one of the wait functions, or you can use the ReadDirectoryChangesW() function.

Hope this helps.

If it is sufficient to use the time the file was last written as an indicator of a change, then
you can utilize GetLastWriteTime(), like: System::DateTime LastWriteTime = System::IO::File::GetLastWriteTime("file.txt"); I.e. at some point store the time and then later on compare the times to see whether there is a change.

If you want to check file content, you'll probably want to look up a CRC algorithm. I believe you can find md5 code on the internet.

Thank you, I beleive this could be a solution in someway. When looking at the members I did find DateTime::Millisecond and if I can notice a change in milliseconds, this will be the thing I am after or if it is possible even faster than that, I also saw a member DateTime::Ticks ?

But if I write System:: DateTime::Ticks CurrentWriteTime, this will not compile.
Is it possible to make a notice in milliseconds ?
(I beleive I have to Save(LastWriteTime) and Get the CurrentTime in milliseconds)


I have tried to do some code out like this. This do compile but if I run the code, an errormessage says that "C:\Folder1\Two\File5.txt already exists"
Is it possible to "force" it to overwrite this file with new contents ?

private: System::DateTime LastWriteTime;
  
System::DateTime CurrentWriteTime = System::IO::File::GetLastWriteTime("C:\\Folder1\\One\\File5.txt");

 if(  CurrentWriteTime != LastWriteTime  )
{
    System::IO::File::Copy
    ("C:\\Folder1\\One\\File5.txt", "C:\\Folder1\\Two\\File5.txt");
    
    System::DateTime LastWriteTime = CurrentWriteTime; 
    //Will save the Last written time for comparison for Next change to the file
}

If it is sufficient to use the time the file was last written as an indicator of a change, then
you can utilize GetLastWriteTime(), like: System::DateTime LastWriteTime = System::IO::File::GetLastWriteTime("file.txt"); I.e. at some point store the time and then later on compare the times to see whether there is a change.

The milliseconds are taken into account when you compare two DateTime values.
Note that if you have your files on a FATxx filesystem, the resolution of the last write time is 2 seconds, in which case you need to consider other means of detecting file changes (i.e. file size /content).

Is it possible to "force" it to overwrite this file with new contents ?

Yes, like below ... System::IO::File::Copy("C:\\Folder1\\One\\File5.txt", "C:\\Folder1\\Two\\File5.txt", [B]true[/B]);

The overwriting worked well. I use NTFS file system and XP Professional.
I am thinking a bit how the comparison in time between "The current time" for System:: DateTime and the Saved variable(LastWriteTime) for the same System:: DateTime is taken.

I red in MSDN this:
Internally, all DateTime values are represented as the number of ticks (the number of 100-nanosecond intervals) that have elapsed since 12:00:00 midnight

When looking at: 12:00:00, I beleive :: DateTime takes "seconds" in considiration but I am not sure. MSDN says: "100-nanosecond intervals". I am not sure exactly of wich timeinterval :: DateTime is.
I might looking for the most detailed time possible. For example this: 12:00:00:00 if this is possible.
Perheps System:: DateTime is the most detailed TimeInterval and if it is, is it seconds like: 12:00:00 Or 12:00:00:00 ?

private: System::DateTime LastWriteTime;

System::DateTime CurrentWriteTime = 
System::IO::File::GetLastWriteTime("C:\\Folder1\\One\\File5.txt");


if(  CurrentWriteTime  != LastWriteTime  )
{
   System::IO::File::Copy("C:\\Folder1\\One\\File5.txt", 
                          "C:\\Folder1\\Two\\File5.txt", true);
			
   System::DateTime LastWriteTime = 
   System::IO::File::GetLastWriteTime("C:\\Folder1\\One\\File5.txt");

}

The milliseconds are taken into account when you compare two DateTime values.
Note that if you have your files on a FATxx filesystem, the resolution of the last write time is 2 seconds, in which case you need to consider other means of detecting file changes (i.e. file size /content).


Yes, like below ... System::IO::File::Copy("C:\\Folder1\\One\\File5.txt", "C:\\Folder1\\Two\\File5.txt", [B]true[/B]);

That format is for use with running programs.

File times are stored as 64-bit FILETIME values. Use DateTimeToFileDate() and FileDateToDateTime() to convert between the two.

Hope this helps.

I am not sure but I think these syntax is "Delphi" (DateTimeToFileDate) etc... but that´s is ok if so.
I can´t find them as members to System:: or System::IO
I beleive if I use a code like below that will "sense" when a file is changed, the code has to run all the time, like a loop.
I have tried to do a while-loop that will go in eternity as I like it to do but wonder if that is the correct way to do it ?
It seems to work for a approx: 1 minute and then the loop is stopping. At the same time it take almost all power from the processor, so I don´t know if there could be any special approach to something like this.

I have tried to search for how you close an open file. I am not sure if the 2 files are opened or closed after these has been copied ?
Thanks...

One\\File5.txt
Two\\File5.txt

while("1" != "2")
{

System::DateTime CurrentWriteTime = System::IO::File::GetLastWriteTime
("C:\\Folder1\\One\\File5.txt");

System::DateTime LastWriteTime2 = System::IO::File::GetLastWriteTime
("C:\\Folder1\\Two\\File5.txt");



if(  CurrentWriteTime != LastWriteTime && CurrentWriteTime != LastWriteTime2  )
 {
System::IO::File::Copy("C:\\Folder1\\One\\File5.txt", "
                                    C:\\Folder1\\Two\\File5.txt", true);
	
System::DateTime LastWriteTime = System::IO::File::GetLastWriteTime
                                  ("C:\\Folder1\\One\\File5.txt");

}

}

That format is for use with running programs.

File times are stored as 64-bit FILETIME values. Use DateTimeToFileDate() and FileDateToDateTime() to convert between the two.

Hope this helps.

I have managed to do a completely working program below that works in this way:
I have 2 different files, (Source) and (Dest).
When any change to the (Source) is made, this file will replace (Dest).

To make this work in "RealTime", I have managed to do a loop that runs in eternity as I want it to do.
The problem though is that I am not sure if that is the correct way to do it because it takes up all the processor power. The processor peeks 100 % all the time.
Why I wonder this is because I know other applications that works in the same way but no processor power is used almost.

Perheps another approach could be used instead of a Loop ?
Thanks...

String^ Source = textBox2->Text; 
String^ Dest = textBox3->Text;

int i = 4;
while(i > 0)
{
Application::DoEvents();
i = (i + 1);

System::DateTime CurrentWriteTime = System::IO::File::GetLastWriteTime(Source);
System::DateTime LastWriteTime2 = System::IO::File::GetLastWriteTime(Dest);

if(  CurrentWriteTime != LastWriteTime && CurrentWriteTime != LastWriteTime2 &&   System::IO::File::Exists(Dest))
{
    System::IO::File::Copy(Source, Dest, true);
    System::DateTime LastWriteTime = System::IO::File::GetLastWriteTime(Source);
}

          if( i > 10000 )
         {
	     i = 4;
         }

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