VBscript - 根据优先级选择文件

时间:2016-05-12 02:19:58

标签: vbscript

我有一个文件夹,我将根据文件名循环处理文件。在我的脚本上做得很好(第一个!),直到我意识到文件名也有代表优先级的数字。例如,在文件夹中可能有:

'NV_CX67_mainx.dxf'
'NV_CX67_mainx1.dxf'
'NV_CX67_mainx2.dxf '
'NV_CX67_mainxroad.dxf'
'NV_CX67_motx.dxf'
'NV_CX67_resxroad.dxf'

mainx,mainx1和mainx2是相同的文件类型,但mainx2具有优先级,应该是唯一处理过的文件。目前,我的发言是:

If Instr(1,FileRef, "mainx",1) then

在移动到下一个文件之前,如何添加第二个过滤器以仅处理编号最大的文件?

1 个答案:

答案 0 :(得分:1)

您将完成以下过程

  1. 对输入文件进行排序
  2. 逐个循环浏览每个文件
  3. 将当前文件与您查看的上一个文件相比减去数字以查看它是否更大。
  4. 仅处理您扫描了所有类似项目的项目,以确保此项目的编号最大
  5. 我在下面写了一个例子。请注意,只处理NV_CX67_mainx4.dxf和NV_CX67_mainxroad.dxf:

    Option Explicit
    
    Dim i, sBaseFileName, sPrevFileName, prevBaseFile
    sPrevFileName = "~"
    prevBaseFile = "~"
    Dim arr(5)
    'Initialize test array. This will need to be sorted for this code to work properly
    arr(0) = "NV_CX67_mainx.dxf"
    arr(1) = "NV_CX67_mainx4.dxf"
    arr(2) = "NV_CX67_mainx2.dxf"
    arr(3) = "NV_CX67_mainxroad.dxf"
    arr(4) = "NV_CX67_motx.dxf"  
    arr(5) = "NV_CX67_resxroad.dxf" 
    
    'Loop through the array
    For i = LBound(arr) to UBound(arr) 
        If Instr(1, arr(i), "mainx",1) Then 'Check prev qualifier
            sBaseFileName = getsBaseFileName(arr(i))
    
            'First Case     
            If prevBaseFile = "~" Then
                prevBaseFile = sBaseFileName
                sPrevFileName = arr(i)
            'Tie - Figure out which one to keep based on number at end of file name
            ElseIf prevBaseFile = sBaseFileName Then
                sPrevFileName = GetMaxFile(sPrevFileName, arr(i))
                prevBaseFile = getsBaseFileName(sPrevFileName)
    
            'New Case - Process prev case
            Else
                'Process File
                MsgBox ("Processing " + sPrevFileName)
                'Capture new current file for future processing
                sPrevFileName = arr(i)
                prevBaseFile = getsBaseFileName(sPrevFileName)          
            End If
        End If
    Next
    'If last file was valid process it
    If sPrevFileName <> "~" Then
        MsgBox ("Processing " + sPrevFileName)
    End If
    
    'Return the larger of the two files based on numbers at end. 
    'Note "file9.txt" > "file10.txt" in this code
    Function GetMaxFile(sFile1, sFile2)
        GetMaxFile = sFile1
        If sFile2 > sFile1 Then
            GetMaxFile = sFile2
        End If
    End Function
    
    'Return the file without extension and trailing numbers
    'getsBaseFileName("hello123.txt") returns "hello"
    Function getsBaseFileName(sFile)
        Dim sFileRev
        Dim iPos
    
        getsBaseFileName = sFile
        sFileRev = StrReverse(sFile)
    
        'Get rid of the extension
        iPos = Instr(1, sFileRev, ".",1)
        If iPos < 1 Then
            Exit Function
        End If
        sFileRev = Right(sFileRev, Len(sFileRev)-iPos)
    
        'Get rid of trailing numbers
        Do
            If InStr(1, "1234567890", Left(sFileRev, 1), 1) Then
                sFileRev = Right(sFileRev, Len(sFileRev)-1)
            Else
                Exit Do
            End If
         Loop While(Len(sFileRev) > 0)
    
        getsBaseFileName =  StrReverse(sFileRev)
    End Function