Problem in incrementing the filename [duplicate]
Possible Duplicate:
Way to get unique filename if specified filename already exists (.NET)
Have a look at the following code:
Dim counter As Integer = 0
While System.IO.File.Exists("C:\Desktop\Sample.xls")
counter = counter + 1
Dim fileName As String = String.Format("{0}({1}){2}", System.IO.Path.GetFileNameWithoutExtension(newfile), counter.ToString(), System.IO.Path.GetExtension(newfile))
newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
End While
If the file exists, the new file name will be Sample(1).xls
. Up to this point, it is working fine. If the file name itself is Sample(1).xls
, the new filename should be Sample(2).xls
. But in this code I am getting it as Sample(1开发者_开发百科)(2).xls
.
How to avoid this problem? Any suggestions?
On the first pass, in this line:
newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
newfile
will be sample(1).xls
. Then when you format it with
System.IO.Path.GetFileNameWithoutExtension(newfile)
It will return sample(1)
which is {1}
in the format string, then in the second pass of the loop you add ({2})
which is 2
, and that makes sample(1)(2)
.
To fix this, you need to not keep adding the (x) to the file name, like so:
Dim counter As Integer = 0
Dim origfile As String = "C:\Desktop\Sample.xls"
Dim newfile As String = origfile
While System.IO.File.Exists(newfile)
counter = counter + 1
newfile = String.Format("{0}({1}){2}", System.IO.Path.GetFileNameWithoutExtension(newfile), counter.ToString(), System.IO.Path.GetExtension(newfile))
End While
use newfile instead of origfile
There was another problem in your original code in the line
While System.IO.File.Exists("C:\Desktop\Sample.xls")
Because if C:\Desktop\Sample.xls
exists, it will cause an infinite loop.
Try this
Dim counter As Integer = 0
Dim tempBaseFile As String = "C:\Desktop\Sample.xls"
Dim newFile As String = tempBaseFile
While System.IO.File.Exists(newFile)
counter = counter + 1
Dim fileName As String = String.Format("{0}({1}){2}", System.IO.Path.GetFileNameWithoutExtension(tempBaseFile), counter.ToString(), System.IO.Path.GetExtension(newFile))
newFile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
End While
please try this
Dim counter As Integer = 0
While System.IO.File.Exists("C:\Desktop\Sample.xls")
counter = counter + 1
Dim fileName As String = String.Format("{0}({1}){2}", System.IO.Path.GetFileNameWithoutExtension(Mid(newfile, 1, InStr(newfile, "(") - 1)), counter.ToString(), System.IO.Path.GetExtension(newfile))
newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
End While
the reason is because you need to get the filenamewithoutextension WITHOUT the stuff in the parenthesis (###)
logic can be :
Dim counter As Integer = 0
Dim testFileName as String = "Sample"
Dim searchFileName as String = testFileName
While System.IO.File.Exists("C:\Desktop\" & searchFileName & ".xls")
counter = counter + 1
searchFileName = testFileName & "(" & counter & ")"
End While
Dim fileName As String = String.Format("{0}({1}){2}", System.IO.Path.GetFileNameWithoutExtension(newfile), counter.ToString(), System.IO.Path.GetExtension(newfile))
newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName)
you can do this as well:
Dim counter As Integer = 0
Dim newFileName As String = orginialFileName
While File.Exists(newFileName)
counter = counter + 1
newFileName = String.Format("{0}({1}", orginialFileName, counter.ToString())
End While
精彩评论