Solved

Change File Names or from deleting from file name

Posted on 2013-01-30
7
177 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
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 33

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 46

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Copy value from a certain cell 5 24
MS Excel IF AND OR statement 3 28
Google Sheets - Artificial Intelligence 2 17
Excel range I cannot find 8 0
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

920 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now