Results 1 to 6 of 6

Thread: Excel 2003 Rename problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Excel 2003 Rename problem

    I have a variable called Dest which holds "e:\Test\test01.txt" and I want to update that file to "c:\Newtest01.txt"

    The command Name Dest as "c:\Newtest01.txt" correctly brings up a 'File already exists' error.

    So I Name Dest as Dest & ".2".

    My logic is it will now be renamed to "e:\Test\test01.txt.2" and Name Dest as "c:\Newtest01.txt" will succeed because Dest no longer exists.

    But the command to rename it with ".2" added also brings up 'File already exists' error

    Here I go nuts. Of course it exists, that what I want to rename it. But why the error, what am I doing wrong?

    Thanks, ABB

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel 2003 Rename problem

    So I Name Dest as Dest & ".2".
    works for me, but only the first time of course

    My logic is it will now be renamed to "e:\Test\test01.txt.2" and Name Dest as "c:\Newtest01.txt" will succeed because Dest no longer exists.
    though your variable dest indicates destination, it is in fact the source for the file move, in anycase it is the target that already exists and is causing the error, otherwise you have your file names in the wrong position in the name statement

    if you want to overwrite the previous file check it exists first and kill

    vb Code:
    1. if len(dir("c:\Newtest01.txt")) > 0 then kill "c:\Newtest01.txt"

    Name Statement


    Renames a disk file, directory, or folder.

    Syntax

    Name oldpathname As newpathname

    The Name statement syntax has these parts:

    Part Description
    oldpathname Required. String expression that specifies the existing file name and location — may include directory or folder, and drive.
    newpathname Required. String expression that specifies the new file name and location — may include directory or folder, and drive. The file name specified by newpathname can't already exist.



    Remarks

    The Name statement renames a file and moves it to a different directory or folder, if necessary. Name can move a file across drives, but it can only rename an existing directory or folder when both newpathname and oldpathname are located on the same drive. Name cannot create a new file, directory, or folder.

    Using Name on an open file produces an error. You must close an open file before renaming it. Name arguments cannot include multiple-character (*) and single-character (?) wildcards.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Excel 2003 Rename problem

    Pete I still don't quite get it. Is it not possible to rename a file, (which of course must exist) without
    Killing it first.

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Excel 2003 Rename problem

    if the destination file (newpathname) already exists you can not rename to that destination

    i think you have source and destination confused in your name statement
    Pete I still don't quite get it. Is it not possible to rename a file, (which of course must exist) without
    Killing it first.
    if you kill the source file first it is gone and you can not rename
    Last edited by westconn1; May 29th, 2011 at 01:25 AM.
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5
    Hyperactive Member
    Join Date
    Oct 2010
    Location
    Indiana
    Posts
    457

    Re: Excel 2003 Rename problem

    If you desire to keep all of the old files, then you need to make the file name more unique. You can try changing the existing file and use a date/time combination within the new name, that way the file name you give it will never be the same, so you will never get a "file already exists" error when you change it's name.
    Last edited by nO_OnE; May 29th, 2011 at 04:19 AM.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jun 2010
    Posts
    1,462

    Re: Excel 2003 Rename problem

    Thanks very much you you both, all under control now

    Cheers, ABB

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