Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Renaming File Statement -How does it work??

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.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 811
Default Renaming File Statement -How does it work??

Hi Fred,

The easiest thing to do would be to stick your cursor on Name in your
code module and press F1. This will bring up the VBA help topic with all the
details. The short answer to your question is that this is a legacy BASIC
function that's not a method of any object and it has no other arguments.

In the case you've shown, it uses the C:\Temp directory by default
because you haven't specified a directory in your arguments and you've set
the C:\Temp directory as the current directory with the ChDir statement. You
could also have done this instead and made it one line of code:

Name "C:\Temp\oldfilename.txt" As "C:\Temp\newfilename.txt"

--
Rob Bovey, Excel MVP
Application Professionals
http://www.appspro.com/

* Take your Excel development skills to the next level.
* Professional Excel Development
http://www.appspro.com/Books/Books.htm

"KAHAUS" wrote in message
...
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.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,119
Default Renaming File Statement -How does it work??

This is one of those places where I sometimes like to use a DOS Shell.
Something like this...

application.shell("Command.com /c ren C:\Test.xls C:\Test.txt")

The advantage of using a shell is that you can do mutiple files at once...

application.shell("Command.com /c ren C:\*.xls C:\*.txt")

Depends on what you are trying to do as to whether a DOS shell is the best
solution or not...
--
HTH...

Jim Thomlinson


"KAHAUS" wrote:

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.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 121
Default Renaming File Statement -How does it work??

application.shell("Command.com /c ren C:\Test.xls C:\Test.txt")

gives me an error message: "Object doesn't support this property or method"

"Jim Thomlinson" wrote in message
...
This is one of those places where I sometimes like to use a DOS Shell.
Something like this..."

application.shell("Command.com /c ren C:\Test.xls C:\Test.txt")

The advantage of using a shell is that you can do mutiple files at once...

application.shell("Command.com /c ren C:\*.xls C:\*.txt")

Depends on what you are trying to do as to whether a DOS shell is the best
solution or not...
--
HTH...

Jim Thomlinson


"KAHAUS" wrote:

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.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 121
Default Renaming File Statement -How does it work??

I removed Application. and it worked

thanks

"William Benson" wrote in message
...
application.shell("Command.com /c ren C:\Test.xls C:\Test.txt")

gives me an error message: "Object doesn't support this property or
method"

"Jim Thomlinson" wrote in message
...
This is one of those places where I sometimes like to use a DOS Shell.
Something like this..."

application.shell("Command.com /c ren C:\Test.xls C:\Test.txt")

The advantage of using a shell is that you can do mutiple files at
once...

application.shell("Command.com /c ren C:\*.xls C:\*.txt")

Depends on what you are trying to do as to whether a DOS shell is the
best
solution or not...
--
HTH...

Jim Thomlinson


"KAHAUS" wrote:

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.







  #6   Report Post  
Posted to microsoft.public.excel.programming
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.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 121
Default Renaming File Statement -How does it work??

dogma :-) I love it!

"Tushar Mehta" wrote in message
om...
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.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with renaming work sheet name D Moonn Excel Discussion (Misc queries) 2 April 19th 09 12:21 PM
VB file renaming. El Bee Excel Worksheet Functions 2 January 23rd 08 01:49 AM
renaming all work-sheets at once Narendra Boga[_2_] Excel Discussion (Misc queries) 4 June 6th 07 03:24 AM
vb code for renaming a work sheet with a cell reference John Britto Excel Discussion (Misc queries) 3 September 17th 06 07:12 PM
Renaming a file Paul C[_2_] Excel Programming 1 January 23rd 04 04:13 PM


All times are GMT +1. The time now is 02:55 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"