2016-05-12 2 views
1

У меня есть папка, которую я буду перебирать, чтобы обрабатывать файлы по-разному на основе их имен файлов. Выполняя хорошее действие на моем скрипте (первый!), Пока я не понял, что будут имена файлов, которые также имеют числа, представляющие приоритет. Например, в папке могут быть: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 

Как я мог бы добавить 2-й фильтр для обработки только файл с самым высоким номером, прежде чем перейти к следующему файлу?

ответ

1

Вы собираетесь поражают следующего процесс

  1. Сортировки входных файлы
  2. Петли через каждый файл один на один
  3. Сравнивает текущий файл с предыдущим вы смотрели на минус номера, чтобы узнать, больше ли это.
  4. только обработать деталь вы просканировали все подобные элементы, чтобы обеспечить этот имеет наибольшее число

Я написал пример ниже. Обратите внимание, что только 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 
Смежные вопросы