| Answer: |
In a previous FAQ - How can I rename a file on my Web site through an ASP page? - we looked at how to rename a file on a Web server's filesystem. To accomplish this, though, you had to resort to clever programming, since the FileSystemObject does include any sort of Rename method. In order to rename a file, you need to use the MoveFile method like so:
Dim objFSO Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
objFSO.MoveFile Server.MapPath("/someDir/Foo.asp"), _ Server.MapPath("/someDir/Bar.asp")
Set objFSO = Nothing
|
which will rename the file on your Web site /someDir/Foo.asp to /someDir/Bar.asp.
For more information on Server.MapPath be sure to read: Using Server.MapPath
To rename a directory on a Web server, we must use the same approach using the MoveDirectory method (seeing as there's no RenameDirectory method available). So, if you have a directory on your Web server with the physical path C:\Foo and you wished to rename it to C:\Bar, you could use the following code:
'Create an instance of the FileSystemObject Dim objFSO Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder "C:\Foo", "C:\Bar"
|
That's it! At this point the directory Foo would be renamed Bar. For more information on manipulating the Web server's filesystem using the FileSystemObject be sure to read the technical docs. Also, be sure to check out the FileSystemObject FAQ category!
Happy Programming! |