首页 文章

尝试比较文件夹中的2个最新文件,如果不同则进行处理

提问于
浏览
0

VBScript专家我不是 . 我可以利用其他人的工作来 Build 我想要的东西,但是这个我有问题....

使用VBScript,我需要比较文件夹中的2个最新文件,如果它们不同,请设置一个我可以传递给调用程序的错误代码 . 新文件将每10分钟传输一次,作为流程的一部分,我需要比较文件,以便用户可以处理新数据(如果存在) . 在一天开始时,它将在新文件和静态空白文件之间进行比较,以查看是否已添加数据 . 文件名的格式为filename-mmddyyyy-hhmmssss.csv

我已经找到了大量的信息,但没有具体做我正在寻找的东西 .

感谢您的帮助!

编辑:更接近我正在寻找的......

'从先前运行中删除文件设置objFSO = CreateObject(“Scripting.FileSystemObject”)objStartFolder =“L:\ Inbox \ Test \”

设置objFolder = objFSO.GetFolder(objStartFolder)

如果instr(objFile.Name,“ . csv”),则为colFiles中的每个objFile设置colFiles = objFolder.Files,然后objFSO.DeleteFile“L:\ Inbox \ Test * . *”end if next

'将最新的2个文件复制到测试文件夹src =“L:\ Inbox”dst =“L:\ Inbox \ Test”

设置fso = CreateObject(“Scripting.FileSystemObject”)

mostRecent = Array(Nothing,Nothing)

对于每个f在fso.GetFolder(src).Files如果LCase(fso.GetExtensionName(f.Name))=“csv”那么如果mostRecent(0)是Nothing则设置mostRecent(0)= f ElseIf f.DateLastModified> mostRecent (0).DateLastModified然后设置mostRecent(1)= mostRecent(0)设置mostRecent(0)= f ElseIf mostRecent(1)Is Nothing或f.DateLastModified> mostRecent(1).DateLastModified然后设置mostRecent(1)= f End如果结束如果下一步

对于i = 0到1如果不是最近的(i)什么都没有那么大多数(i) . 复制dst&“\”下一步

'比较L:\ Inbox \ Test中的2个文件并设置错误级别

***这是找出***的下一部分

1 回答

  • 0

    它不漂亮,可以清理,但也许它会帮助别人......

    Option Explicit
    
    Dim src, dst, f1, f2, mostRecent, objFSO, fso, objStartFolder, objFolder, colFiles, objFile, f, i, objFile1, strCurrentDevices, objFile2, objFile3, strAddress, strNotCurrent, cmd, strFile1, strFile2, arrFile1, arrFile2, intLineCount, strError
    
    'Delete files from previous run
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objStartFolder = "L:\Inbox\Test\"
    
    Set objFolder = objFSO.GetFolder(objStartFolder)
    
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
       if instr(objFile.Name,".csv") then
           objFSO.DeleteFile "L:\Inbox\Test\*.csv"
       end if
    Next
    
    
    'Copy the newest 2 files to the tesing folder
    src = "L:\Inbox"
    dst = "L:\Inbox\Test"
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    
    mostRecent = Array(Nothing, Nothing)
    
    For Each f In fso.GetFolder(src).Files
      If LCase(fso.GetExtensionName(f.Name)) = "csv" Then
        If mostRecent(0) Is Nothing Then
          Set mostRecent(0) = f
        ElseIf f.DateLastModified > mostRecent(0).DateLastModified Then
          Set mostRecent(1) = mostRecent(0)
          Set mostRecent(0) = f
        ElseIf mostRecent(1) Is Nothing Or f.DateLastModified > mostRecent(1).DateLastModified Then
          Set mostRecent(1) = f
        End If
      End If
    Next
    
    For i = 0 To 1
      If Not mostRecent(i) Is Nothing Then mostRecent(i).Copy dst & "\"
    Next
    
    'Compare the 2 files in  L:\Inbox\Test
    
    strFile1 = mostRecent(0)
    strFile2 = mostRecent(1)
    set objFSO = CreateObject("Scripting.FilesystemObject")
    set objFile1 = objFSO.opentextfile(strFile1,1)
    set objFile2 = objFSO.opentextfile(strFile2,1)
    arrFile1 = split(objFile1.ReadAll,vbNewLine)
    arrFile2 = split(objFile2.ReadAll,vbNewLine)
    objFile1.close
    objFile2.close
    
    if ubound(arrFile1) < ubound(arrFile2) then
       intLineCount = ubound(arrFile1)
       strError = strFile2 & " is bigger than " & strFile1
    elseif ubound(arrFile1) > ubound(arrFile2) then
       intLineCount = ubound(arrFile2)
       strError = strFile2 & " is bigger than " & strFile1
    else 
       intLineCount = ubound(arrFile2)
    end if
    
    for i = 0 to intLineCount
       if not arrFile1(i) = arrFile2(i) then 
          exit for
       end if
    next
    
    if i < (intLineCount + 1) then
       WScript.Echo "Line " & (i+1) & " not equal"
    '   WScript.Echo strError
    'MISetErrorCode(1)
    elseif strError <> "" then
    '   WScript.Echo strError
    else 
       WScript.Echo "Files are identical."
    'MISetErrorCode(0)
    end if
    

相关问题