|
-
Jan 15th, 2010, 05:06 PM
#1
Thread Starter
Addicted Member
[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:
If IO.File.Exists(TargetFile) Then
MsgBox(TargetFileInfo.LastWriteTime & " , " & SourceFileInfo.LastWriteTime)
If TargetFileInfo.LastWriteTime < SourceFileInfo.LastWriteTime Then
SaveFile(SourceFile, TargetFile, "Over Written File:", True)
Else
SaveFile(SourceFile, TargetFile, "Exsisting File:", False)
End If
Else
If TargetFileInfo.Directory.Exists = False Then IO.Directory.CreateDirectory(TargetFileInfo.Directory.ToString)
SaveFile(SourceFile, TargetFile, "New File:", True)
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?
-
Jan 15th, 2010, 05:12 PM
#2
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.
-
Jan 15th, 2010, 05:16 PM
#3
Thread Starter
Addicted Member
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.
-
Jan 15th, 2010, 05:43 PM
#4
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
-
Jan 15th, 2010, 05:48 PM
#5
Thread Starter
Addicted Member
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.
-
Jan 15th, 2010, 05:55 PM
#6
Thread Starter
Addicted Member
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.
-
Jan 15th, 2010, 06:01 PM
#7
Thread Starter
Addicted Member
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:
Imports System
Imports System.IO
Public Class Form1
Dim SourceFolder As String = "C:\testfrom\"
Dim SourceFolderFileCount
Dim TargetFolder As String = "z:\"
Dim ioFile As New StreamWriter("c:\TestLogFile.txt")
Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
ioFile.WriteLine("BackUp Started: " & System.DateTime.Now)
SourceFolderFileCount = 0
Dim nameOfDirectory As String = SourceFolder
Dim myDirectory As DirectoryInfo
myDirectory = New DirectoryInfo(nameOfDirectory)
WorkWithDirectory(myDirectory)
ioFile.WriteLine("BackUp Ended: " & System.DateTime.Now)
MsgBox("Done!")
ioFile.Close()
IO.File.Copy("C:\TestLogFile.txt", TargetFolder & "BackUpLog.txt", True)
IO.File.Delete("C:\TestLogFile.txt")
End Sub
Public Sub WorkWithDirectory(ByVal aDir As DirectoryInfo)
Dim nextDir As DirectoryInfo
WorkWithFilesInDir(aDir)
For Each nextDir In aDir.GetDirectories
WorkWithDirectory(nextDir)
Next
End Sub
Public Sub WorkWithFilesInDir(ByVal aDir As DirectoryInfo)
Dim aFile As FileInfo
For Each aFile In aDir.GetFiles()
Application.DoEvents()
Checkfile(aFile)
Next
End Sub
Sub Checkfile(ByVal FileToCheck As FileInfo)
SourceFolderFileCount = SourceFolderFileCount + 1
Label1.Text = SourceFolderFileCount
Dim SourceFile As String = SourceFolder & Microsoft.VisualBasic.Right(FileToCheck.FullName, Len(FileToCheck.FullName) - (Len(SourceFolder)))
Dim SourceFileInfo As FileInfo
SourceFileInfo = New FileInfo(SourceFile)
Dim TargetFile As String = TargetFolder & Microsoft.VisualBasic.Right(FileToCheck.FullName, Len(FileToCheck.FullName) - (Len(SourceFolder)))
Dim TargetFileInfo As FileInfo
TargetFileInfo = New FileInfo(TargetFile)
If IO.File.Exists(TargetFile) Then
MsgBox(TargetFile & " " & SourceFile)
MsgBox(TargetFileInfo.LastWriteTime & " , " & SourceFileInfo.LastWriteTime)
If TargetFileInfo.LastWriteTimeUtc < SourceFileInfo.LastWriteTimeUtc Then
SaveFile(SourceFile, TargetFile, "Over Written File:", True)
MsgBox("not good")
Else
SaveFile(SourceFile, TargetFile, "Exsisting File:", False)
End If
Else
If TargetFileInfo.Directory.Exists = False Then IO.Directory.CreateDirectory(TargetFileInfo.Directory.ToString)
SaveFile(SourceFile, TargetFile, "New File:", True)
End If
End Sub
Sub SaveFile(ByVal SourceFile As String, ByVal TargetFile As String, ByVal Status As String, ByVal DoICopy As Boolean)
If DoICopy = True Then
IO.File.Copy(SourceFile, TargetFile, True)
End If
ListBox1.Items.Add(Status)
ListBox1.Items.Add(SourceFile)
ListBox1.Items.Add(" -->" & TargetFile)
ListBox1.SelectedIndex = ListBox1.Items.Count - 1
ioFile.WriteLine(Status & System.DateTime.Now)
ioFile.WriteLine(" Source File:" & SourceFile)
ioFile.WriteLine(" Target File:" & TargetFile)
ioFile.WriteLine("")
End Sub
End Class
-
Jan 15th, 2010, 06:03 PM
#8
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???????
-
Jan 18th, 2010, 10:25 AM
#9
Thread Starter
Addicted Member
Re: Compairing dates...might have stopped working.
 Originally Posted by dbasnett
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?
-
Jan 18th, 2010, 10:46 AM
#10
Frenzied Member
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.
-
Jan 18th, 2010, 10:49 AM
#11
Thread Starter
Addicted Member
Re: Compairing dates...might have stopped working.
 Originally Posted by nbrege
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?
-
Jan 18th, 2010, 11:14 AM
#12
Thread Starter
Addicted Member
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?
-
Jan 18th, 2010, 11:26 AM
#13
Frenzied Member
Re: Compairing dates...might have stopped working.
-
Jan 18th, 2010, 11:26 AM
#14
Thread Starter
Addicted Member
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.
-
Jan 18th, 2010, 12:51 PM
#15
Re: Compairing dates...might have stopped working.
 Originally Posted by MotoMan_Yz400
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?
-
Jan 18th, 2010, 01:03 PM
#16
Thread Starter
Addicted Member
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.
-
Jan 18th, 2010, 01:36 PM
#17
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|