【发布时间】:2015-05-12 10:32:46
【问题描述】:
我附上了一个代码,但是,这只会找到文件夹中存在的文件。
我想要的是文件的增量计数器。问题是有时版本会以 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
【问题讨论】:
-
似乎这不是整个代码。第一个循环位于哪个子系统中?另一个问题:如果版本不以1开头,它会给你一个错误吗?
-
嗨@EngJon,第一个“版本检查”来自我保存文件的子目录。但是,这个 sub 只是一个测试,看看我是否可以让它工作,然后替换那个版本检查。不,现在数组只会列出文件夹中的文件数量,但不会保存或做任何其他事情。版本检查将转到第一个可用值,因此如果存在的文件是 3 和 4,它将以 1 开头 :( 我希望它是 5。希望您理解。
-
版本检查只是假设没有更高的版本。如果您知道起始版本永远不会高于 4,您不仅可以在循环中检查版本 1,还可以强制它查找版本 4。您可以在实际循环之前的单独 for 循环中执行此操作。
-
我不知道会有多少,它可以从 1 到 9 不等(我会说)。问题是我们将这个文件导入到文件夹中,所以有时我们会导入版本 4,因此我们需要创建版本 5。所以如果宏可以自动回忆文件夹中找到的最高数字,那就太酷了.
-
您可以遍历文件夹中的所有文件,剪切第一部分(名称和周),然后逐个检查字符直到它不再是数字值,然后从该字符剪切 -> 你得到版本。然后将这些字符串转换为数字,将它们放入数组(列表)中并对其进行排序。这样你会得到最高的数字