Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact
A1VBCode Forums
Home      Members   Calendar   Who's On
Welcome Guest ( Login | Register )
      

Home » Classic Visual Basic (VB 6 or earlier) » General Visual Basic » Rename files and folders in VB


Rename files and folders in VBExpand / Collapse
Author
Message
Posted 11/9/2004 8:51:10 AM
Forum Member

Forum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum MemberForum 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

 

Post #5891
Posted 11/9/2004 9:30:57 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum 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

Milo!

Post #5894
Posted 11/9/2004 10:38:42 AM
Forum God

Forum God

Group: Moderators
Last Login: Yesterday @ 7:12:27 AM
Posts: 1,107, Visits: 10,602

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
Post #5895
Posted 11/9/2004 10:49:41 AM


Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum 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

Milo!

Post #5896
Posted 11/9/2004 9:51:28 PM
Forum God

Forum GodForum GodForum GodForum GodForum GodForum GodForum GodForum 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"




Post #5911
Posted 11/8/2006 12:28:57 PM
Junior Member

Junior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior MemberJunior 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
Post #20634
« Prev Topic | Next Topic »


Reading This TopicExpand / Collapse
Active Users: 1 (1 guest, 0 members, 0 anonymous members)
No members currently viewing this topic.
Forum Moderators: Admin, Mod, Mark, Keithuk

PermissionsExpand / Collapse

All times are GMT -5:00, Time now is 2:30am