none
Rename Exception RRS feed

  • Question

  • I am trying to run a program written and run successfully using VB 2015 on VB 2017. First time I ran it I needed to switch some code to reflect "My" in Robust Programming. Did all that and that part worked fine. The program is used to make batch corrections in file names. The program runs until it hits this code: My.Computer.FileSystem.RenameFile(Foundfile, HoldString1). HoldString1 has the corrected file name "Z0557 Picture Day 14May17.jpg".  Foundfile is the original file name "Z0557 Picture day 14May17.jpg" Note the change is the word day/Day.  Here is the code and exception:

    Monday, May 22, 2017 11:20 PM

Answers

  • Hank,

    Welcome aboard. :)

    If you think about it, there really is no such thing as "rename"; it's actually moving a file using a different name - even if it's to the same directory.

    Look at the exception message - that "name" is already taken. If you're sure that it's ok, delete that file and continue on; otherwise log it and let the user know there's a conflict.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Monday, May 22, 2017 11:27 PM
  •  If the only difference is the word "day" being changed to "Day",  then that is why.  Changing the casing of the words is not considered as a name change.  I believe the Rename method creates a new copy of the file with the new name in the directory and then deletes the old file.  However,  it can not create a file with the same name in the same directory.

     You could rename the file with something added to the name like "_Renamed" or something,  then rename that file to the original name with the "Day" capitalized.  Just as a simplified example....

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            My.Computer.FileSystem.RenameFile("C:\testfolder\Whacky.png", "WHACKY_Renamed.png")
            My.Computer.FileSystem.RenameFile("C:\testfolder\WHACKY_Renamed.png", "WHACKY.png")
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Hank Rother Tuesday, May 23, 2017 1:54 AM
    Monday, May 22, 2017 11:43 PM
  • Going with the current replies, if you look at File.Move and the exception it's clear why you received this exception. File.Move is basically doing a rename when specifying the same folder with a completely different file name for that file. Moving to My.Computer.FileSystem.RenameFile the same exception holds true. Look at the exceptions in both methods, the are exactly the same method for the same reason.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Hank Rother Tuesday, May 23, 2017 1:54 AM
    Tuesday, May 23, 2017 12:22 AM
    Moderator

All replies

  • Hank,

    Welcome aboard. :)

    If you think about it, there really is no such thing as "rename"; it's actually moving a file using a different name - even if it's to the same directory.

    Look at the exception message - that "name" is already taken. If you're sure that it's ok, delete that file and continue on; otherwise log it and let the user know there's a conflict.


    "A problem well stated is a problem half solved.” - Charles F. Kettering


    Monday, May 22, 2017 11:27 PM
  •  If the only difference is the word "day" being changed to "Day",  then that is why.  Changing the casing of the words is not considered as a name change.  I believe the Rename method creates a new copy of the file with the new name in the directory and then deletes the old file.  However,  it can not create a file with the same name in the same directory.

     You could rename the file with something added to the name like "_Renamed" or something,  then rename that file to the original name with the "Day" capitalized.  Just as a simplified example....

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            My.Computer.FileSystem.RenameFile("C:\testfolder\Whacky.png", "WHACKY_Renamed.png")
            My.Computer.FileSystem.RenameFile("C:\testfolder\WHACKY_Renamed.png", "WHACKY.png")
        End Sub
    


    If you say it can`t be done then i`ll try it

    • Marked as answer by Hank Rother Tuesday, May 23, 2017 1:54 AM
    Monday, May 22, 2017 11:43 PM
  • The program runs until it hits this code: My.Computer.FileSystem.RenameFile(Foundfile, HoldString1).

    According to Windows' file naming rules, you aren't actually changing anything.  Change to something completely different, then change to what you want.
    https://msdn.microsoft.com/en-us/library/system.io.path.gettempfilename(v=vs.110).aspx

    Monday, May 22, 2017 11:45 PM
  • Going with the current replies, if you look at File.Move and the exception it's clear why you received this exception. File.Move is basically doing a rename when specifying the same folder with a completely different file name for that file. Moving to My.Computer.FileSystem.RenameFile the same exception holds true. Look at the exceptions in both methods, the are exactly the same method for the same reason.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Hank Rother Tuesday, May 23, 2017 1:54 AM
    Tuesday, May 23, 2017 12:22 AM
    Moderator
  • Worked perfectly!
    Tuesday, May 23, 2017 2:21 AM
  • Worked perfectly!
    Tuesday, May 23, 2017 2:22 AM
  • Hi Hank,

    I agree with IronRazerz's opinion, there is no difference between new name(Z0557 Picture Day 14May17.jpg) and old name(Z0557 Picture day 14May17.jpg).

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 23, 2017 2:41 AM
    Moderator