| | | Forum Member
       
Group: Forum Members Last Login: 4/2/2005 4:33:00 PM Posts: 50, Visits: 1 |
| I am writing a program that requires me to rename files and folders. Im using the FileSystemObject and i cant find anything in this book that tells how to rename a file or folder. I can move them copy them create them and delete them but i cant find anything that tells me the syntax for renaming a file or folder? Can anyone help me out here? Thanks |
| | | | 
Forum God
       
Group: Forum Members Last Login: 1/27/2008 2:50:27 PM Posts: 334, Visits: 16 |
| I did a search for renaming files and folders using vb but i came up with no results. I dont think there is a straight forward statement to do this. I suggest trying the api...
Milo  |
| | | | Forum God

Group: Moderators Last Login: Yesterday @ 7:18:05 AM Posts: 1,100, Visits: 10,194 |
| The easiest way is to use the name function. Keep in mind if you try to rename a file that is in use you will throw an error. Private Sub Command1_Click() 'To rename Directory Name "C:\New Folder" As "C:\MyFolder" 'To rename file Name "C:\MyFolder\New Text Document.txt" As "C:\MyFolder\MyText.txt" End Sub
|
| | | | 
Forum God
       
Group: Forum Members Last Login: 1/27/2008 2:50:27 PM Posts: 334, Visits: 16 |
| Okay, maybe there is a short statement of doing it then  lol
Milo  |
| | | | Forum God
       
Group: Forum Members Last Login: 3/13/2006 2:34:13 AM Posts: 794, Visits: 338 |
| You may have noticed that the FileSystemObject doesn't have a Rename method anywhere in its object model. This does not, however, mean that you can't rename a file. The answer lies in the realization that you can use the MoveFile method, specifying two different file names in the same directory. A sample of this technique is shown below:
<% Dim fso Set fso = CreateObject("Scripting.FileSystemObject") fso.MoveFile "d:dummytest.txt", "d:dummytest2.txt"
|
| | | | Junior Member
       
Group: Forum Members Last Login: 5/24/2007 7:39:28 AM Posts: 14, Visits: 24 |
| | I know this is an incredibly old thread, but I found it while Googling for my answer. Well, I have a command button that executes this code, but I want it to check if the folder exists first. I have this code inside of a command button so far, which renames an existing folder, but causes the application to close if the path doesn't exist. Private Sub cmdRename_Click() Name App.Path & "\" & frmLaunch.txtName.Text As App.Path & "\" & txtRename.Text MsgBox "Directory renamed to " & txtRename.Text Unload Me End Sub EDIT: Found a solution! Private Sub cmdRename_Click() If Dir(App.Path & "\" & frmLaunch.txtName.Text, vbDirectory) <> frmLaunch.txtName.Text Then MsgBox "Game directory does not exist." Exit Sub Else Name App.Path & "\" & frmLaunch.txtName.Text As App.Path & "\" & txtRename.Text MsgBox "Directory renamed to " & txtRename.Text Unload Me End If End Sub |
| |
|
|