-3

How can I identify when the file have been modified. If the file is modified save a copy each time to "archive" folder. The file name will increment: file (1).txt , file (2).txt .....

My code below stop increment on 2.

FileInfo fileSettings = new FileInfo(@"c:\path\file.txt");
string archive = @"C:\Users\Desktop\archive\";`

if ( fileSettings.LastWriteTime > DateTime.Today )
{
  int count = 1;
  string woExtension = Path.GetFileNameWithoutExtension(@"c:\path\file.txt");
  string extension = Path.GetExtension(@"c:\path\file.txt");

  string tempFilename = string.Format("{0} ({1})", woExtension, count++);
  fileSettings.CopyTo(Path.Combine(archive + tempFilename + extension));
}
AppDev
  • 11
  • 2
  • 2
    I recommending taking a look at our [help] . There, it explains why this question is not a good one for StackOverflow. You have shown that you have done no research or made attempts on your own, which is a big part of StackOverflow. How can we help you with your code, if there is no code to help with? If you do have code, please, share it! – Jaskier Oct 09 '19 at 18:21
  • () => https://stackoverflow.com/questions/3360324/check-last-modified-date-of-file-in-c-sharp – Nafis Islam Oct 09 '19 at 18:21
  • 1
    This lacks a crucial piece of information. Modified since when? Since the last read? – Sach Oct 09 '19 at 18:25

1 Answers1

0

It appears that what you need is an algorithm to copy files with file names appropriately incremented. This will do what you're looking for.

Say these are you file and archive directory names.

var file = @"Data/A.txt";
var archive = @"Data/Archive";

We see if the last write time condition is satisfied. If so, we enumerate all files in your destination directory that match your original file patter. That is, it must start with A, then it may or may not have (1) etc. So all files such as A.txt, A(1).txt, A(2).txt are enumerated. Then we get the last of those files in order (and here we must be careful to sort by length, if you simply sort by value, A.txt will come after all others). Then using a Regex we get the last file's number, increment, and copy file with the new name.

if (File.GetLastWriteTime(file) > DateTime.Today)
{
    var fileNameOnly = Path.GetFileNameWithoutExtension(file);
    var ext = Path.GetExtension(file);
    var pattern = fileNameOnly + "*" + ext;

    var lastCopy = Directory.EnumerateFiles(archive, pattern, System.IO.SearchOption.TopDirectoryOnly)
        .OrderBy(x => x.Length)
        .LastOrDefault();

    var newName = fileNameOnly + ext;
    if (lastCopy != null)
    {
        var lastCopyNameOnly = Path.GetFileNameWithoutExtension(lastCopy);
        var match = Regex.Match(lastCopyNameOnly, @"(.+)\((\d+)\)");
        int lastNum = 1;
        if (match.Success)
        {
            int.TryParse(match.Groups[2].Value, out lastNum);
            lastNum++;
        }
        newName = string.Format("{0}({1}){2}", fileNameOnly, lastNum, ext);
    }

    var dest = Path.Combine(archive, newName);
    File.Copy(file, dest);
}

Note

Be careful about var lastNum = 1; initializatin as well. You must initialize to 1, not zero. Because if the match.Success == false, that mean you have only the A.txt file in the destination, so then your next file will be A(1).txt. If you initialized it to zero, then your next file will be A(0).txt which isn't what your desired output is.

Sach
  • 10,091
  • 8
  • 47
  • 84