Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file renameing (how to) 1

Status
Not open for further replies.

mscallisto

Technical User
Jun 14, 2001
2,990
US
I believe I need some assistance with this one.

How can I rename all the files in a particular folder with the same .extention using this rule:

Example: all filenames are of this structure companynameMMW.nct where MMW represent month and week.

I would like to rename to MMWcompanyname.nct

I can't think of a way to do this with windows explorer and don't know how exactly to do it in VBA.
 
I would suggest looking at the FileSystemObject especially its .Files collection. You should also look at the string manipulation functions (Mid(), Left(), and Right()) to rearrange the name the way that you want.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Hi mscallisto

You could create a macro to do what you want, here is some code amended from an example I have posted here before. This will rename all the .NCT extension files in the directory d:\files (change this to your directory) where the last last 3 chars of their name are numeric to MMWcompanyname.nct

Code:
Sub RenameFiles()
Dim OldFileName As String, NewFileName As String, FolderName As String
Dim TempDate As String
    FolderName = "D:\Files\"
    OldFileName = Dir(FolderName & "*.nct", vbDirectory)
    

    Do While OldFileName <> ""
        'Check last 3 chars of name are numeric
        TempDate = Mid(OldFileName, Len(OldFileName) - 6, 3)
        If IsNumeric(TempDate) Then
            NewFileName = TempDate & Left(OldFileName, Len(OldFileName) - 7) & ".nct"
    
            Name FolderName & OldFileName As FolderName & NewFileName
                    
        End If
        
        OldFileName = Dir
    Loop

End Sub


This is of course assuming the last 3 digits of all the files you wish to rename are numeric, if other .NCT files in this file meet this criteria they'll get renamed also.

This will work on any file type not just .NCT just change the code slightly.

Hope this helps,

A,
 
Here is an example

Code:
Public Sub RenameFiles()
    
    Dim fsoX As FileSystemObject
    Dim strDirectory As String
    Dim strCompanyName As String
    Dim fileX As File
    Dim intMMW_Length As Integer
    
    'Create a new file system object to work with
    Set fsoX = New FileSystemObject
    
    'variables for the rename process
    'directory files are in
    strDirectory = "C:\Test\"
    'the company name
    strCompanyName = "tw"
    'the length of the date part of the file name
    intMMW_Length = 3
    
    'for loop to iterate through each file
    'uses the filesystem object to grab a reference of the directory
    'and then an enumerator of the files contained
    For Each fileX In fsoX.GetFolder(strDirectory).Files
        'rename the file
        fileX.Name = Mid(fileX.Name, Len(strCompanyName) + 1, intMMW_Length) & _
                    Left(fileX.Name, Len(strCompanyName)) & _
                    ".nct"
    Next
    
    'set references to nothing
    Set fileX = Nothing
    Set fsoX = Nothing
    
End Sub

Hope that helps
 
Jayla,

beat you to it ;) Also my code doesn't care about the length of the company name, it will rename files of different company names. It appears you have hard coded the name, which is fine if it never changes.

Take care,

A,
 
Also Dir does not require the reference to Scripting Runtime. FileSystemObject does. Not that this is a big deal. I have not actually tested this kind of operation - comparing Dir with FSO - but I suspect that FSO may be faster. Be interesting to find out....hmmmmm.

Gerry
 
I don't have any preference to either really. I just come from the script world so it is the one I know better. :)

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Thanks to all for your quick responses.

I Tried StuckInTheMiddle's first and it works just great.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top