Results 1 to 17 of 17

Thread: [RESOLVED] Compairing dates...might have stopped working.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Resolved [RESOLVED] Compairing dates...might have stopped working.

    I'm building a back-up program. It will check to see if the target file first exists (file has already been backed up) then it checks the "Last Modified" date of the source and target files (to see if source is newer and needs to be backed up). I'm pretty sure this worked in testing, now it's not.

    vb.net Code:
    1. If IO.File.Exists(TargetFile) Then
    2.             MsgBox(TargetFileInfo.LastWriteTime & " , " & SourceFileInfo.LastWriteTime)
    3.             If TargetFileInfo.LastWriteTime < SourceFileInfo.LastWriteTime Then
    4.                 SaveFile(SourceFile, TargetFile, "Over Written File:", True)
    5.             Else
    6.                 SaveFile(SourceFile, TargetFile, "Exsisting File:", False)
    7.             End If
    8.         Else
    9.             If TargetFileInfo.Directory.Exists = False Then IO.Directory.CreateDirectory(TargetFileInfo.Directory.ToString)
    10.             SaveFile(SourceFile, TargetFile, "New File:", True)
    11.         End If

    The MSGBOX before the if statement is used for debugging. So I can see if they match. Now everytime this runs (even if dates match) it still over writes file. Its still saying Target date is less than Source date. I tried changing the "<" to "<>" still nothing. Am I implementing this wrong?

  2. #2
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Compairing dates...might have stopped working.

    Datetimes are quite precise. If you want to look at just the date then LastWriteTime.Date, or time LastWriteTime.TimeOfDay.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    The Date matches to the second. They are completely Identical (since it was just copied a minute ago). So I'm not understanding why it's not working. Sounds like the system has trouble comparing dates. However I would like to keep the datetime checking as accurate as possible.

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Compairing dates...might have stopped working.

    I think that LastWriteTime isn't changed. When you write or create the file, set it manually.

    See example TouchFile here http://msdn.microsoft.com/en-us/libr...writetime.aspx
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    Well the LastWriteTime should be the last time the file was modified (created date changes to current date on copy of file). Now it's gotten weirder. Now it's kinda working. After changing back to my test directories, it seems like some files are now showing as existing, and some are showing as overwritten, even tho I ran the backup seconds before. Something seems fishy here. I manually checked in explorer all date and times of target and source files, and they are identical. So for some reason it's selecting which files are pre existing, and which are being over written. I'm going to look a little closer, and if I still can find anything, I'm going to post my whole code.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    *Update- Ok then I uses my source files from "c:\testfrom\" and use the script to back them up to the target directory "c:\testto\" it works just fine..no problems. But when I change the target directory to the sever "z:\" is when I get some files are classified as preexisting and some as overwritten. Now the thing is after running it a few times, looks like the same files are being overwritten and same with the preexisting.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    I'm getting uber frustrated so he's the full code. It's still very sloppy, have not had time to go back and clean it up. Suggestions are very welcome!

    vb.net Code:
    1. Imports System
    2. Imports System.IO
    3. Public Class Form1
    4.     Dim SourceFolder As String = "C:\testfrom\"
    5.     Dim SourceFolderFileCount
    6.     Dim TargetFolder As String = "z:\"
    7.     Dim ioFile As New StreamWriter("c:\TestLogFile.txt")
    8.  
    9.     Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    10.         ioFile.WriteLine("BackUp Started: " & System.DateTime.Now)
    11.         SourceFolderFileCount = 0
    12.         Dim nameOfDirectory As String = SourceFolder
    13.         Dim myDirectory As DirectoryInfo
    14.         myDirectory = New DirectoryInfo(nameOfDirectory)
    15.         WorkWithDirectory(myDirectory)
    16.         ioFile.WriteLine("BackUp Ended: " & System.DateTime.Now)
    17.         MsgBox("Done!")
    18.         ioFile.Close()
    19.         IO.File.Copy("C:\TestLogFile.txt", TargetFolder & "BackUpLog.txt", True)
    20.         IO.File.Delete("C:\TestLogFile.txt")
    21.  
    22.     End Sub
    23.  
    24.     Public Sub WorkWithDirectory(ByVal aDir As DirectoryInfo)
    25.         Dim nextDir As DirectoryInfo
    26.         WorkWithFilesInDir(aDir)
    27.         For Each nextDir In aDir.GetDirectories
    28.             WorkWithDirectory(nextDir)
    29.         Next
    30.     End Sub
    31.  
    32.     Public Sub WorkWithFilesInDir(ByVal aDir As DirectoryInfo)
    33.         Dim aFile As FileInfo
    34.         For Each aFile In aDir.GetFiles()
    35.             Application.DoEvents()
    36.             Checkfile(aFile)
    37.         Next
    38.     End Sub
    39.  
    40.     Sub Checkfile(ByVal FileToCheck As FileInfo)
    41.         SourceFolderFileCount = SourceFolderFileCount + 1
    42.         Label1.Text = SourceFolderFileCount
    43.         Dim SourceFile As String = SourceFolder & Microsoft.VisualBasic.Right(FileToCheck.FullName, Len(FileToCheck.FullName) - (Len(SourceFolder)))
    44.         Dim SourceFileInfo As FileInfo
    45.         SourceFileInfo = New FileInfo(SourceFile)
    46.  
    47.         Dim TargetFile As String = TargetFolder & Microsoft.VisualBasic.Right(FileToCheck.FullName, Len(FileToCheck.FullName) - (Len(SourceFolder)))
    48.         Dim TargetFileInfo As FileInfo
    49.         TargetFileInfo = New FileInfo(TargetFile)
    50.  
    51.  
    52.  
    53.         If IO.File.Exists(TargetFile) Then
    54.             MsgBox(TargetFile & " " & SourceFile)
    55.             MsgBox(TargetFileInfo.LastWriteTime & " , " & SourceFileInfo.LastWriteTime)
    56.             If TargetFileInfo.LastWriteTimeUtc < SourceFileInfo.LastWriteTimeUtc Then
    57.                 SaveFile(SourceFile, TargetFile, "Over Written File:", True)
    58.                 MsgBox("not good")
    59.             Else
    60.                 SaveFile(SourceFile, TargetFile, "Exsisting File:", False)
    61.             End If
    62.         Else
    63.             If TargetFileInfo.Directory.Exists = False Then IO.Directory.CreateDirectory(TargetFileInfo.Directory.ToString)
    64.             SaveFile(SourceFile, TargetFile, "New File:", True)
    65.         End If
    66.     End Sub
    67.  
    68.     Sub SaveFile(ByVal SourceFile As String, ByVal TargetFile As String, ByVal Status As String, ByVal DoICopy As Boolean)
    69.         If DoICopy = True Then
    70.             IO.File.Copy(SourceFile, TargetFile, True)
    71.         End If
    72.         ListBox1.Items.Add(Status)
    73.         ListBox1.Items.Add(SourceFile)
    74.         ListBox1.Items.Add(" -->" & TargetFile)
    75.         ListBox1.SelectedIndex = ListBox1.Items.Count - 1
    76.  
    77.         ioFile.WriteLine(Status & System.DateTime.Now)
    78.         ioFile.WriteLine(" Source File:" & SourceFile)
    79.         ioFile.WriteLine(" Target File:" & TargetFile)
    80.         ioFile.WriteLine("")
    81.  
    82.     End Sub
    83.  
    84. End Class

  8. #8
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Compairing dates...might have stopped working.

    I just copied a file by drag and drop. The creation time, and last access time were the current time. The modified time was from 2 years ago, same as the source. What would it hurt to manually change the lastwritetime???????
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    Quote Originally Posted by dbasnett View Post
    I just copied a file by drag and drop. The creation time, and last access time were the current time. The modified time was from 2 years ago, same as the source. What would it hurt to manually change the lastwritetime???????
    Well the thing is, yes the creation date changes, but the last modified date doesnt. The issue I'm running into is the the date/time is the same on either local copy, the code works fine. When the date/time is the same for the local copy, and the copy on the server, then the code selectively works. Sees like somewhere something is not working correctly on about 50% of the server files. I first thought maybe when I request the server file modified time it uses the servers local time or something (it is a unix server, while my local machine is XP). But then all files on the server shouldnt work. I guess if it cam down to it, I could store my own record info in the notes of the file or something, but I'd rather not mess with too much of the file. Just in case its a file format that may be doing the same thing.

    Anyone have ideas I can try?

  10. #10
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Compairing dates...might have stopped working.

    Do you need to use dates to compare files? I do something similar for copying files to a server, but I calculate a hash for each file. If the hash value for the source file is equal the the hash of the destination file, then the files are the same & I don't copy it. If they're different, then the files are different & I copy it up to the server.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    Quote Originally Posted by nbrege View Post
    Do you need to use dates to compare files? I do something similar for copying files to a server, but I calculate a hash for each file. If the hash value for the source file is equal the the hash of the destination file, then the files are the same & I don't copy it. If they're different, then the files are different & I copy up to the server.
    Now I was thinking about doing that. Good solid way of telling if the files are different. How-ever you dont know if one is older. You could accidentally copy an old file over a new file. With what I'm copying, this could happen, something would def have to be messed up on the local machine to do this, but i could. I might have to go that way if I cant figure out this issue.

    There an easy way to get the hash using vb.net?

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    Let me ask this question.... is there a way to convert date/time to a numeric value? I know OS'es like Linux use seconds that have elapsed after a set date and time. I feel if I were to do this, then I can compare integers rather than date/times. That or see where this error is coming from.

    Can this be done?

  13. #13
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Compairing dates...might have stopped working.

    Convert to ticks.

  14. #14

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    #UPDATE - Think I found the problem. Just for grins I decided to compair datetime.ticks. I found the working files have same value for ticks. The none working ones look like this target = 633863851070000000 while the source = 633863851074530000. So I think when I'm comparing datetimes, the system is comparing ticks. And my target time is rounded to the nearest second, with the source is to the nearest millisecond. So If I use ticks, round to the nearest second...should work... If it does, I repost and mark as resolved.

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Compairing dates...might have stopped working.

    Quote Originally Posted by MotoMan_Yz400 View Post
    Well the thing is, yes the creation date changes, but the last modified date doesnt. The issue I'm running into is the the date/time is the same on either local copy, the code works fine. When the date/time is the same for the local copy, and the copy on the server, then the code selectively works. Sees like somewhere something is not working correctly on about 50% of the server files. I first thought maybe when I request the server file modified time it uses the servers local time or something (it is a unix server, while my local machine is XP). But then all files on the server shouldnt work. I guess if it cam down to it, I could store my own record info in the notes of the file or something, but I'd rather not mess with too much of the file. Just in case its a file format that may be doing the same thing.

    Anyone have ideas I can try?
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Feb 2007
    Posts
    180

    Re: Compairing dates...might have stopped working.

    Ok so found the problem. Windows stores DateTime using time passed using 100 nano second units. The server uses seconds. So I just use DateTime.Ticks then truncated them to seconds. Then they match up perfectly. So there was the issue.

    When you ask for DateTime, it gets the DateTime in ticks, then returns it as a Date/Time format. So even if the DateTime looks the same, the system still looks at it and compares it as ticks. Problem solved.


    PS. - The server is setup as a simple networked drive. So I'm just copying from c: to z:. I guess the server adapts files for its system.

  17. #17
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: [RESOLVED] Compairing dates...might have stopped working.

    @moto - if you had mentioned that the server was Unix we would have been done on 15 Jan 2010.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width