Solved

Change File Names or from deleting from file name

Posted on 2013-01-30
7
205 Views
Last Modified: 2013-01-30
excel 2010 vba code needed:

I have a folder:

C:\MyPics\

I have 219,00+ files in this folder..All  .jpg..

22222_as01.jpg

324e3_ax02.jpg


etc...

I need the code to take out the characters starting at the Underscore.

So once the code has finished:

2222.jpg
324e3.jpg


Any help appreciated
fordraiders
0
Comment
Question by:fordraiders
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 39

Accepted Solution

by:
nutsch earned 250 total points
ID: 38837147
Here's a macro to do that.

Sub RenameFiles()
Dim FileName As String, sNewName As String, sPath As String

sPath = "C:\MyPics\"

FileName = Dir(sPath & "*.jpg")
Do While FileName <> ""
    If InStr(StrReverse(FileName), "_") > 0 Then
        sNewName = Left(FileName, Len(FileName) - InStr(StrReverse(FileName), "_")) & ".jpg"
        Name sPath & FileName As sPath & sNewName
    End If
    FileName = Dir
Loop

End Sub

Open in new window


Thomas
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38837154
Try this macro

Sub renfiles()
    Dim fpath As String, fnam As String
    fpath = "C:\MyPics\"
    fnam = Dir(fpath & "*.jpg")
    Do While fnam <> ""
        Name fpath & fnam As fpath & Replace(fnam, "_", "")
        fnam = Dir
    Loop
End Sub
0
 
LVL 34

Assisted Solution

by:Norie
Norie earned 250 total points
ID: 38837170
Try this.
Dim strPath As String
Dim strFilename As String
Dim strNewName As String
Dim pos As Long

    strPath = "C:\MyPics\"

    strFilename = Dir(strPath & "*.jpg")

    While Len(strFilename) > 0

        pos = InStr(strFilename, "_")

        If pos > 0 Then
            strNewName = Left(strFilename, pos - 1) & ".jpg"
            Name strPath & strFilename As strPath & strNewName

        End If

        strFilename = Dir()
    Wend

Open in new window

0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 48

Expert Comment

by:Martin Liss
ID: 38837177
Too late:)
0
 
LVL 13

Expert Comment

by:Shanan212
ID: 38837191
Sub GetFFRecentFile()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    Dim FileSys As Object, objFile As Object, myFolder As Object, temp As String
    Dim curFile As String, oldFile As String, iRow As Long, myDir As String
        
    myDir = "C:\MyPics\"
        
    Set FileSys = CreateObject("Scripting.FileSystemObject")
    Set myFolder = FileSys.GetFolder(myDir)
    
    For Each objFile In myFolder.Files
            If Right(objFile.Name, 3) = "jpg" Then
                oldFile = vbNullString
                curFile = vbNullString
                
                temp = InStr(objFile.Name, "_")
                
                If temp > 0 Then
                    oldFile = objFile.Name
                    curFile = Left(objFile.Name, temp - 1) & ".jpg"
                    
                    Name myDir & oldFile As myDir & curFile
                End If
            End If
    Next objFile
    
End Sub

Open in new window

0
 
LVL 13

Expert Comment

by:Shanan212
ID: 38837193
meh too too late
0
 
LVL 3

Author Closing Comment

by:fordraiders
ID: 38838285
Thanks
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

622 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