Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 212
  • Last Modified:

Change File Names or from deleting from file name

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
Fordraiders
Asked:
Fordraiders
2 Solutions
 
nutschCommented:
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
 
Saqib Husain, SyedEngineerCommented:
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
 
NorieCommented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Martin LissRetired ProgrammerCommented:
Too late:)
0
 
Shanan212Commented:
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
 
Shanan212Commented:
meh too too late
0
 
FordraidersAuthor Commented:
Thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now