On Error Resume Next
Dim FailCount, DuplicateCount, TNcount, NoResults, VCcount, FileCount
Dim fso, folder, file, NewsFile, LogFolder, LogDir, DateTest, CreateDate, datelist, Count, AppendFile
Dim ResultsFile, ImportFile, ImportLine, ItemCountTN, Items, ImportFullFile, ReadThroughFile, ItemCountVC, ItemCount
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Set FSO = CreateObject("Scripting.FileSystemObject")
LogFolder = "\\server\path\"
LogDir = "\\server\path\"
Set folder = fso.GetFolder(LogFolder)
Set folderidx = folder.Files
'Create Log file and setup columns"
Set NewFile = fso.CreateTextFile("c:\Macro\LogReport.csv", True)
NewFile.WriteLine("Order Import Log")
NewFile.WriteLine("Date,TNCount,VCCount,FailCount,DuplicateCount,NoResultsCount,FileCount,ItemCount")
NewFile.Close
Count = 0
'Set Log File for open for appending
Set AppendFile = FSO.OpenTextFile("c:\Macro\LogReport.csv", ForAppending, True)
Do
'Set Date to check for
DateTest = Date()-Count
DateList = right("00" & day(DateTest),2) & "-" & right("00" & month(DateTest),2) & "-" & year(DateTest)
FailCount = 0
DuplicateCount = 0
NoResults = 0
FileCount = 0
TNCount = 0
VCCount = 0
ItemCount = 0
For Each file In folderidx
'Check date of files
CreateDate = file.DateCreated
CreateDate = right("00" & day(CreateDate),2) & "-" & right("00" & month(CreateDate),2) & "-" & year(CreateDate)
IF DateList = CreateDate Then
FileCount = Filecount + 1
If RegExpTest(file, "Import Log-Day-") Then
NoResults = NoResults + 1
ElseIf RegExpTest(file, "Import Log-TN") Then
'Start Count of items section
'Open the import results file to find the import file
Set ResultsFile = fso.OpenTextFile(file, ForReading)
ReadThroughFile = 0
Do Until ReadThroughFile=4
ImportFile = ResultsFile.ReadLine
ImportFile = Trim(ImportFile)
'Msgbox ImportFile
If RegExpTest(ImportFile, "-A.txt") or RegExpTest(ImportFile, "-1.txt") or RegExpTest(ImportFile, "_dPO") or RegExpTest(ImportFile, "-a.txt") or RegExpTest(ImportFile, "_ddPO.txt") or RegExpTest(ImportFile, "_pTerm.txt") or RegExpTest(ImportFile, "_CreditTerm.txt") or RegExpTest(ImportFile, "_crTerm.txt") or RegExpTest(ImportFile, "_LocationError.txt") or RegExpTest(ImportFile, "_LCOATION.txt") or RegExpTest(ImportFile, "-PO-Incorrect.txt") or RegExpTest(ImportFile, "_VISA.txt") or RegExpTest(ImportFile, "-not-imported.txt") or RegExpTest(ImportFile, "_cpu.txt") or RegExpTest(ImportFile, "_emptyPaymentMethod.txt") then
ReadThroughFile=4
ImportFile = Right(Left(ImportFile,121),16)
ImportFile = Trim(ImportFile)
ResultsFile.Close
'msgbox importfile
'Open the Import file and read the contents to find out the items ordered and sum them.
Set ImportFullFile = FSO.OpenTextFile("\\server\path\"&ImportFile, ForReading)
ImportFile.Close
Do Until ImportFullFile.AtEndOfStream
ImportLine = ImportFullFile.ReadLine
If RegExpTest(ImportLine, "ITM") Then
If Mid(ImportLine,18,1) = "^" Then
Items = Mid(ImportLine,17,1)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,19,1) = "^" Then
Items = Mid(ImportLine,17,2)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,20,1) = "^" Then
Items = Mid(ImportLine,17,3)
ItemCount = ItemCount + Items
End If
Else
End If
Loop
ImportFullFile.Close
ElseIf RegExpTest(ImportFile,".txt") Then
ReadThroughFile=4
ImportFile = Right(Left(ImportFile,119),14)
ImportFile = Trim(ImportFile)
ResultsFile.Close
'msgbox importfile
'Open the Import file and read the contents to find out the items ordered and sum them.
Set ImportFullFile = FSO.OpenTextFile("\\server\path\"&ImportFile, ForReading)
ImportFile.Close
Do Until ImportFullFile.AtEndOfStream
ImportLine = ImportFullFile.ReadLine
If RegExpTest(ImportLine, "ITM") Then
If Mid(ImportLine,18,1) = "^" Then
Items = Mid(ImportLine,17,1)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,19,1) = "^" Then
Items = Mid(ImportLine,17,2)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,20,1) = "^" Then
Items = Mid(ImportLine,17,3)
ItemCount = ItemCount + Items
End If
Else
End If
Loop
ImportFullFile.Close
Else
ReadThroughFile=ReadThroughFile+1
End If
Loop
'msgbox "done file"
TNcount = TNcount + 1
ElseIf RegExpTest(file, "Import Log-FailedDay") Then
FailCount = FailCount + 1
ElseIf RegExpTest(file, "Import Log-VC") Then
'Start Count of items section
'Open the import results file to find the import file
Set ResultsFile = fso.OpenTextFile(file, ForReading)
ReadThroughFile = 0
Do Until ReadThroughFile=4
ImportFile = ResultsFile.ReadLine
ImportFile = Trim(ImportFile)
'Msgbox ImportFile
If RegExpTest(ImportFile, "-A.txt") or RegExpTest(ImportFile, "-1.txt") or RegExpTest(ImportFile, "_dPO") or RegExpTest(ImportFile, "-a.txt") or RegExpTest(ImportFile, "_ddPO.txt") or RegExpTest(ImportFile, "_pTerm.txt") or RegExpTest(ImportFile, "_CreditTerm.txt") or RegExpTest(ImportFile, "_crTerm.txt") or RegExpTest(ImportFile, "_LocationError.txt") or RegExpTest(ImportFile, "_LCOATION.txt") or RegExpTest(ImportFile, "-PO-Incorrect.txt") or RegExpTest(ImportFile, "_VISA.txt") or RegExpTest(ImportFile, "-not-imported.txt") or RegExpTest(ImportFile, "_cpu.txt") or RegExpTest(ImportFile, "_emptyPaymentMethod.txt") then
ReadThroughFile=4
ImportFile = Right(Left(ImportFile,121),16)
ImportFile = Trim(ImportFile)
ResultsFile.Close
'msgbox importfile
'Open the Import file and read the contents to find out the items ordered and sum them.
Set ImportFullFile = FSO.OpenTextFile("\\server\path\"&ImportFile, ForReading)
ImportFile.Close
Do Until ImportFullFile.AtEndOfStream
ImportLine = ImportFullFile.ReadLine
If RegExpTest(ImportLine, "ITM") Then
If Mid(ImportLine,18,1) = "^" Then
Items = Mid(ImportLine,17,1)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,19,1) = "^" Then
Items = Mid(ImportLine,17,2)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,20,1) = "^" Then
Items = Mid(ImportLine,17,3)
ItemCount = ItemCount + Items
End If
Else
End If
Loop
ImportFullFile.Close
ElseIf RegExpTest(ImportFile,".txt") Then
ReadThroughFile=4
ImportFile = Right(Left(ImportFile,119),14)
ImportFile = Trim(ImportFile)
ResultsFile.Close
'msgbox importfile
'Open the Import file and read the contents to find out the items ordered and sum them.
Set ImportFullFile = FSO.OpenTextFile("\\server\path\"&ImportFile, ForReading)
ImportFile.Close
Do Until ImportFullFile.AtEndOfStream
ImportLine = ImportFullFile.ReadLine
If RegExpTest(ImportLine, "ITM") Then
If Mid(ImportLine,18,1) = "^" Then
Items = Mid(ImportLine,17,1)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,19,1) = "^" Then
Items = Mid(ImportLine,17,2)
ItemCount = ItemCount + Items
ElseIf Mid(ImportLine,20,1) = "^" Then
Items = Mid(ImportLine,17,3)
ItemCount = ItemCount + Items
End If
Else
End If
Loop
ImportFullFile.Close
Else
ReadThroughFile=ReadThroughFile+1
End If
Loop
VCcount = VCcount + 1
ElseIf RegExpTest(file, "Import Log-ORDER ALREADY EXISTS (") Then
DuplicateCount = DuplicateCount + 1
End If
Else
End If
Next
'Write Log data.
If FileCount = 0 Then
Else
AppendFile.WriteLine(DateTest&","&TNcount&","&VCcount&","&FailCount&","&DuplicateCount&","&NoResults&","&FileCount&","&ItemCount)
End If
Count = Count + 1
'Msgbox DateList
Loop Until DateList="01-08-2009"
AppendFile.Close
Function RegExpTest(strInput, strPattern)
Dim objRegExp : Set objRegExp = New RegExp
objRegExp.IgnoreCase = True
objRegExp.Global = False
objRegExp.Pattern = strPattern
RegExpTest = objRegExp.Test(strInput)
objRegExp.Pattern = ""
end function