Consulting

Results 1 to 11 of 11

Thread: Solved: Change the complicated names of the files in a folder as regular (1 - 2 - 3 ... n )

  1. #1
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location

    Solved: Change the complicated names of the files in a folder as regular (1 - 2 - 3 ... n )

    Hi there,

    I want to make regular (1 - 2 - 3 ... n ) name the files in a folder which files are ".bmp" surnamed instead of the complicated names.

    For example:

    The folder is:
    C:\Pictures

    The Files of this Directory:
    ASAD.bmp
    WER.bmp
    SDLS.bmp
    TEE.xls
    FDFDL.doc
    DPFDP.bmp
    GKFLLF.jpg
    LAST.bmp

    My wish is;
    Give new names only to .bmp surnamed files with a VBA code.

    After code is running;

    instead of ASAD.bmp : 1.bpm
    instead of WER.bmp : 2.bpm
    instead of SDLS.bmp : 3.bpm
    TEE.xls : No changing
    FDFDL.doc : No changing
    instead of DPFDP.bmp : 4.bpm
    GKFLLF.jpg : No changing
    instead of LAST.bmp : 5.bpm

    I tried some different ways to do it but i couldn't.

    Thanks a lot.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub LoopFolders()
    Dim oFSO As Object
    Dim Folder As Object
    Dim Files As Object
    Dim file As Object
    Dim i As Long
    Dim sPath As String


    Set oFSO = CreateObject("Scripting.FileSystemObject")

    sPath = "C:\Pictures"

    Set Folder = oFSO.GetFolder(sPath)

    For Each file In Folder.Files
    If file.Type = "Bitmap Image" Then
    MsgBox file.Path
    i = i + 1
    Name file.Path As Replace(file.Path, file.Name, i & ".bmp")
    End If
    Next file

    Set oFSO = Nothing

    End Sub
    [/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [vba]Option Explicit
    Sub ReNames()
    Dim MyPath As String, MyName As String, i As Long
    MyPath = "C:\Pictures\"
    MyName = Dir(MyPath)
    Do While MyName <> ""
    If UCase(Right(MyName, 3)) = "BMP" Then
    i = i + 1
    Name MyPath & MyName As MyPath & i & ".bmp"
    End If
    MyName = Dir
    Loop
    End Sub
    [/vba]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    [VBA]Option Explicit
    Option Compare Text

    Sub ChangeBMP()
    Dim path As String
    Dim i As Long
    path = "C:\Pictures\"

    With Application.FileSearch
    .NewSearch
    .LookIn = path
    .Filename = "*.bmp"
    If .Execute() > 0 Then
    For i = 1 To .FoundFiles.Count
    Name CStr(.FoundFiles(i)) As path & i & ".bmp"
    Next
    End If
    End With
    End Sub[/VBA]




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  5. #5
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location

    Soo fast solutions.!

    I wanted just only one "eye" from God, God gave me three eyes!!

    Thank you very much mdmackillop and xld and malik641, these are perfect.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  6. #6
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by Erdin? E. Ka
    thank you very much msmackillop and malik641, these are perceft.
    Well, I didn't know Ms. mackillop was writing your code for you


    and no problem

    BTW, I noticed you changed it....that's okay...it's still funny to me




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  7. #7
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Hey Bob,

    When it comes to file manipulation I've noticed you seem to like to use the FileSystemObject approach...so why do you use the Name file As new_name method when you could use the .Move or .MoveFile methods?

    Just wondering




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  8. #8
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Quote Originally Posted by malik641
    Well, I didn't know Ms. mackillop was writing your code for you


    and no problem

    BTW, I noticed you changed it....that's okay...it's still funny to me

    Excuse me, i wrote quickly and did it wrong.

    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by malik641
    Hey Bob,

    When it comes to file manipulation I've noticed you seem to like to use the FileSystemObject approach...so why do you use the Name file As new_name method when you could use the .Move or .MoveFile methods?

    Just wondering
    I use FSO because both FileSearch and Dir are flawed, but when I can use pur VB I do.

  10. #10
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by xld
    I use FSO because both FileSearch and Dir are flawed, but when I can use pur VB I do.
    I see.



    Just when I liked my solution too




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  11. #11
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Quote Originally Posted by Erdin? E. Ka
    ...I wanted just only one "eye" from God, God gave me three eyes!...
    Just a little correction:

    It's a Turkish Proverb:
    The blindman wished just only 1 eye from God, then God gave him 3 eyes.

    Thanks again to kindly helps. See you.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •