+ Reply to Thread
Results 1 to 5 of 5

Catching duplicate filenames when renaming a file

  1. #1
    General
    Guest

    Catching duplicate filenames when renaming a file

    Hi,

    In my code I have to rename some files, using the command:

    Name oldfilename as newfilename

    Sometimes the newfilename already exists so I am looking for a way to
    detect that condition, I tried:

    If iserror(Name oldfilename as newfilename) then
    CODE HERE
    else
    name oldfilename as newfilename
    end if

    However, Excel "Expects list seperator or )" around the oldfilename in
    the iserror statement. Help!

    Also, for this code I am in the middle of an application filesearch so
    I can't search for the file using application.filesearch.

    Thank you!!!

    Phil


  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello Phil,

    Test if the file name exisits use the following code:

    Dim FileExists

    FileExists = Dir("C:\My Documents\Test.xls")


    If the file exists then FileExists = the File Name in this "C:\My Documents\Test.xls".
    If not, FileExists = "". Best part, no error messages.

    Hope this helps,
    Leith Ross

  3. #3
    Robin Hammond
    Guest

    Re: Catching duplicate filenames when renaming a file

    Phil,

    How about

    If Dir(newfilename)<>"" then
    'the file exists
    else
    'save the file
    end if

    Robin Hammond
    www.enhanceddatasystems.com

    "General" <[email protected]> wrote in message
    news:[email protected]...
    > Hi,
    >
    > In my code I have to rename some files, using the command:
    >
    > Name oldfilename as newfilename
    >
    > Sometimes the newfilename already exists so I am looking for a way to
    > detect that condition, I tried:
    >
    > If iserror(Name oldfilename as newfilename) then
    > CODE HERE
    > else
    > name oldfilename as newfilename
    > end if
    >
    > However, Excel "Expects list seperator or )" around the oldfilename in
    > the iserror statement. Help!
    >
    > Also, for this code I am in the middle of an application filesearch so
    > I can't search for the file using application.filesearch.
    >
    > Thank you!!!
    >
    > Phil
    >




  4. #4
    Bob Phillips
    Guest

    Re: Catching duplicate filenames when renaming a file

    Another way

    On Error Resume Next
    Name oldfilename As newfilename
    If Err.Number = 58 Then 'already exists
    MsgBox "file " & newfilename & " already exists"
    End If
    On Error GoTo 0


    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Robin Hammond" <[email protected]> wrote in message
    news:%[email protected]...
    > Phil,
    >
    > How about
    >
    > If Dir(newfilename)<>"" then
    > 'the file exists
    > else
    > 'save the file
    > end if
    >
    > Robin Hammond
    > www.enhanceddatasystems.com
    >
    > "General" <[email protected]> wrote in message
    > news:[email protected]...
    > > Hi,
    > >
    > > In my code I have to rename some files, using the command:
    > >
    > > Name oldfilename as newfilename
    > >
    > > Sometimes the newfilename already exists so I am looking for a way to
    > > detect that condition, I tried:
    > >
    > > If iserror(Name oldfilename as newfilename) then
    > > CODE HERE
    > > else
    > > name oldfilename as newfilename
    > > end if
    > >
    > > However, Excel "Expects list seperator or )" around the oldfilename in
    > > the iserror statement. Help!
    > >
    > > Also, for this code I am in the middle of an application filesearch so
    > > I can't search for the file using application.filesearch.
    > >
    > > Thank you!!!
    > >
    > > Phil
    > >

    >
    >




  5. #5
    General
    Guest

    Re: Catching duplicate filenames when renaming a file

    Thanks, that solves it for me!


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1