Vous êtes sur la page 1sur 11

Private Function UpdateJob() As Long ' Copy New/Newer

If InAnExe Then On Error GoTo ErrHandler

Dim sSrc1Path As String

Dim sSrc2Path As String

Dim sDestRoot As String

Dim sDestPath As String

Dim sWorkPath As String

Dim aStr() As String

Dim i As Long, j As Long

Dim k As Long, x As Long

Dim fMatchRelSpec As Long

Dim fRemoveFlag As Long

Dim rTotal As Single

Dim increm As Long

Dim iUpTo As Long

Dim iCnt As Long

Dim iCompare As Long

Dim iFile As Integer

DoEvents

fMatchRelSpec = (chkAnyLoc = 0)

If fMatchRelSpec Then 'chkKillOrphans.Enabled

fRemoveFlag = (chkKillOrphans = 1)

If fRemoveFlag And (Not fSuppress) Then

If MsgBox("Are you sure you want to remove destination files no


longer in source location? ", vbOKCancel Or vbQuestion, _

" Delete Confirmation") = vbCancel Then fCancel = -1&: Exit


Function

End If

Else
If MsgBox("Are you sure you want to copy matching files from any
location? ", vbOKCancel Or vbQuestion, _

" Location Confirmation") = vbCancel Then fCancel = -1&: Exit


Function

End If

ArrowHourGlass True

Me.Caption = sCAPT & "scanning ."

sSrc1Path = AddSlash(txtPaths(1)) 'A

sSrc2Path = AddSlash(txtPaths(2)) 'B

If Not DirExists(sSrc1Path) Then GoTo ExitFunc

If Not DirExists(sSrc2Path) Then GoTo ExitFunc

sDestRoot = AddSlash(txtPaths(3))

If NotMatchFolderName(sSrc1Path, sSrc2Path, sDestRoot, fRemoveFlag) Then


fCancel = -1&: GoTo ExitFunc

If CreatePath(sDestRoot) = 0& Then GoTo ExitFunc

DoEvents

If fCancel Then GoTo ExitFunc

Me.Caption = sCAPT & "scanning . ."

cFindSrcFiles.RelativePaths = fMatchRelSpec

cFindSrcFiles.FindAllFiles sSrc1Path

DoEvents

If fCancel Then GoTo ExitFunc

Me.Caption = sCAPT & "scanning . . ."

cFindDstFiles.RelativePaths = fMatchRelSpec
cFindDstFiles.FindAllFiles sSrc2Path

DoEvents

If fCancel Then GoTo ExitFunc

ReDim aStr(200) As String

rTotal = cFindDstFiles.FilesUB + 1&

increm = CLng(rTotal / 100!)

If increm = 0& Then increm = 1& 'Total < 100

If fRemoveFlag Then ' Remove orphans from backup location

Me.Caption = sCAPT & "removing . . . ."

Me.Refresh

Do Until i > cFindDstFiles.FilesUB

For j = iUpTo To cFindSrcFiles.FilesUB

iCompare = StrComp(cFindDstFiles.RelFileSpec(i),
cFindSrcFiles.RelFileSpec(j))

If iCompare = Equal Then

iUpTo = j ' Sorted data so skip all prev src items from next
tests

Exit For ' Found match so skip to next backup item

ElseIf iCompare = Greater Then

iUpTo = j ' Sorted data so skip all prev src items from next
tests

ElseIf iCompare = Lesser Then

j = cFindSrcFiles.FilesUB ' Sorted data so orphaned backup item


End If

Next j

If GetInputState Then DoEvents

If fCancel Then GoTo ExitFunc

If j > cFindSrcFiles.FilesUB Then

' Remove destination files no longer in source location

If fBackup Then

sWorkPath = sDestRoot & "_B_A_C_K_U_P\" &


cFindDstFiles.RelFileSpec(i) & ".bkup" & iBackup

