Remove characters from a file name

gtd526

Well-known Member
Joined
Jul 30, 2013
Messages
657
Office Version
  1. 2019
Platform
  1. Windows
Hello,
How to remove characters from beginning of a file in a certain folder?
I can identify the folder from a cell. (myPath="A1")
File Name (example):
07. White Knuckles.mp3
07 White Knuckles.mp3
7 White Knuckles.mp3

Maybe removing from the first space and backwards?
Thank you.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
If you want to rename the file, then you can do a saveas using the name with the omitted characters, then delete the old file.
If you only want to edit the text of a file name on a sheet then:

VBA Code:
Sub t()
With Range("A1")
    .Characters(1, 1).Delete
    .Characters(2, 1).Delete
End With
End Sub
 
Upvote 0
If you want to rename the file, then you can do a saveas using the name with the omitted characters, then delete the old file.
If you only want to edit the text of a file name on a sheet then:

VBA Code:
Sub t()
With Range("A1")
    .Characters(1, 1).Delete
    .Characters(2, 1).Delete
End With
End Sub
It only using Range A1 not the entire list beginning in A1 while
removing the first 1 or 2 characters.

It's a list of file names beginning in A2 (not A1) to A???.
I would like it to begin in A2, working the range to A???
and remove all characters before and including a (space) or a (.).
Maybe looking for a (space) or a (.) in the beginning(within first 5 characters or spaces) of file (not including the extension (.)) name and delete everything
before and including the (space) and (.).
It can be 2 diff macros, if needed.
Here is what I'm working with:
Replace Files.xlsm
AB
1Old File NameNew File Name
21 Intro.mp3
302. Under And Over It.mp3
403. Burn It Down.mp3
504. American Capitalist.mp3
605. Hard To See.mp3
706. Coming Down.mp3
807. Bad Company.mp3
908. White Knuckles.mp3
1009. Drum Solo.mp3
1110. Far From Home.mp3
1211. Never Enough.mp3
1312. War Is The Answer.mp3
1413. Remember Everything.mp3
1514. No One Gets Left Behind.mp3
1615. The Bleeding.mp3
17
18
19
20
Sheet1
 
Upvote 0
How about
+Fluff New.xlsm
AB
1Old File NameNew File Name
21 Intro.mp3Intro.mp3
302. Under And Over It.mp3Under And Over It.mp3
403. Burn It Down.mp3Burn It Down.mp3
504. American Capitalist.mp3American Capitalist.mp3
605. Hard To See.mp3Hard To See.mp3
706. Coming Down.mp3Coming Down.mp3
807. Bad Company.mp3Bad Company.mp3
908. White Knuckles.mp3White Knuckles.mp3
1009. Drum Solo.mp3Drum Solo.mp3
1110. Far From Home.mp3Far From Home.mp3
1211. Never Enough.mp3Never Enough.mp3
1312. War Is The Answer.mp3War Is The Answer.mp3
1413. Remember Everything.mp3Remember Everything.mp3
1514. No One Gets Left Behind.mp3No One Gets Left Behind.mp3
1615. The Bleeding.mp3The Bleeding.mp3
Results
Cell Formulas
RangeFormula
B2:B16B2=REPLACE(A2,1,FIND(" ",A2),"")
 
Upvote 0
If you are looking to actually rename filenames in a particular folder (path in cell "A1" as you mentioned), you may want to use the following code. The first space and everything before it in the file name is cut out and the file is then renamed, provided the file also exists.
VBA Code:
Public Sub Example()

    Dim oWs         As Worksheet
    Dim sNameOld    As String
    Dim sPath       As String
    
    sNameOld = "07. White Knuckles.mp3"     ' <<<< change accordingly
    
    Set oWs = ActiveSheet                   ' <<<< change accordingly
    
    sPath = IIf(Right(oWs.Range("A1"), 1) = "\", oWs.Range("A1"), oWs.Range("A1") & "\")
    If Not RenameFile(sPath & sNameOld, RemovePrefix(sNameOld)) Then
        MsgBox "Source file [" & sPath & sNameOld & "] doesn't exist"
    End If
End Sub

Public Function RemovePrefix(ByVal argString As String) As String
    Dim r   As Long
    Dim s   As Long
    r = Len(argString)
    If r > 0 Then
        s = InStr(1, argString, " ", vbTextCompare)
        If Not s = 0 Then
            RemovePrefix = Mid(argString, s + 1, r - s)
        End If
    End If
End Function

Public Function RenameFile(ByVal argFullName As String, ByRef argNewName As String) As Boolean
    Dim fso     As Object
    Dim oFile   As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(argFullName) Then
        Set oFile = fso.GetFile(argFullName)
        oFile.Name = argNewName
        RenameFile = True
        Set oFile = Nothing
        Set fso = Nothing
    End If
End Function
 
Upvote 0
How about
+Fluff New.xlsm
AB
1Old File NameNew File Name
21 Intro.mp3Intro.mp3
302. Under And Over It.mp3Under And Over It.mp3
403. Burn It Down.mp3Burn It Down.mp3
504. American Capitalist.mp3American Capitalist.mp3
605. Hard To See.mp3Hard To See.mp3
706. Coming Down.mp3Coming Down.mp3
807. Bad Company.mp3Bad Company.mp3
908. White Knuckles.mp3White Knuckles.mp3
1009. Drum Solo.mp3Drum Solo.mp3
1110. Far From Home.mp3Far From Home.mp3
1211. Never Enough.mp3Never Enough.mp3
1312. War Is The Answer.mp3War Is The Answer.mp3
1413. Remember Everything.mp3Remember Everything.mp3
1514. No One Gets Left Behind.mp3No One Gets Left Behind.mp3
1615. The Bleeding.mp3The Bleeding.mp3
Results
Cell Formulas
RangeFormula
B2:B16B2=REPLACE(A2,1,FIND(" ",A2),"")
Looks good. I will test when I get back from work.
Thank you.
 
Upvote 0
Looks good. I will test when I get back from work.
Thank you.
Here are my results.
Replace Files.xlsm
AB
1Old File NameNew File Name
21 Intro.mp3#VALUE!
302. Under And Over It.mp3#VALUE!
403. Burn It Down.mp3#VALUE!
504. American Capitalist.mp3#VALUE!
605. Hard To See.mp3#VALUE!
706. Coming Down.mp3#VALUE!
807. Bad Company.mp3#VALUE!
908. White Knuckles.mp3#VALUE!
1009. Drum Solo.mp3#VALUE!
1110. Far From Home.mp3#VALUE!
1211. Never Enough.mp3#VALUE!
1312. War Is The Answer.mp3#VALUE!
1413. Remember Everything.mp3#VALUE!
1514. No One Gets Left Behind.mp3#VALUE!
1615. The Bleeding.mp3#VALUE!
Sheet1
Cell Formulas
RangeFormula
B2:B16B2=REPLACE(A2,1,FIND(" ",A2),"")
 
Upvote 0
Did my formula work or not? I am guessing not given the result you got from Fluff's formula. With Sheet1 active, put this formula in an empty cell and tell us what it displays...

=CODE(MID(A2,2,1))
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,331
Members
449,077
Latest member
jmsotelo

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