Results 1 to 5 of 5

Thread: fso.movefile

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2004


    I was trying to rename a file under the same directory,like,
    rename "c:\program files\test\file1.txt"
    to "c:\program files\test\file_1.txt"

    I use fso.movefile("c:\program files\test\file1.txt" ,"c:\program files\test\file_1.txt")

    but it only works fine if file_1.txt doesn't exist,
    if "file_1.txt" already exists, there is a message box pop up asking " the file already exists, do you want to replace it?" , I clicked o.k and nothing happened, didn't rename at all. how do I write the code to let user choose to really overwrite it or not to overwrite?

    thanks for your help!

  2. #2
    PowerPoster Dave Sell's Avatar
    Join Date
    Mar 2004

    Re: fso.movefile

    MoveFile Method
    Moves one or more files from one location to another.
    object.MoveFile source, destination 
    The MoveFile method syntax has these parts:
    Part Description 
    object Required. Always the name of a FileSystemObject. 
    source Required. The path to the file or files to be moved. The source argument string can contain wildcard characters in the last path component only. 
    destination Required. The path where the file or files are to be moved. The destination argument can't contain wildcard characters. 
    If source contains wildcards or destination ends with a path separator (\), it is assumed that destination specifies an existing folder in which to move the matching files. Otherwise, destination is assumed to be the name of a destination file to create. In either case, three things can happen when an individual file is moved: 
    If destination does not exist, the file gets moved. This is the usual case.
    If destination is an existing file, an error occurs.
    If destination is a directory, an error occurs. 
    An error also occurs if a wildcard character that is used in source doesn't match any files. The MoveFile method stops on the first error it encounters. No attempt is made to roll back any changes made before the error occurs.
    Important   This method allows moving files between volumes only if supported by the operating system.
    According to this doc, you have to take care manually to make sure the destination filename does not exist.
    Nobody knows what software they want until after you've delivered what they originally asked for.

    Don't solve problems which don't exist.

    "If I had eight hours to cut down a tree, I'd spend six hours sharpening my axe." --- Abraham Lincoln (1809-1865)

    2 idiots don't make a genius.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    New Amsterdam

    Re: fso.movefile

    As an alternative to FSO which does create some unnecessary dependencies you may simply use built-in functionality:
    VB Code:
    1. Private Sub Command1_Click()
    2.     If Dir("c:\temp\test1.txt") = "" Then
    3.         Name "c:\temp\test.txt" As "c:\temp\test1.txt"
    4.     Else
    5.         MsgBox "File: '" & "c:\temp\test1.txt" & "' already exist"
    6.     End If
    7. End Sub

  4. #4
    Super Moderator Hack's Avatar
    Join Date
    Aug 2001
    Searching for mendhak

    To Take RhinoBull's Code One Step Further

    VB Code:
    1. Private Sub Command1_Click()
    2. Dim vResponse As VbMsgBoxResult
    3. If Dir("c:\temp2.txt") = "" Then
    4.    Name "c:\temp.txt" As "c:\temp2.txt"
    5. Else
    6.    vResponse = MsgBox("File: c:\temp2.txt already exists.  Would you like to overwrite it?", vbQuestion + vbYesNo, "Overwright File?")
    7.        If vResponse = vbYes Then
    8.           Kill "c:\temp2.txt"
    9.           Name "c:\temp.txt" As "c:\temp2.txt"
    10.        End If
    11. End If
    12. End Sub

  5. #5

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.