CreatePath sWorkPath

SetAttributes StrPtr(sWorkPath), vbNormal

CopyFile StrPtr(cFindDstFiles(i)), StrPtr(sWorkPath)

If fLog Then

x = x + 1&

If x > UBound(aStr) Then ReDim Preserve aStr(x + x) As


String

aStr(x) = sWorkPath

Me.Caption = sCAPT & CStr(CLng(100! * i / rTotal)) & " %


(" & x & ")"

End If

End If

SetAttributes StrPtr(cFindDstFiles(i)), vbNormal

Kill cFindDstFiles(i)

' Delete data record to speed up process below

cFindDstFiles.RemoveFile i

rTotal = rTotal - 1!

increm = CLng(rTotal / 100!)


If increm = 0& Then increm = 1&

Else

i = i + 1&

End If

If i Mod increm = 0& Then

On Error Resume Next ' Bug fix for no files left in folder

Me.Caption = sCAPT & CStr(CLng(100! * i / rTotal)) & " %"

On Error GoTo 0

If InAnExe Then On Error GoTo ErrHandler

End If

Loop 'i

Me.Caption = sCAPT & "removing . . . . ."

Me.Refresh

DoEvents

If fCancel Then GoTo ExitFunc

On Error Resume Next

If (chkRemoveEmpty = 1) Then ' Remove empty folders

For j = cFindDstFiles.FoldersUB To cFindDstFiles.FoldersLB Step -1

If cFindDstFiles.FolderIsEmpty(j) Then ' Is valid empty folder

' Remove destination folder no longer containing fso's

SetAttributes StrPtr(cFindDstFiles.FolderSpec(j)), vbNormal

RmDir cFindDstFiles.FolderSpec(j)

End If

Next j

End If

On Error GoTo 0

End If

If InAnExe Then On Error GoTo ErrHandler


Me.Caption = sCAPT & "working . . . . . ."

Me.Refresh

DoEvents

If fCancel Then GoTo ExitFunc

rTotal = cFindSrcFiles.FilesUB + 1&

increm = CLng(rTotal / 100!)

If increm = 0& Then increm = 1& 'Total < 100

iUpTo = 0&

i = 0&

Do Until i = rTotal

sDestPath = sDestRoot & cFindSrcFiles.RelFileSpec(i)

For j = iUpTo To cFindDstFiles.FilesUB

If GetInputState Then DoEvents

If fCancel Then GoTo ExitFunc

If fMatchRelSpec Then

iCompare = StrComp(cFindSrcFiles.RelFileSpec(i),
cFindDstFiles.RelFileSpec(j))

Else

iCompare = StrComp(cFindSrcFiles.FileName(i),
cFindDstFiles.FileName(j))

End If

If iCompare = Equal Then

iUpTo = j ' Sorted data so skip past all prev dest items on next
tests

iCompare = CompareFileTime(cFindSrcFiles.FileLastWrite(i),
cFindDstFiles.FileLastWrite(j))
If iCompare = ftNewer Then

If fMatchRelSpec Then

CreatePath sDestPath

SetAttributes StrPtr(sDestPath), vbNormal

If fBackup Then

If FileExists(sDestPath) Then

sWorkPath = sDestRoot & "_B_A_C_K_U_P\" &


cFindSrcFiles.RelFileSpec(i) & ".bkup" & iBackup

CreatePath sWorkPath

SetAttributes StrPtr(sWorkPath), vbNormal

CopyFile StrPtr(sDestPath), StrPtr(sWorkPath)

If fLog Then

x = x + 1&

If x > UBound(aStr) Then ReDim Preserve aStr(x


+ x) As String

aStr(x) = sWorkPath

End If

End If

End If

CopyFile StrPtr(cFindSrcFiles(i)), StrPtr(sDestPath)

iCnt = iCnt + 1&

If fLog Then

x = x + 1&

