【问题标题】:Trying to compare the 2 most recent files in a folder and process if different尝试比较文件夹中的 2 个最新文件,如果不同则处理
【发布时间】:2018-07-27 18:33:25
【问题描述】:

VBScript 专家我不是。我可以使用其他人的工作来构建我想要的东西,但是这个我遇到了问题......

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

我找到了大量信息,但没有专门针对我正在寻找的信息。

感谢您的所有帮助!

编辑: 离我要找的更近了....

'删除之前运行的文件 设置 objFSO = CreateObject("Scripting.FileSystemObject") objStartFolder = "L:\Inbox\Test\"

设置 objFolder = objFSO.GetFolder(objStartFolder)

设置 colFiles = objFolder.Files 对于 colFiles 中的每个 objFile 如果 instr(objFile.Name,".csv") 那么 objFSO.DeleteFile "L:\Inbox\Test*.*" 万一 下一个

'复制最新的2个文件到tesing文件夹 src = "L:\收件箱" dst = "L:\Inbox\Test"

设置 fso = CreateObject("Scripting.FileSystemObject")

mostRecent = Array(Nothing, Nothing)

对于 fso.GetFolder(src).Files 中的每个 f 如果 LCase(fso.GetExtensionName(f.Name)) = "csv" 那么 如果 mostRecent(0) 什么都不是,那么 设置 mostRecent(0) = f ElseIf f.DateLastModified > mostRecent(0).DateLastModified Then 设置 mostRecent(1) = mostRecent(0) 设置 mostRecent(0) = f ElseIf mostRecent(1) Is Nothing 或 f.DateLastModified > mostRecent(1).DateLastModified Then 设置 mostRecent(1) = f 万一 万一 下一个

对于 i = 0 到 1 If Not mostRecent(i) Is Nothing Then mostRecent(i).Copy dst & "\" 下一个

'比较L:\Inbox\Test中的2个文件并设置一个errorlevel

*****这是下一部分要弄清楚的*****

【问题讨论】:

    标签: vbscript


    【解决方案1】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 2018-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-07
      • 1970-01-01
      相关资源
      最近更新 更多