Solved

Change File Names or from deleting from file name

Posted on 2013-01-30
7
163 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Too late:)
0
 
LVL 13

Expert Comment

by:Shanan212
Comment Utility
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
Comment Utility
meh too too late
0
 
LVL 3

Author Closing Comment

by:fordraiders
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

INDEX and MATCH can be used to great effect to replace HLOOKUP and VLOOKUP as it does not have the limitation of needing the data to be sorted so that the reference value is in the first column or row. It also has the ability to perform a bi-directi…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now