增加数字现有文件名以避免在同一路径中创建重复文件 [英] Incrementing a digit tin existing file name to avoid duplicate file creation in same path
问题描述
我在特定路径中生成一个excel文件
路径与文件名连接:
Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal)&\ RevvLog_reports\ABC
所以默认文件名是ABC。扩展名
以下是生成文件的代码:
xlWorkBook = oAPP.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets(" sheet1")
for nrow As Integer = 1 To newgrid.Rows - 2
For crow As Integer = 1 to newgrid.Cols - 2
xlWorkSheet.Cells(nrow,crow)= _
newgrid.Cell(nrow,crow).Text
下一页
下一页
xlWorkBook.SaveAs(filename )
我想要defa ult文件是ABC(0).Xlsx
如果生成第二次文件,它应该是ABC(1).Xlsx
如果文件已经存在同名它应该增加+1以避免重复。
像这样每次创建文件时文件夹中都没有歧义。
因为我在支持项目中我不能更改默认路径。
我必须在导出excel时将文件作为弹出窗口打开。
I am generating an excel file in a particular path
the path is concatenated with the file name :
Dim filename As String = Environment.GetFolderPath(Environment.SpecialFolder.Personal) & "\RevLog_reports\ABC"
so the default file name is "ABC."extension""
Here is the code to generate the file:
xlWorkBook = oAPP.Workbooks.Add(misValue)
xlWorkSheet = xlWorkBook.Sheets("sheet1")
For nrow As Integer = 1 To newgrid.Rows - 2
For crow As Integer = 1 To newgrid.Cols - 2
xlWorkSheet.Cells(nrow, crow) = _
newgrid.Cell(nrow, crow).Text
Next
Next
xlWorkBook.SaveAs(filename)
I want the default file to be ABC(0).Xlsx
if second time file is generated it should be ABC(1).Xlsx
if a file is already existing with same name it should be incremented by +1 to avoid duplication.
like this each time file should be created without ambiguity in the folder.
As I am in a support project I cannot change the default path.
and I have to open the file as a popup while exporting the excel.
推荐答案
有2种可能性:
- 您生成了您的文件名,然后在此文件存在的情况下查看您的路径。如果没有,你可以使用文件名 - 否则你必须增加(或添加)你的索引并重复这个。
- 你读了你的路径的完整目录, look,匹配的最后一个文件名是。
There are 2 Possibilities :
- you generate your Filename and after that you look in your path if this file exists. If not, you can use the Filename - else you have to increment (or add) your index and repeat this.
- you read the complete Directory of your path and look, which the last filename that matches is.
这篇关于增加数字现有文件名以避免在同一路径中创建重复文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!