Solved

Bulk rename files to Win95 format

Posted on 2004-08-13
14
237 Views
Last Modified: 2010-05-02
Hi,

I need to bulk rename a large amound of files held in various sub directories to 42 char length (including 3 char extension)

Can anyone suggest some quick code to do that for me?

Thanks in advance
Matt
0
Comment
Question by:razorhazor
  • 7
  • 3
14 Comments
 
LVL 19

Expert Comment

by:Shauli
ID: 11795470
As far as I recall, win95 format is 8 characters and extension, not 42, so what are you after, renaming to 8 characters length or to 42 characters length?

S
0
 

Author Comment

by:razorhazor
ID: 11795506
42 characters ala xbox FATX
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11795549
OK :), any particular way you want to rename to? How do you want the renamed file to look like? can you post an example of a current filename and the desired renamed filename?

S
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:razorhazor
ID: 11795589
if they could just be renamed to say the first 39 chars of the existing filename that would be cool.
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11795660
So its 42 including extension, which will make it 38 plus dot plus three extension. If that is correct then give me 10, otherwise, stop me right away  :)

S
0
 
LVL 19

Accepted Solution

by:
Shauli earned 250 total points
ID: 11795820
Try this:

'where you want to execute.
Private Sub Command1_Click()
Call RenameForXbox("C:\myFolder\mySubFolder\")
End Sub


Public Sub RenameForXbox(ByVal sbPath As String)
Dim myFileName As String
                myFileName = Dir(sbPath, vbDirectory)
                Do While Not myFileName = vbNullString
                    Select Case myFileName
                        Case ".", ".."
                        Case Else
                            If Len(myFileName) >= 38 Then
                                'if filename is equal to or greater then 38 characters
                                Name myFileName As Left(myFileName, 38) & Right(myFileName, 4)
                            Else
                                'if filename is shorter then 38 characters
                                Name sbPath & myFileName As sbPath & Left(myFileName, Len(myFileName) - 4) & Right(myFileName, 4)
                            End If
                    End Select
                    myFileName = Dir
                Loop

End Sub

S

0
 
LVL 19

Expert Comment

by:Shauli
ID: 11795836
ps. Make sure you have a backup before you test it, though :)

S
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11796533
Ops, one correction, it might affect those filenames which are exactly 38 length, so

change this line

 If Len(myFileName) >= 38 Then

To be this line

 If Len(myFileName) >= 42 Then
'                                  ^^

S
0
 

Author Comment

by:razorhazor
ID: 11797195
cool, cheers mate, will that do sub dirs automatically? thats what I really need.

Cheers man!
Matt
0
 
LVL 19

Expert Comment

by:Shauli
ID: 11797226
No. You'll have to call it for each folder or subfolder, or you can do:

Private Sub Command1_Click()
Call RenameForXbox("C:\myFolder1\mySubFolder1\")
Call RenameForXbox("C:\myFolder1\mySubFolder2\")
Call RenameForXbox("C:\myFolder1\mySubFolder3\")
Call RenameForXbox("C:\myFolder2\mySubFolder1\")
Call RenameForXbox("C:\myFolder2\mySubFolder2\")
'etc etc etc...
End Sub

S
0
 
LVL 7

Assisted Solution

by:Burbble
Burbble earned 250 total points
ID: 11800263
Here's a recursive function that you can use to access files in subdirectories:

-----

Dim AllFolders As New Collection

Private Sub GetSubFolders(ByVal Directory As String)
    Dim FSO, tmpGet, tmpSub, tmpFolder
    Dim CurrentFolders As New Collection

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set tmpGet = FSO.GetFolder(Directory)
    Set tmpSub = tmpGet.SubFolders

    For Each tmpFolder In tmpSub
        If Right$(Directory, 1) = "\" Then
            AllFolders.Add Directory & tmpFolder.Name
            CurrentFolders.Add Directory & tmpFolder.Name
        Else
            AllFolders.Add Directory & "\" & tmpFolder.Name
            CurrentFolders.Add Directory & "\" & tmpFolder.Name
        End If
    Next

    For Each tmpFolder In CurrentFolders
        GetSubFolders (tmpFolder)
    Next
End Sub

Private Sub GetFiles(ByVal Directory As String)
    Dim FSO, tmpGet, tmpSub, tmpFile
    Dim CurrentFiles As New Collection

    Set FSO = CreateObject("Scripting.FileSystemObject")
    Set tmpGet = FSO.GetFolder(Directory)
    Set tmpSub = tmpGet.Files

    For Each tmpFile In tmpSub
        processfile (tmpFile) 'Use your function here, this is the loop for each file
    Next
End Sub

Private Sub Form_Load()
    Dim tmpFolder

    GetSubFolders ("C:\My Music") 'Change this path to whatever you need

    For Each tmpFolder In AllFolders
        GetFiles tmpFolder
    Next
End Sub

-----

-Burbble
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

777 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