2014-06-20 121 views
0

我想在移動到另一個子文件夾之前重命名文件。我所做的第一件事是獲取LOCATION FOLDER上文件的文件名和擴展名。之後,我檢查ACTIVE FOLDER上是否存在這些文件。如果是的話,我通過添加遞增的整數來重命名它。如何重命名重命名的文件,如果已經存在於asp.net中?

例如,LOCATION FOLDER包含'sample.txt'文件,ACTIVE FOLDER沒有這樣的文件。在這種情況下,我不需要重命名文件'sample.txt',我需要做的就是將其移動到ACTIVE FOLDER。但是當ACTIVE FOLDER包含這樣的文件名時,移動它時必須重命名爲sample(1).txt,並且當LOCATION FOLDER上的另一個文件在移動時具有文件名'sample.txt'時,必須是sample(2).txt。

下面是我的代碼

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 


    Dim dir As New DirectoryInfo("C:\Documents and Settings\Admin\Desktop\LOCATION") 
    Dim Folder1Files As FileInfo() = dir.GetFiles() 

    For Each nFile As FileInfo In Folder1Files 

     Dim fileName As String = Path.GetFileNameWithoutExtension(nFile.Name) 
     Dim fileExt As String = Path.GetExtension(nFile.Name) 
     Dim newFileName As String 
     Dim fileNumber = 0 


     If File.Exists("C:\Documents and Settings\Admin\Desktop\LOCATION\ACTIVE FOLDER\" & fileName & fileExt) Then 

      fileNumber += 1 
      newFileName = String.Format("{0}({1}){2}", fileName, fileNumber, fileExt) 


      File.Move("C:\Documents and Settings\Admin\Desktop\LOCATION\" & fileName & fileExt, "C:\Documents and Settings\Admin\Desktop\LOCATION\ACTIVE FOLDER\" & newFileName) 

     Else 
      File.Move("C:\Documents and Settings\Admin\Desktop\LOCATION\" & fileName & fileExt, "C:\Documents and Settings\Admin\Desktop\LOCATION\ACTIVE FOLDER\" & fileName & fileExt) 
     End If 


    Next 


End Sub 

每當我試圖調試上面的代碼,以及「sample.txt的」存在時,移動它成爲「樣本(1)的.txt」,因此它是正確的,但是當' sample.txt'在LOCATION FOLDER上再次存在時,它會變成'sample(1)(1).txt',實際上它必須是'sample(2).txt'。

我該怎麼做才能獲得預期的效果?

在此先感謝。

+0

嘗試沒有做加法操作 - 剛剛離開fileNumber爲1.請勿在1 –

+0

它不起作用先生加一... –

回答

1

試試看看這段代碼吧,如果有任何語法錯誤,我已經將代碼從C#轉換爲VB了。

Dim oldDir As String = "C:\Documents and Settings\Admin\Desktop\LOCATION" 
Dim newDir As String = "C:\Documents and Settings\Admin\Desktop\LOCATION\ACTIVE FOLDER\" 
Dim newFileName As String = String.Empty 
Dim dir As New DirectoryInfo(oldDir) 
Dim Folder1Files As FileInfo() = dir.GetFiles() 

For Each nFile As FileInfo In Folder1Files 
    Dim oldFileName As String = Path.GetFileNameWithoutExtension(nFile.Name) 
    Dim fileExt As String = Path.GetExtension(nFile.Name) 
    Dim oldPath As String = oldDir & oldFileName & fileExt 
    Dim newPath As String = newDir & oldFileName & fileExt 
    Dim index As Integer = 1 

    While File.Exists(newPath) 
     newFileName = oldFileName & "(" & index & ")" 
     newPath = newDir & newFileName & fileExt 
     index += 1 
    End While 

    File.Move(oldPath, newPath) 
Next 

我希望它給你一個更好的線索。

0

試試這個:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click 


Dim dir As New DirectoryInfo("C:\Documents and Settings\Admin\Desktop\LOCATION") 
Dim Folder1Files As FileInfo() = dir.GetFiles() 

For Each nFile As FileInfo In Folder1Files 

    Dim fileName As String = Path.GetFileNameWithoutExtension(nFile.Name) 
    Dim fileExt As String = Path.GetExtension(nFile.Name) 
    Dim newFileName As String 
    Dim fileNumber = 0 

    string pathAndFileName = dir & Path.DirectorySeparatorChar & fileName 
    string fileExtension = "." & fileExt 

    ' if file exists then add a file counter at the end of the file name 
    int fileNumber = 1 
    while (File.Exists(pathAndFileName & fileExtension))  ' Check if the file already exists 
    { 
     string fileNameConcatenationStr = "_" & String.Format("{0:0000}", fileNumber)  ' The file name format will be something like --> FileName_0001.csv 
     pathAndFileName = Path.GetDirectoryName(pathAndFileName) & Path.DirectorySeparatorChar & fileName & fileNameConcatenationStr & Path.GetExtension(pathAndFileName)  ' Insert the _0001 string into the file name and path. 
     fileNumber += 1 
    } 

    ' Do your file move here using pathAndFileName & fileExtension 
    File.Move(....) 




Next 
相關問題