Renaming files from Access

G

Guest

Is there some code that I can run that will programatically rename all the
files in a particular folder on my desktop. I have about 1000 files in it.
How I need the name changed is this:
There is a 1 one the end of every file that I need removed. the file
extension doesn't need changed, just the last character of the name removed.
For example the file Sample1.wma needs to be renamed to Sample.wma
thanks,
ck
 
D

Dirk Goldgar

Charlie said:
Is there some code that I can run that will programatically rename
all the files in a particular folder on my desktop. I have about
1000 files in it. How I need the name changed is this:
There is a 1 one the end of every file that I need removed. the file
extension doesn't need changed, just the last character of the name
removed. For example the file Sample1.wma needs to be renamed to
Sample.wma

The Name statement changes the name of a file, and the Dir() function
can return a list of all the files in a directory. So your code might
look something like the following untested "air code":

Dim strFileName As String
Dim strNewName As String
Dim strNamePart As String
Dim strExtPart As String
Dim I as Integer

strFileName = Dir("C:\Your Path\Your Folder\*.*")

Do Until Len(strFileName) = 0

I = InStrRev(strFileName, ".")
If I = 0 Then
strNamePart = strFileName
strExtPart = vbNullString
Else
strNamePart = Left$(strFileName, I - 1)
strExtPart = Mid$*strFileName, I)
End If

If Right$(strNamePart, 1) = "1" Then
' Construct new file name.
strNewName = _
Left$(strNamePart, Len(strNamePart) - 1) & strExtPart
' Rename the file.
Name strFileName As strNewName
End If

' Get next file in folder
strFileName = Dir()
Loop
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top