簡體   English   中英

使用數組循環遍歷文件夾以使用VBA查找最新版本(計數)?

[英]Loop through folder using array to find lastest version (count) with VBA?

我已經附上了代碼,但是,這只會找到文件夾中存在的文件。
我想要的是文件的增量計數器。 問題是有時版本會以0或1以外的形式開頭,例如3。
Amesto non AN suppliers TEST W20-3 AN那么我希望下一個字符串為4。

我目前正在使用此功能,但只有在第一個為1的情況下才能使用,以此類推。

' Version check
    Do While Len(Dir(strPath2 & "Amesto non AN suppliers TEST W" & week & "-" & version & "*.cif")) <> 0
        version = version + 1
        strPath = getDirectoryPath & "Amesto non AN suppliers TEST W" & week & "-" & version & " " & UserName & ".cif"
    Loop


Sub loadversion()
Dim MyFile As String
Dim Counter As Long

'Create a dynamic array variable, and then declare its initial size
Dim DirectoryListArray() As String
ReDim DirectoryListArray(1000)

'Loop through all the files in the directory by using Dir$ function
MyFile = Dir$("C:\Users\niclas.madsen\Desktop\AP\WAVE3\CIF\*.*")
Do While MyFile <> ""
    DirectoryListArray(Counter) = MyFile
    MyFile = Dir$
    Counter = Counter + 1
Loop

' do something here?!
If MyFile = vbNullString Then

Else

End If 

'Reset the size of the array without losing its values by using Redim Preserve
ReDim Preserve DirectoryListArray(Counter - 1)

For Counter = 0 To UBound(DirectoryListArray)
    'Debug.Print writes the results to the Immediate window (press Ctrl + G to view it)'
    Debug.Print DirectoryListArray(Counter)
Next Counter 
End Sub

要獲得目錄中文件名的最高版本,請插入以下功能:

Function CheckHighestVersion(path As String, cutLettersAtWordBeginning As Integer) As Integer
    Dim file As Variant
    Dim toBeCut As String
    Dim verLength As Integer
    Dim highestVersion As Integer
    highestVersion = 0
    file = Dir(path)

    While (file <> "")
        toBeCut = file
        toBeCut = Mid(toBeCut, cutLettersAtWordBeginning + 1)
        verLength = FindVerLength(toBeCut)
        If verLength = -1 Then
            CheckHighestVersion = 0
            Exit Function
        End If
        toBeCut = Left(toBeCut, verLength)
        If Val(toBeCut) > highestVersion Then
            highestVersion = Val(toBeCut)
        End If
        file = Dir
    Wend
    CheckHighestVersion = highestVersion
End Function

Function FindVerLength(fileName As String) As Integer
    Dim i As Integer
    For i = 1 To Len(fileName)
        If Not IsNumeric(Mid(fileName, i, 1)) Then
            If i = 1 Then
                MsgBox "Couldn't obtain the highest version of the files: " & _
                "The first letter of the version is not numeric. The letter is " & Mid(fileName, i, 1) & _
                ". Please use correct amount of letters to be cut at the beginning of the file name."
                FindVerLength = -1
                Exit Function
            End If
            FindVerLength = i - 1
            Exit Function
        End If
    Next i
    FindVerLength = i
End Function

在您的Sub中調用CheckHighestVersion 路徑僅是目錄(例如C:\\ Test \\),第二個參數是單詞開頭不需要的字母數。 如果我計算正確,則該值應為30+(一周的長度,第25周為2,第7周為1)。 該函數返回該文件夾中包含的最高版本。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM