2011-06-07 27 views
1

可能重複:
Way to get unique filename if specified filename already exists (.NET)問題在遞增文件名

看一看下面的代碼:

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 

如果該文件存在,新的文件名將是Sample(1).xls。到目前爲止,它工作正常。如果文件名本身是Sample(1).xls,則新文件名應該是Sample(2).xls。但在這個代碼中,我得到它作爲Sample(1)(2).xls

如何避免此問題?有什麼建議麼?

+1

可能dublicate:http://stackoverflow.com/questions/ 3093008 /方式對獲得唯一的文件名,如果指定的文件名,已經存在的網 – Stefan 2011-06-07 17:52:31

回答

0

請儘量將

 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 

的原因是因爲你需要得到filenamewithoutextension沒有東西在括號(###)

1

在第一遍,在這一行:

newfile = System.IO.Path.Combine(ProcessedView.processedPath, fileName) 

newfile將是sample(1).xls。然後,當你與

System.IO.Path.GetFileNameWithoutExtension(newfile) 

格式化,它會返回sample(1)這是在格式字符串{1},然後在循環的第二次添加({2})這是2,這使得sample(1)(2)

要解決這個問題,你需要不繼續增加第(x)的文件名,例如:

 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 

有在行你原來代碼中的另一個問題

While System.IO.File.Exists("C:\Desktop\Sample.xls") 

因爲如果C:\Desktop\Sample.xls存在,它將導致無限循環。

0

邏輯可能是:

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) 
0

你可以這樣做,以及:

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 
1

試試這個

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