我想在移动到另一个子文件夹之前重命名文件。我要做的第一件事是获取LOCATION FOLDER上文件的文件名和扩展名。之后,我检查ACTIVE FOLDER上是否存在这些文件。如果是,我通过添加递增的整数重命名它。
例如,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上,当它被移动时,它变成了样本(1)(1).txt',实际上它必须是' sample(2).txt'。
我该怎样做才能获得预期的结果?
提前致谢。
答案 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
我希望它能为您提供更好的线索。
答案 1 :(得分: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