我有一个工作代码,它将获取一个文件,例如“photo.png”并将其移动到一个文件夹中。如果它已经存在,它将重命名为“1_photo.png”,但如果您有另一个照片名称“photo.png”,它会将其重命名为已经存在且无法使用的文件“1_photo.png”。我想知道解决方案是什么。
Dim grade As String
grade = (FolderBrowserDialog1.SelectedPath)
grade = My.Settings.SD
My.Computer.FileSystem.CreateDirectory(
grade + ("\Pictures"))
Dim filePaths = IO.Directory.GetFiles(grade, "*.png")
For Each filePath In filePaths
Dim filename = IO.Path.GetFileName(filePath)
Dim newPath = IO.Path.Combine(grade + ("\Pictures"), filename)
If IO.File.Exists(newPath) Then
Dim dr = MessageBox.Show($"File {newPath} exists, do you want to keep both files? The recently moved file will have a number added to its name", "", MessageBoxButtons.YesNoCancel)
Select Case dr
Case DialogResult.Cancel
Continue For
Case DialogResult.No
IO.File.Delete(newPath)
Case DialogResult.Yes
If IO.File.Exists(newPath) Then
Dim rn As New Random
My.Computer.FileSystem.RenameFile(newPath, "1_" + filename)
IO.File.Move(filePath, newPath)
MessageBox.Show("Pictures Compiled and Cleaned")
Return
End If
End Select
End If
IO.File.Move(filePath, newPath)