Results 1 to 7 of 7

Thread: [RESOLVED] error using File.Move

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    69

    Resolved [RESOLVED] error using File.Move

    I am saving files in a folder and rename them to a correct name (according to our system).
    I have the following options:

    1. if the file with the old name exists we want to give it a new name;
    2. if the file name is already correct we just want to make it uppercase, using File.Move
    3. if the file name was not correct we want to change it to the new name, using RenameFile

    Code:
       If File.Exists(row.Item("Document Location") + row.Item("OldName")) And row.Item("Document Name") <> "" Then
                            'does the file exists with the new name?
                            If File.Exists(row.Item("Document Location") + row.Item("Document Name")) Then
                                'if yes then we make the name uppercase
                                File.Move(row.Item("Document Location") + row.Item("OldName"), row.Item("Document Location") + row.Item("Document Name"))
                            End If
                            If Not File.Exists(row.Item("Document Location") + row.Item("Document Name")) Then
                                My.Computer.FileSystem.RenameFile(row.Item("Document Location") + row.Item("OldName"), row.Item("Document Name").ToUpper())
                            End If
                        End If
    Now I get an error when I try to use File.Move to set a filename to uppercase when it accidentely already exists in the folder.

    How can I solve this?

  2. #2
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,435

    Re: error using File.Move

    Quote Originally Posted by clausowitz View Post
    I am saving files in a folder and rename them to a correct name (according to our system).
    I have the following options:

    1. if the file with the old name exists we want to give it a new name;
    2. if the file name is already correct we just want to make it uppercase, using File.Move
    3. if the file name was not correct we want to change it to the new name, using RenameFile

    Code:
       If File.Exists(row.Item("Document Location") + row.Item("OldName")) And row.Item("Document Name") <> "" Then
                            'does the file exists with the new name?
                            If File.Exists(row.Item("Document Location") + row.Item("Document Name")) Then
                                'if yes then we make the name uppercase
                                File.Move(row.Item("Document Location") + row.Item("OldName"), row.Item("Document Location") + row.Item("Document Name"))
                            End If
                            If Not File.Exists(row.Item("Document Location") + row.Item("Document Name")) Then
                                My.Computer.FileSystem.RenameFile(row.Item("Document Location") + row.Item("OldName"), row.Item("Document Name").ToUpper())
                            End If
                        End If
    Now I get an error when I try to use File.Move to set a filename to uppercase when it accidentely already exists in the folder.

    How can I solve this?
    Although Windows preserves the case of a filename it isn't case sensitive - that means it would see "text1.txt" and "Text1.txt" as the same filename and wouldn't allow the move. Try moving it to a temporary name and then moving it to the upper cased name.

  3. #3
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,646

    Re: error using File.Move

    Filenames aren't case sensitive, so you are attempting to move the file onto itself.

    That being said, why try to move it at all? Why not just rename the file like you are already doing in your code with other files?

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    69

    Re: error using File.Move

    Quote Originally Posted by PlausiblyDamp View Post
    Although Windows preserves the case of a filename it isn't case sensitive - that means it would see "text1.txt" and "Text1.txt" as the same filename and wouldn't allow the move. Try moving it to a temporary name and then moving it to the upper cased name.
    But how to catch the error before it terminates my process?
    At the moment when the error ocuures it says Can't create a file that already exists and terminates

  5. #5
    Frenzied Member PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Newport, UK
    Posts
    1,435

    Re: error using File.Move

    Quote Originally Posted by clausowitz View Post
    But how to catch the error before it terminates my process?
    At the moment when the error ocuures it says Can't create a file that already exists and terminates
    You could wrap the failing code in a try catch block, however you could just check if the filename already exists and then handle that case by using a temp file name.

  6. #6
    Frenzied Member
    Join Date
    Nov 2017
    Posts
    1,646

    Re: error using File.Move

    Quote Originally Posted by clausowitz View Post
    But how to catch the error before it terminates my process?
    At the moment when the error ocuures it says Can't create a file that already exists and terminates
    Step 1 is to write code that makes sense
    Step 2 is to enclose any potentially "problematic" code in a Try statement, and Catch exceptions and write code to properly proceed.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Apr 2021
    Posts
    69

    Re: error using File.Move

    Quote Originally Posted by OptionBase1 View Post
    Step 1 is to write code that makes sense
    Step 2 is to enclose any potentially "problematic" code in a Try statement, and Catch exceptions and write code to properly proceed.
    thanks for the compliment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width