
VB,如何在保存文件时,碰到文件名重复,则在文件名后加序号后保存?
比如说若文件夹中已有文件名为张三的文件,要保存新的名为张三的文件,则将张三的文件保存为张三1,如果再有张三的文件要保存,则保存为张三2、张三3等等,以此类推。...
比如说若文件夹中已有文件名为张三的文件,要保存新的名为张三的文件,则将张三的文件保存为张三1,如果再有张三的文件要保存,则保存为张三2、张三3等等,以此类推。
展开
3个回答
展开全部
遍历文件夹下面的文件,并统计文件名与“张三”开头的个数,
然后保存文件时,在后面加这个统计的数量,注意如果个数为0,则不加。
然后保存文件时,在后面加这个统计的数量,注意如果个数为0,则不加。
追问
怎么做呢?
展开全部
Private Function GetFilePath(FilePath As String) As String
Dim FolderPath As String, FileName As String, FileType As String
Dim n As Long
GetFilePath = FilePath
FolderPath = Left(FilePath, InStrRev(FilePath, "\"))
FileName = Mid(FilePath, InStrRev(FilePath, "\") + 1, InStrRev(FilePath, ".") - InStrRev(FilePath, "\") - 1)
FileType = Right(FilePath, Len(FilePath) - InStrRev(FilePath, ".") + 1)
If Dir(FilePath) <> "" Then
Do
n = n + 1
If Dir(FolderPath & FileName & CStr(n) & FileType) = "" Then Exit Do
Loop
GetFilePath = FolderPath & FileName & CStr(n) & FileType
End If
End Function
Dim FolderPath As String, FileName As String, FileType As String
Dim n As Long
GetFilePath = FilePath
FolderPath = Left(FilePath, InStrRev(FilePath, "\"))
FileName = Mid(FilePath, InStrRev(FilePath, "\") + 1, InStrRev(FilePath, ".") - InStrRev(FilePath, "\") - 1)
FileType = Right(FilePath, Len(FilePath) - InStrRev(FilePath, ".") + 1)
If Dir(FilePath) <> "" Then
Do
n = n + 1
If Dir(FolderPath & FileName & CStr(n) & FileType) = "" Then Exit Do
Loop
GetFilePath = FolderPath & FileName & CStr(n) & FileType
End If
End Function
追问
不太理解其意思啊,是不是可以简单解释一下?我是要保存图片,有一样名字的图片则在名字后面加序号保存。
追答
比如,你想把文件保存为“D:\张三.jpg”
FilePath="D:\张三.jpg"
则,想要保存的文件夹为:
FolderPath="D:\"
文件类型为:
FileType=".jpg"
文件名为:
FileName="张三"
程序先判断是否存在"D:\张三.jpg",
如果不存在,那就保存为"D:\张三.jpg"。
如果存在,就判断"D:\张三1.jpg"是否存在,...
一直到判断"D:\张三n.jpg"不存在为止。
...
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用if条件语句做
追问
不知道怎么做,无头绪。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询