2

我有这段代码来构建一个表格并通过电子邮件将其发送给企业。
他们想知道每天有多少文件从源传输到目标,以及文件数量是否存在差异(可能的传输失败/损坏)。所有这些都按文件夹。

问题是这段代码不知何故错过了目标文件夹中缺失文件数的行。似乎它是以随机方式归因于该行。下图显示,即使文件夹 3 是完整的,它确实说有 1 个丢失的文件,并且文件夹 5 有 1 个丢失的文件(源文件夹中的 59 个,目标文件夹中的只有 58 个)并且它表明丢失了 0 个项目。

我在这里想念什么?

错误

代码是:

' >>> init a Windows Shell object to run system commands
SET WshShell = WScript.CREATEOBJECT("WScript.Shell")
' >>> load email Class code
WDIR = "D:\Kofax_Scripts"
SET objFSO = CreateObject("Scripting.FileSystemObject")
SET mailObjectFile = objFSO.OpenTextFile( WDIR & "\email.vbs", 1)
Execute mailObjectFile.ReadAll()

' >>> TEST passed arguments
If WScript.Arguments.Count = 0 Then
  Wscript.echo vbCr & vbLf & "Usage is: cscript.exe //nologo Kofax_SAP_crosscheck.vbs DEV|PRD [date]" & vbCrLf
  Wscript.echo               "If date is not given, script uses system current date. To run this script for other dates, you must pass it in format YYYY-MM-DD"
  wscript.quit
End If 
If WScript.Arguments.Item(0) = "DEV" Then
    Wscript.echo "Running in DEV..."
ElseIf WScript.Arguments.Item(0) = "PRD" Then
    Wscript.echo "Running in PRD..."
Else    
  Wscript.echo vbCr & vbLf & "Environment parameter is wrong! Possible choices:  DEV|PRD"
  wscript.quit
End If
' >>> Get today's date
t1=Now()
Wscript.echo "starting at: " & t1
' >>> Set date to use for the files' date crosscheck 
Dim date_cross_check
If WScript.Arguments.Count = 2 Then
    date_cross_check =  CDate(WScript.Arguments.Item(1))
Else
    date_cross_check = t1
End If
' >>> compose date string from the files pathname to be checked
ano = Year(date_cross_check)
mes = Month(date_cross_check)
dia = Day(date_cross_check)
date_cross_check_str = ano & "/" & mes & "/" & dia

' Set lists of Folders to cross-check
Set KofaxFolders = CreateObject("Scripting.Dictionary")
Set SapFolders = CreateObject("Scripting.Dictionary")

KofaxFolders.Add "AMOS", CreateObject("Scripting.Dictionary")
KofaxFolders.Add "bomdia", CreateObject("Scripting.Dictionary")
KofaxFolders.Add "cockpit", CreateObject("Scripting.Dictionary")
KofaxFolders.Add "irreg", CreateObject("Scripting.Dictionary")
KofaxFolders.Add "miro", CreateObject("Scripting.Dictionary")
KofaxFolders.Add "BSP", CreateObject("Scripting.Dictionary")

SapFolders.Add "AMOS", CreateObject("Scripting.Dictionary")
SapFolders.Add "bomdia", CreateObject("Scripting.Dictionary")
SapFolders.Add "cockpit", CreateObject("Scripting.Dictionary")
SapFolders.Add "irreg", CreateObject("Scripting.Dictionary")
SapFolders.Add "miro", CreateObject("Scripting.Dictionary")
SapFolders.Add "BSP", CreateObject("Scripting.Dictionary")

' init dictionaries
For each key in KofaxFolders
    KofaxFolders(key).Add "files", CreateObject("Scripting.Dictionary")
    KofaxFolders(key).Add "count", 0
Next
     For each key in SapFolders
            SapFolders(key).Add "files", CreateObject("Scripting.Dictionary")
            SapFolders(key).Add "missing", 0
            SapFolders(key).Add "count", 0  
        Next

      ' init File System Object
    Dim objFSO
    Set objFSO = CreateObject("Scripting.FileSystemObject")

    ' Loop on KofaxFolders to fill contents folders
    for each key in KofaxFolders
        fldr = "D:\Projetos\EXPORT\" & key &"\Save\"& ano &"\"& mes &"\"& dia
        If (objFSO.FolderExists(fldr)) Then
            Set objFolder = objFSO.GetFolder(fldr)
            For Each objFile In objFolder.Files
                ' Don't consider garbage files like Thumbs.db       
                If ( objFile.name <> "Thumbs.db" ) Then 
                    ' Wscript.echo "Folder : " & key  & "Filename: " & objFile.name & ", size (bytes): " & objFile.size
                    KofaxFolders(key)("files").Add objFile.name, objFile.size
                    KofaxFolders(key)("count") = KofaxFolders(key)("count") + 1
                End If
            Next    
        End If
    next

    ' Loop on SapFolders to fill contents folders
    for each key in SapFolders
        Set objFolder = objFSO.GetFolder("\\iftpv01\TPP\transdata\InB\OCRSave\Save"&key)
        'WScript.Echo "Folder : " & key
        For Each objFile In objFolder.Files
            'Wscript.echo "Folder : " & key  & "Filename: " & objFile.name & ", size (bytes): " & objFile.size      
            ' Check only SAP files with last Modified Date equal to specified date
            ' --------------------------------------------------------------------
            If ( DateDiff("d",objFile.DateLastModified, CDate(date_cross_check)) = 0 )Then
                ' Don't consider garbage files like Thumbs.db
                If ( objFile.name <> "Thumbs.db" ) Then 
                    SapFolders(key)("files").Add objFile.name, objFile.size
                    SapFolders(key)("count") = SapFolders(key)("count") + 1
                End If
            End If
        Next
    next

        ' ------------------------'
        ' Start new empty log file'
        ' ------------------------'
        Dim log_file
        log_file = WDIR & "\tmp\kofax_sap_crosscheck.log"
        Set objLogFile = objFSO.CreateTextFile(log_file,True)   
        objLogFile.Close
        ' open file in write mode
        Set objLogFile = objFSO.OpenTextFile(log_file, 2)

        ' Loop on KofaxFolders Contents and check if file exists in SAP structure
        For each key in KofaxFolders
            For each file in KofaxFolders(key)("files")
                If ( NOT SapFolders(key)("files").Exists(file) ) Then  
                    objLogFile.WriteLine("file " & file & " is missing from InB SAP folder "&key)
                    SapFolders(key)("missing") = SapFolders(key)("missing") + 1
                Else 
                    ' If file size is different between Kofax and SAP, this may be due to corrupt transfer
                    If SapFolders(key)("files")(file) <> KofaxFolders(key)("files")(file) Then
                        objLogFile.WriteLine("file " & file & " has not same size in SAP and Kofax " & key &" Folders!!!  Kofax size:  " _
                            & KofaxFolders(key)("files")(file) & "| SAP size: " _
                            & SapFolders(key)("files")(file) _
                        )
                        SapFolders(key)("missing") = SapFolders(key)("missing") + 1
                    End If 
                End If 
            Next
        Next
        ' close log file
        objLogFile.Close
        ' compute execution time
        exec_time = datediff("s",t1,Now)

        ' Global missing count
        Dim missing_files : missing_files = 0
        For each key in SapFolders
            missing_files = missing_files + SapFolders(key)("missing")
        Next

        ' Build summary HTML table according to "missing_files" count
        Dim rep_table : rep_table  = ""
        if ( missing_files > 0 )Then
            rep_table = "<table border=""1""><tr><th>Folder</th><th>Nr of files Source</th><th>Nr of files Dest</th><th>Nr of Files missing Dest</th></tr>"
            For each key in SapFolders
                rep_table = rep_table & "<tr><td>" & key & "</td><td align=""right"">" &  KofaxFolders(key)("count") &"</td><td align=""right"">" & SapFolders(key)("count") & "</td>"
                if ( SapFolders(key)("missing") > 0 ) Then
                    rep_table = rep_table & "<td align=""right"" bgcolor=""#FF0000"">" 
                Else
                    rep_table = rep_table & "<td align=""right"">" 
                End If 
                rep_table = rep_table & SapFolders(key)("missing") &"</td></tr>"
            Next
        Else
            rep_table = "<table border=""1""><tr><th>Pasta</th><th>Nº ficheiros no Kofax</th><th>Nº ficheiros no SAP</th></tr>"
            For each key in SapFolders
                rep_table = rep_table & "<tr><td>" & key & "</td><td align=""right"">" &  KofaxFolders(key)("count") &"</td><td align=""right"">" & SapFolders(key)("count") & "</td></tr>"
            Next
        End If
        rep_table = rep_table & "</table>"
4

1 回答 1

2

我不是 100% 确定这里发生了什么,但我认为问题在于这SapFolders是一本字典,而您正在使用该行

For each key in SapFolders

创建表时对其进行迭代。这种迭代中的键顺序(基本上)是随机的。在您的情况下,循环按该顺序遍历键是不正确的。"AMOS", "bomdia", "cockpit", "irreg", "miro", "BSP"

你可以做的是创建一个数组:

keys = Array("AMOS", "bomdia", "cockpit", "irreg", "miro", "BSP")

并替换开始的每个循环

For each key in SapFolders

经过

For i = 0 to UBound(keys)
   key = keys(i)

(并且可能对涉及的迭代进行类似的操作KofaxFolders)。

这将保证您知道填充报告表的顺序。

于 2015-11-12T16:23:21.687 回答