尝试比较文件夹中的2个最新文件,如果不同

时间:2018-02-16 19:38:30

标签: vbscript

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:\ Inbox” dst =“L:\ Inbox \ Test”

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

mostRecent = Array(Nothing,Nothing)

For each f in fso.GetFolder(src).Files   如果LCase(fso.GetExtensionName(f.Name))=“csv”那么     如果mostRecent(0)Nothing Then       设置mostRecent(0)= f     ElseIf f.DateLastModified> mostRecent(0).DateLastModified然后       设置mostRecent(1)= mostRecent(0)       设置mostRecent(0)= f     ElseIf MostRecent(1)Is Nothing or f.DateLastModified> mostRecent(1).DateLastModified然后       设置mostRecent(1)= f     万一   万一 下一步

对于i = 0到1   如果不是最近的(i)什么都不是那么最近(i).Copy dst& “\” 下一步

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

*****这是下一步找出*****

1 个答案:

答案 0 :(得分: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