Permalink
Browse files

ModControlFilesDatabase, cAccnScan / move more responsibility for sta…

…ndardizing PDF naming conventions over to cAccnScan. Handle docs that got badly named due to missing timestamp, etc.
  • Loading branch information...
C. Johnson
C. Johnson committed Apr 27, 2018
1 parent 50385d1 commit e4157d5bced78bab740c8ff350413b54005ad804
Showing with 148 additions and 79 deletions.
  1. +38 −28 ModControlFilesDatabase.bas
  2. +110 −51 cAccnScan.cls
@@ -708,15 +708,13 @@ Public Function RenamePDFs(Optional ByVal LogLevel As Integer)
Let sFileName = Dir(sPattern, vbNormal)
Do While Len(sFileName) > 0
Set oAccnScan = New cAccnScan: With oAccnScan
Let .Url = sDrive & vSearchDir & "\" & sFileName
.Url = sDrive & vSearchDir & "\" & sFileName
End With
If oAccnScan.IsAccnSheet(Version:=2) Then
' NOOP
ElseIf oAccnScan.IsAccnSheet(Version:=1) Then
' NOOP
ElseIf oAccnScan.IsDocumentationSheet(Version:=2) Then
' NOOP
ElseIf oAccnScan.IsDocumentationSheet(Version:=1) Then
Set cBits = New Collection
cBits.Add RegexComponent(sFileName, oAccnScan.MATCH_DOCUMENTATION_V1, 1)
@@ -739,49 +737,61 @@ Public Function RenamePDFs(Optional ByVal LogLevel As Integer)
Rs.Close
Set cBits = Nothing
ElseIf RegexMatch(sFileName, oAccnScan.MATCH_COPIER_SCAN) Then
ElseIf oAccnScan.IsCopierScan Or oAccnScan.IsDocumentationSheet(Version:=2) Then
' Is it referenced in AccnScans?
Set Rs = CurrentDb.OpenRecordset("SELECT * FROM AccnScans LEFT JOIN Accessions ON (AccnScans.ACCN=Accessions.ACCN) WHERE FileName='" & Replace(sFileName, "'", "''") & "'")
'No. We don't have any meta-data to intelligently rename it, so skip it for now.
If Rs.EOF Then
If LogLevel = 0 Or LogLevel > 1 Then
Debug.Print "COPIER SCAN UNPROCESSED [NO DB]: ", sFileName
Debug.Print "COPIER SCAN/DOC SHEET UNPROCESSED [NO DB]: ", sFileName
End If
'Yes. Get the meta-data from the database
Else
Let sFullPath = sDrive & vSearchDir & "\" & sFileName
If Rs!FileNameToBeFixed.value Then
Let oAccnScan.SheetType = Rs!SheetType.value
Let oAccnScan.Timestamp = Rs!Timestamp.value
Let sCreator = Nz(Rs!Creator.value)
If Len(sCreator) = 0 Then
Let sCreator = oAccnScan.Creator
End If
Let sCurName = GetCurNameFromCreatorCode(sCreator)
If Len(Nz(Rs!SheetType.value)) > 0 And UCase(Nz(Rs!SheetType.value)) <> "ACCN" Then
Let oAccnScan.SheetType = Nz(Rs!SheetType.value)
Let sNewFileName = sCurName & oAccnScan.SheetTypeSlug & GetDateSlug(Rs!Timestamp.value) & ".PDF"
ElseIf Len(Nz(Rs.Fields("AccnScans.ACCN").value)) > 0 Then
Let sNewFileName = sCurName & Replace(Nz(Rs.Fields("AccnScans.ACCN").value), ".", "") & ".PDF"
Else
Let sNewFileName = sFileName
End If
'If Len(Nz(Rs!SheetType.value)) > 0 And UCase(Nz(Rs!SheetType.value)) <> "ACCN" Then
'
' Let oAccnScan.SheetType = Nz(Rs!SheetType.value)
' Let sNewFileName = sCurName & oAccnScan.SheetTypeSlug & GetDateSlug(Rs!Timestamp.value) & ".PDF"
'
'ElseIf Len(Nz(Rs.Fields("AccnScans.ACCN").value)) > 0 Then
'
' Let sNewFileName = sCurName & Replace(Nz(Rs.Fields("AccnScans.ACCN").value), ".", "") & ".PDF"
'
'Else
' Let sNewFileName = sFileName
'End If
Let sNewFullPath = sDrive & vSearchDir & "\" & sNewFileName
'Let sNewFullPath = sDrive & vSearchDir & "\" & sNewFileName
If LogLevel = 0 Or LogLevel > 0 Then
Debug.Print "COPIER SCAN [DB]: ", sFullPath, "=>", sNewFileName
oAccnScan.ConvertFileName Result:=sNewFileName
If Len(sNewFileName) > 0 Then
If LogLevel = 0 Or LogLevel > 0 Then
Debug.Print "COPIER SCAN [DB]: ", sFullPath, "=>", sNewFileName
End If
End If
On Error GoTo CatchNameAsFailure
Name sFullPath As sNewFullPath
On Error GoTo 0
Rs.Edit
Let Rs!FileName = sNewFileName
Let Rs!FileNameToBeFixed = False
Rs.Update
'On Error GoTo CatchNameAsFailure
'Name sFullPath As sNewFullPath
'On Error GoTo 0
'Rs.Edit
'Let Rs!FileName = sNewFileName
'Let Rs!FileNameToBeFixed = False
'Rs.Update
Else
If LogLevel = 0 Or LogLevel > 1 Then
Debug.Print "COPIER SCAN UNPROCESSED [DB]: ", sFileName
@@ -857,7 +867,7 @@ Public Function GetDateSlug(Timestamp As Variant, Optional ByVal OmitTime As Boo
Else
Let sDate = Format(Timestamp, "YYYYmmdd")
If OmitTime Or Format(Timestamp, "HMS") <> "000" Then
Let sDate = sDate & "_" & Format(Timestamp, "HM")
Let sDate = sDate & "_" & Format(Timestamp, "HHMM")
End If
Let GetDateSlug = sDate
End If
@@ -553,6 +553,7 @@ Public Sub InsertIntoAccessions(Optional ByVal OnCurrent As Boolean, Optional By
Dim sOldFileName As String
Dim sNewFileName As String
If bFileNameToBeFixed Then
Let sOldFileName = FileName
@@ -563,35 +564,6 @@ Public Sub InsertIntoAccessions(Optional ByVal OnCurrent As Boolean, Optional By
End If
ConvertFileName Result:=sNewFileName
If Len(sNewFileName) > 0 And sNewFileName <> sOldFileName Then
On Error Resume Next: CurrentDb.QueryDefs.Delete "qUpdateAccnScans": On Error GoTo 0
Set oQuery = CurrentDb.CreateQueryDef( _
Name:="qUpdateAccnScans", _
SQLText:="SELECT * FROM AccnScans " _
& "WHERE FileName=[paramFileName] " _
& "AND FilePath=[paramFilePath]" _
)
oQuery.Parameters("paramFileName") = Trim(sOldFileName)
oQuery.Parameters("paramFilePath") = Trim(FilePath)
Set rsAccnScan = oQuery.OpenRecordset
Do Until rsAccnScan.EOF
rsAccnScan.Edit
rsAccnScan!FileName = sNewFileName
rsAccnScan!FileNameToBeFixed = False
rsAccnScan.Update
rsAccnScan.MoveNext
Loop
rsAccnScan.Close
Set rsAccnScan = Nothing
On Error Resume Next: CurrentDb.QueryDefs.Delete "qUpdateAccnScans": On Error GoTo 0
End If
End If
Set oQuery = Nothing
@@ -601,16 +573,20 @@ End Sub
Public Sub ConvertFileName(ByRef Result As String)
Dim oIsOldAccnFile As New RegExp
Dim reIsCopierScan As New RegExp
Dim reIsBadDateScan As New RegExp
Dim MetaD As Variant
Dim sOldNew As String
Dim sNewFileName As String
Dim sNewFileBaseName As String
Dim sNewFileExtension As String
Dim sOldFileName As String, sOldFilePath As String
Dim oRef As Variant
Dim cRefs As Variant
Dim I As Integer
Dim sOldFull As String
Dim sNewFull As String
Dim sCurName As String
With oIsOldAccnFile
.IgnoreCase = True
@@ -622,55 +598,138 @@ Public Sub ConvertFileName(ByRef Result As String)
.Pattern = MATCH_COPIER_SCAN
End With
With reIsBadDateScan
.IgnoreCase = True
.Pattern = MATCH_DOCUMENTATION_V2
End With
Set cRefs = oIsOldAccnFile.Execute(FileName)
If cRefs.Count > 0 Then
For Each oRef In cRefs
Set MetaD = oRef.SubMatches
Exit For
Next oRef
Let sNewFileName = MetaD.Item(0) & MetaD.Item(1) & MetaD.Item(2) & MetaD.Item(6) & ".PDF" & MetaD.Item(7)
Let sNewFileBaseName = MetaD.Item(0) & MetaD.Item(1) & MetaD.Item(2) & MetaD.Item(6)
Let sNewFileExtension = ".PDF" & MetaD.Item(7)
Let sNewFileName = sNewFileBaseName & sNewFileExtension
ElseIf reIsCopierScan.Test(FileName) Then
Let sNewFileName = Creator & Replace(ACCN, ".", "") & ".PDF"
Let sCurName = GetCurNameFromCreatorCode(Creator)
Let sNewFileExtension = ".PDF"
Select Case SheetType
Case "", "ACCN":
Let sNewFileBaseName = sCurName & Replace(ACCN, ".", "")
Case Else:
Let sNewFileBaseName = sCurName & SheetTypeSlug & GetDateSlug(Timestamp)
End Select
Let sNewFileName = sNewFileBaseName & sNewFileExtension
ElseIf reIsBadDateScan.Test(FileName) Then
Set cRefs = reIsBadDateScan.Execute(FileName)
If cRefs.Count > 0 Then
For Each oRef In cRefs
Set MetaD = oRef.SubMatches
Next oRef
If Not MetaD Is Nothing Then
If MetaD(2) = "18991230" Then
Let sNewFileBaseName = MetaD(0) & MetaD(1) & GetDateSlug(Timestamp)
Let sNewFileExtension = MetaD(3)
Let sNewFileName = sNewFileBaseName & sNewFileExtension
End If
End If
End If
End If
If Len(sNewFileName) > 0 Then
Let sOldFileName = FileName
Let sOldFilePath = FilePath
FileSystemConvertFileName NewFileName:=sNewFileName, BaseName:=sNewFileBaseName, Extension:=sNewFileExtension, Result:=Result
If Len(Result) > 0 Then
DatabaseConvertFileNames OldFileName:=sOldFileName, OldFilePath:=sOldFilePath
End If
End Sub
Public Sub FileSystemConvertFileName(ByVal NewFileName As String, ByVal BaseName As String, ByVal Extension As String, ByRef Result As String)
Dim sOldFull As String, sNewFull As String
Dim I As Integer, sOldNew As String
Let Result = ""
TryRename:
On Error GoTo CatchRename
On Error GoTo CatchRename
Let Result = ""
If Len(NewFileName) > 0 Then
Let sOldFull = Url
Let sNewFull = Drive & FilePath & "\" & sNewFileName
Let sNewFull = Drive & FilePath & "\" & NewFileName
If Me.Exists Then
If (sOldFull <> sNewFull) Then
Name sOldFull As sNewFull
Let Result = sNewFileName
Let FileName = sNewFileName
Let Result = NewFileName
Let FileName = NewFileName
End If
Else
Debug.Print "DID/COULD NOT RENAME: ", Url, "DOES NOT EXIST..."
End If
End If
FinallyRename:
On Error GoTo 0
Else
Set MetaD = Nothing
End If
On Error GoTo 0
Exit Sub
CatchRename:
Debug.Print "DID/COULD NOT RENAME: ", Url, "TRY AGAIN..."
Let sOldNew = sNewFileName
Let I = I + 1
Let sNewFileName = MetaD.Item(0) & MetaD.Item(1) & MetaD.Item(2) & MetaD.Item(6) & "-" & Format(I, "00") & ".PDF" & MetaD.Item(7)
Let sOldNew = NewFileName
If Err.Number = EX_FILEALREADYEXISTS Then
Debug.Print "DID/COULD NOT RENAME: ", Url, "TRY AGAIN..."
Let I = I + 1
Let NewFileName = BaseName & "-" & Format(I, "00") & Extension
ElseIf Err.Number = EX_FILEPERMISSIONDENIED Then
'NOOP
End If
If sOldNew <> sNewFileName Then Resume TryRename
If sOldNew <> NewFileName Then Resume TryRename
Resume FinallyRename
End Sub
Public Sub DatabaseConvertFileNames(OldFileName As String, OldFilePath As String)
Dim oQuery As DAO.QueryDef
Dim rsAccnScan As DAO.Recordset
On Error Resume Next: CurrentDb.QueryDefs.Delete "qUpdateAccnScans": On Error GoTo 0
Set oQuery = CurrentDb.CreateQueryDef( _
Name:="qUpdateAccnScans", _
SQLText:="SELECT * FROM AccnScans " _
& "WHERE FileName=[paramFileName] " _
& "AND FilePath=[paramFilePath]" _
)
oQuery.Parameters("paramFileName") = Trim(OldFileName)
oQuery.Parameters("paramFilePath") = Trim(OldFilePath)
Set rsAccnScan = oQuery.OpenRecordset
Do Until rsAccnScan.EOF
rsAccnScan.Edit
rsAccnScan!FileName = FileName
rsAccnScan!FilePath = FilePath
rsAccnScan!FileNameToBeFixed = False
rsAccnScan.Update
rsAccnScan.MoveNext
Loop
rsAccnScan.Close
Set rsAccnScan = Nothing
On Error Resume Next: CurrentDb.QueryDefs.Delete "qUpdateAccnScans": On Error GoTo 0
End Sub
Public Function isOnDefaultDrive() As Boolean

0 comments on commit e4157d5

Please sign in to comment.