View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Tushar Mehta Tushar Mehta is offline
external usenet poster
 
Posts: 1,071
Default Renaming File Statement -How does it work??

As Rob [Bovey] pointed out, it is a legacy capability from the DOS
days. No object associated with them, just native Basic statements.

Yes, Name can be easier to use than the FileSystemObject capability,
but it does have more subtleties attached to it. For one, ChDir
doesn't affect the current *drive* which is given by ChDrive. Use of a
filename without the full path qualification means that VBA will use
the current *drive* and the current path for *that* drive. May not be
what you meant to do!

As far as needing multiple steps with FSO goes, what people don't
realize, often because of the programming dogma they've been exposed
to, is that those multiple steps are often not necessary. For example,

CreateObject("scripting.filesystemobject").movefil e _
"g:\temp\book1.xls", "g:\book1.xls"

is sufficient. Even if one wanted to carry out multiple steps with
this one instance of FSO, one could use

With CreateObject("scripting.filesystemobject")
.movefile _
"g:\temp\book1.xls", "g:\book1.xls"
.movefile _
"g:\book1.xls", "g:\temp\book1.xls"
End With

Of course, in this case, I am relying on the compiler to clean up the
FSO object for me. But then I rely on it for a lot more than just this
cleanup. ;-)


--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Could someone please explain how this Excel 2003 VBA routine works to rename
a file.
Assume oldfilename.txt exist in the C:\temp directory, when the name
statement executes, it is renamed to "newfilename.txt"

Sub RenameTheFiles()
ChDir "C:\Temp\"
Name "oldfilename.txt" As "newfilename.txt"
End Sub

Originally I was using a multi step routine using the FileSystemObject and
setting a fileObject.Name = new name, thus performing the rename. The
simplicity of this code is confusing what I thought I knew about the object
models. Specifically, Name in the above routine is a method? How is it
associated, what is the object or how does it know to rename the file in the
temp directory??? Can anyone give me the complete syntax of the name
statement? I assume there to be several defaults omitted. What role does the
"AS" play? Is this some kind of shorthand for a SET statement?

Puzzled!

Fred Krause.