If x > UBound(aStr) Then ReDim Preserve aStr(x + x) As


String
aStr(x) = sDestPath

End If

Else 'If cFindSrcFiles.FileSize(i) <>


cFindDstFiles.FileSize(j) Then

On Error Resume Next 'Minimized!

lstResults.Visible = False

lstExists.Visible = True

Me.Height = rCaptH + 6780!

On Error GoTo 0

If InAnExe Then On Error GoTo ErrHandler

With cFindDstFiles

lstExists.Clear

lstExists.AddItem "File exists:"

AddMakeFit lstExists, .FileSpec(j)

lstExists.AddItem "Created: " &


FileTimeToString(.FileCreated(j))

lstExists.AddItem "Last Mod: " &


FileTimeToString(.FileLastWrite(j))

lstExists.AddItem "File Size: " & .FileSize(j) & "


bytes"

End With

With cFindSrcFiles

lstDups.Clear

lstDups.AddItem "Also exists:"

AddMakeFit lstDups, .FileSpec(i)

lstDups.AddItem "Created: " &


FileTimeToString(.FileCreated(i))

lstDups.AddItem "Last Mod: " &


FileTimeToString(.FileLastWrite(i))

lstDups.AddItem "File Size: " & .FileSize(i) & "


bytes"
End With

k = MsgBox("Do you want to copy this file?", vbYesNoCancel)

If k = vbCancel Then GoTo ExitFunc

If k = vbYes Then

CreatePath sDestPath

SetAttributes StrPtr(sDestPath), vbNormal

CopyFile StrPtr(cFindSrcFiles(i)), StrPtr(sDestPath)

lstDups.AddItem "File Copied"

iCnt = iCnt + 1&

If fLog Then

x = x + 1&

If x > UBound(aStr) Then ReDim Preserve aStr(x + x)


As String

aStr(x) = sDestPath

End If

End If

End If

End If

If fMatchRelSpec Then Exit For

ElseIf iCompare = Greater Then

iUpTo = j ' Sorted data so skip all prev dest items from next
tests

ElseIf iCompare = Lesser Then


j = cFindDstFiles.FilesUB ' Sorted data so new src item

End If

Next j

If j > cFindDstFiles.FilesUB Then

CreatePath sDestPath

CopyFile StrPtr(cFindSrcFiles(i)), StrPtr(sDestPath)

iCnt = iCnt + 1&

If fLog Then

x = x + 1&

If x > UBound(aStr) Then ReDim Preserve aStr(x + x) As String

aStr(x) = sDestPath

End If

End If

If i Mod increm = 0& Then ' Every one percent

Me.Caption = sCAPT & CStr(CLng(100! * i / rTotal)) & " %"

DoEvents

If fCancel Then Exit Do

End If

i = i + 1&

Loop 'i

ExitFunc:

Me.Caption = sCAPT & iCnt & " file(s) updated successfully"

i = 45 - Len(sDestRoot): If i < 1 Then i = 1

lstExists.Visible = False

lstResults.Visible = True

lstResults.AddItem sDestRoot & Space$(i) & iCnt & " file(s) updated"

lstResults.TopIndex = lstResults.NewIndex
If x <> 0& And fLog Then

ReDim Preserve aStr(x) As String

strSort45 aStr(), 1&, x

aStr(0) = "FileSync backup file(s) " & CStr(Now)

sDestPath = sSrc1Path & "FileSync.log"

SetAttributes StrPtr(sDestPath), vbNormal

iFile = FreeFile

Open sDestPath For Output Access Write Lock Write As #iFile

Print #iFile, Join$(aStr, vbCrLf)

Close #iFile

End If

ArrowHourGlass False

UpdateJob = iCnt

Exit Function

ErrHandler:

If Err.Number = 75 Then

Resume Next

Else

AlertError "frmFileSync.UpdateJob", DebugPrint

End If

End Function

Vous aimerez peut-être aussi