0

I have a working code which will take a file eg, "photo.png" and move it into a folder. If it already exists it will rename it "1_photo.png" although if you have another photo name "photo.png" it will rename it to the file "1_photo.png" that already exits and will not work. I would like to know what the solution is for this.

        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)
Brenduan
  • 9
  • 5

1 Answers1

0

You make a loop conditional on the file existing:

Dim filename = "a.png"
Dim dirname = "c:\temp"

Dim filePath = Path.Combine(dirname, filename)

Dim i as Integer = 1
While(IO.File.Exists(filePath))
  filePath = Path.Combine(dirname, i & "_" & filename)
  i += 1
End While

Eventually, the loop will find a filePath that doesn't exist.

Putting a number at the start of a filename is probably a bad idea by the way. I recommend you put it at the end:

filePath = Path.Combine(dirname, Path.ChangeExtension(filename, i & Path.GetExtension(filename)))

This makes files like photo.png, photo.1.png, photo.2.png, photo.3.png...

I would wrap it up in a function that finds a filename that doesn't clash:

Function GetRelatedNonExistantFilepath(Dim desiredPath as String) As String()

    Dim filename =  Path.GetFilenameWithoutExtension(desiredPath)
    Dim ext = Path.GetExtension(desiredPath)
    Dim dirname = Path.GetDirectoryName(desiredPath)

    Dim filePath = desiredPath

    Dim i as Integer = 1
    While(IO.File.Exists(filePath))
      filePath = Path.Combine(dirname, filename & "_" & i & ext)
      i += 1
    End While

    Return filePath
End Function

You'd hence use it like:

    'want to move to c:\temp\photo.png but it might exist
    Dim dest = "c:\temp\photo.png"

    Dim actualDest = GetRelatedNonExistantFilepath(dest)

    'move file to actual dest
    IO.FIle.Move(sourcePath, actualDest)
Caius Jard
  • 72,509
  • 5
  • 49
  • 80
  • Ok, I have integrated this into the code and it seems to rename the file to grab before actually renaming the file so it returns the error that "photo.1.png" cannot be found. I have tried renaming the file before and after it tries to move it with this ` My.Computer.FileSystem.RenameFile` although that doesn't seem to work. Fix for this? – Brenduan May 30 '20 at 20:27
  • The code I presented does not rename a file, it works with your desired path and filename and gives you a full path that does not exist. If you can point to the place where I use IO.File.Move (the comand that renames a file) then I'll accept the assertion that this code renames files – Caius Jard May 30 '20 at 20:29