Solved

Change File Names or from deleting from file name

Posted on 2013-01-30
7
204 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 47

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

729 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