rename all files in a folder removing first 10 characters of name

chas1

New Member
Joined
Oct 1, 2010
Messages
12
Hi I need code to rename all word doc files in a folder. I require to remove the first 10characters from the file name for the new filename.
Can anyone help me on this. (I am currently doing it via the command prompt to list all files and then generating the new filename through excel and putting the new list of names back into the command prompt to rename file.) I'm guessing this could be done more neatly through VBA code.Thanks in advance.
 
To prompt the user to select a folder, try replacing...

Code:
Folder = "C:\Documents and Settings\oregac1\Desktop\test dhr\test\"

with

Code:
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Folder Selection"
        .InitialFileName = Application.DefaultFilePath & Application.PathSeparator
        .Show
        If .SelectedItems.Count > 0 Then
            Folder = .SelectedItems(1)
        Else
            MsgBox "Canceled!"
            Exit Sub
        End If
    End With
    
    Folder = Folder & Application.PathSeparator

By the way which code are you using? The first one or the second one?
 
Upvote 0

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi Domenic,
i am using the last code you sent on the first one isn't doing anything at all strange how it worked once though for me.
i tried adding the code you forwarded for folder seleciton but got an error "method or data memebr not found" on below line

Code:
 .InitialFileName = Application.DefaultFilePath & Application.PathSeparator

the code i am using is
Code:
Option Explicit
Sub FileRenameLess10Charsrev2()
    Dim FileSys As Object
    Dim Files As Object
    Dim File As Object
    Dim Folder As String
 
    'enter the path to your folder between the quotation marks below
    'Folder = "C:\Documents and Settings\oregac1\Desktop\test dhr\test\"
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Folder Selection"
        .InitialFileName = Application.DefaultFilePath & Application.PathSeparator
        .Show
        If .SelectedItems.Count > 0 Then
            Folder = .SelectedItems(1)
        Else
            MsgBox "Canceled!"
            Exit Sub
        End If
    End With
 
    Folder = Folder & Application.PathSeparator
 
    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set Files = FileSys.GetFolder(Folder).Files
 
    For Each File In Files
        If InStr(1, File, ".doc") > 0 Then
            Name File As Folder & Mid(File.Name, 11)
        End If
    Next File
 
End Sub
 
Upvote 0
i am using the last code you sent...

Good...

i tried adding the code you forwarded for folder seleciton but got an error "method or data memebr not found" on below line

Code:
 .InitialFileName = Application.DefaultFilePath & Application.PathSeparator

I tested the code and it seems to work fine. InitialFileName is definitely a member of the FileDialog object. So make sure that you don't have some sort of spelling mistake on your actual code.
 
Upvote 0
hi domenic,
code is as exactly as i have pasted here. does mainly what i want so thanks for your help.

rgds
Chas
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top