2014-06-20 116 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 
相关问题