Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


Rename a file on HD

Posted on 1999-06-26
Medium Priority
Last Modified: 2010-05-02
How do you rename a file on the HD in Visual Basic?
Question by:vienna
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

groone earned 60 total points
ID: 1519981
I've had this snippet for a while and am unsure to whom the auther is, but it works really well.

Option Explicit

Private Sub Form_Load()

   RenameFiles "d:\bat"

End Sub

Private Function GiveNewName%(ByVal Oldname$, ByVal NewName$)

   On Error GoTo Error_In_Renaming

   ' Now rename the file
   Name Oldname$ As NewName$

   ' Return True to indicate a success
   GiveNewName% = True

   Exit Function


   ' Return False to indicate failure
   MsgBox Error$(Err)
   GiveNewName% = False
   Resume Exit_The_Function

End Function

Private Sub RenameFiles(Path$)

Dim t$      ' To hold the filename while renaming
Dim ok%     ' To hold the return values from GiveNewName% and MsgBox
Dim Counter%   ' Counts the files

   ' Get the first filename
   t$ = Dir$(Path$ & "\*.*")
   ' Keep on renaming until the directory contains no unrenamed files
   Do While t$ <> ""

      ' Increment the counter
      Counter% = Counter% + 1
      ' Give new filename that will look like this "FILE####.$$$" where "####" is the Counter%
      ' value and the "$$$" is the original file's extension
      ok% = GiveNewName%(Path$ & "\" & t$, Path$ & "\FILE" & CStr(Counter%) & Right$(t$, 4))

      ' If there was an error renaming the file...
      If Not ok% Then
         ' Ask the user to proceed or quit
         ok% = MsgBox("There was an error renaming " & t$ & " to " & "File" & CStr(Counter%) & Right$(t$, 4) & Chr$(13) & Chr$(13) & "Proceed?", 4 + 32, "Rename files")
         If ok% = 7 Then   ' No was selected
            ' Quit
         End If
      End If

      ' Get new filename
      t$ = Dir$


End Sub

LVL 12

Expert Comment

ID: 1519982
What your looking for is the 'Name' statement. There are three ways it can be used:

Rename a file:
Name "c:\abc.txt" As "c:\xyz.txt"

Rename and move a file:
Name "c:\abc.txt" As "c:\my documents\xyz.txt"

Move a file
Name "c:\abc.txt" As "c:\my documents\abc.txt"

Expert Comment

ID: 1519983
so simple


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question