如果存在文件创建一个具有版本号的新文件
我正在尝试检查是否存在具有给定名称的文件,并且它是否确实创建了一个新的名称,该名称结束V2,然后是V3等。
Sub checkFileExists()
Dim strFileExists, strFileName As String
Dim nextDay as Date
Dim i As Integer
strFileName = file_name
strFileExists = Dir(strFileName)
If strFileExists = vbNullString Then
strFileName = file_name
Else
i = 1
Do
i = i + 1
strFileName = file_name & "_v" & i
strFileExists = Dir(strFileName)
Loop Until strFileExists = vbNullString
strFileName = file_name & "_v" & i
End If
End Sub
上面的代码几乎总是创建文件名,即使此文件已经退出。
strFileName = "Plan_" & Format(nextDay, "yyyy-mm-dd")
I am trying to check if a file with a given name exists and if it does create a new one with name that ends v2, then v3 and so on.
Sub checkFileExists()
Dim strFileExists, strFileName As String
Dim nextDay as Date
Dim i As Integer
strFileName = file_name
strFileExists = Dir(strFileName)
If strFileExists = vbNullString Then
strFileName = file_name
Else
i = 1
Do
i = i + 1
strFileName = file_name & "_v" & i
strFileExists = Dir(strFileName)
Loop Until strFileExists = vbNullString
strFileName = file_name & "_v" & i
End If
End Sub
Above code create almost always file name even if this file already exits.
strFileName = "Plan_" & Format(nextDay, "yyyy-mm-dd")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此功能将返回下一个未使用的文件名版本:
This function will return the next unused filename version: