+ Reply to Thread
Results 1 to 9 of 9

renaming files with macro

  1. #1
    Registered User
    Join Date
    12-17-2008
    Location
    Singapore
    MS-Off Ver
    2002
    Posts
    71

    renaming files with macro

    I have several files in one folder. C:/data/
    These files are named as date_xxxx.wk1. where xxx are random and the date format is YYYYMMDD.

    Everyday I need to rename them as date_cellvalueA1.xls where the date format is DDMMYY. Cellvalue A1 is taken from each of the respective workbook.

    As I need to rename them on a daily basis. Can I use 2 field, one to determine the directory path and one to determine what file name to search for.

    If the files contain the YYYYMMDD, it will be rename to DDMMYY_cellvalueA1.xls

  2. #2
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: renaming files with macro

    Hi
    Save the file in the folder where you want to rename the files. Run the macro by clicking the button on sheet1. Test it on a sample 3-4 files in a folder before using it. Col A will list current files. col B will list files with new names.
    Hope that helps
    Ravi
    Attached Files Attached Files

  3. #3
    Registered User
    Join Date
    04-28-2009
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: renaming files with macro

    Quote Originally Posted by ravishankar View Post
    Hi
    Save the file in the folder where you want to rename the files. Run the macro by clicking the button on sheet1. Test it on a sample 3-4 files in a folder before using it. Col A will list current files. col B will list files with new names.
    Hope that helps
    Ravi
    Hello Ravi -

    I am interested in the macro you created. I have a similar task, where I need to name name images files. Where Column A will have the existing filename, and Column B will have the new file name.

    I am looking at your macro and am wondering if I change the following lines if it will work (Sorry for my noob-ness I have never done anything with VBA before). I deleted two lines and changed the line in BOLD

    So if I have a directory full of images:
    1.jpg
    2.jpg
    3.jpg
    4.jpg

    and my excel as the following (note it may not contain everything in the dir):
    | Column A | Column B |
    1.jpg | aa1.jpg
    3.jpg | cc3.jpg
    4.jpg | dd4.jpg


    Sub kaffal()
    Dim z As Long, e As Long, g As Long
    Dim f As String, b As String
    Sheets("Sheet1").Select
    Cells(1, 1) = "=cell(""filename"")"
    Cells(1, 2) = "=left(A1,find(""["",A1)-1)"
    Cells(2, 1).Select
    f = Dir(Cells(1, 2) & "*.xls")
    Do While Len(f) > 0
    ActiveCell.Formula = f
    ActiveCell.Offset(1, 0).Select
    f = Dir()
    Loop
    z = Cells(Rows.Count, 1).End(xlUp).Row
    For e = 2 To z
    If Cells(e, 1) <> ActiveWorkbook.Name Then
    b = Cells(1, 2)
    Name Cells(1, 2) & Cells(e, 1) As b
    Cells(e, 2) = b
    End If
    Next e
    MsgBox "Renaming is complete."
    End Sub
    Last edited by judoGTI; 04-28-2009 at 04:38 PM.

  4. #4
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: renaming files with macro

    HI
    Try these macro codes
    Please Login or Register  to view this content.
    Ravi

  5. #5
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: renaming files with macro

    kaffar, Ravi, judoGTI

    Here is another possibility I cobbled together with help from several people on these forums.
    If you find anything needs to be changed, please let me know also.

    HTH

    John
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    04-28-2009
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: renaming files with macro

    Quote Originally Posted by jolivanes View Post
    kaffar, Ravi, judoGTI

    Here is another possibility I cobbled together with help from several people on these forums.
    If you find anything needs to be changed, please let me know also.

    HTH

    John
    Hey John -

    This one works great for me, the only problem I have is if it can't find a file in the directory the macro stops. Is there a way I can make it continue to the next row and skip ones it doesn't find?

    What Im trying to do, is match peoples security badge picture to their employee number at work, so when people search for their extension they can see the pic of the person. The only thing is sometimes the excel doc contains records of people who havent had their picture taken yet. (I hope that makes sense), so I'd like it to skip over any files it cannot find and continue to work through the spreadsheet.

    Think its possible?

  7. #7
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: renaming files with macro

    JudoGTI
    Sorry, I don't quite follow you. If you click on the "Get Files" button, you have to select the files you want to rename. You can select consecutive multiple files by holding down the Shift key or select non consecutive multiple files by holding down the Ctrl key while selecting with your mouse. I have another file somewhere that brings up the picture you choose so you can rename it. Is that what you're after?
    Let me know and I'll hunt it down in my excel files I have.

    HTH
    John

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,516

    Re: renaming files with macro

    judoGTI
    Is the attached file of any help?
    Let me know.
    Regards.
    John
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    04-28-2009
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: renaming files with macro

    Quote Originally Posted by jolivanes View Post
    JudoGTI
    Sorry, I don't quite follow you. If you click on the "Get Files" button, you have to select the files you want to rename. You can select consecutive multiple files by holding down the Shift key or select non consecutive multiple files by holding down the Ctrl key while selecting with your mouse. I have another file somewhere that brings up the picture you choose so you can rename it. Is that what you're after?
    Let me know and I'll hunt it down in my excel files I have.

    HTH
    John
    John -

    My mistake, I forgot the original purpose of the macros. I guess Im sort of 'customizing (not really but I'll explain)' its purpose.

    First challenge) I'm getting a list of id numbers from a database. Technically speaking I should have 1 picture for each ID. But because the people responsible for retaining the pictures dont do a very good job, when someone is terminated or gets a new photo they didn't delete the old picture. This makes it so I cannot just select the entire directory of photos because some dont have matching new names in the database (I have ~3000 pics for ~1750 database records). But using the CTRL-Click will take forever (over 1000 photos)

    Second Challenge) The people responsible for entering information to the DB are lazy and have no thumbs (Im guessing) because there are a lot of typo's. So I might have a photo for 1234.jpg but the ID in the database might say 123t.jpg When click the 'Change Name in Folder' button I get a message saying File Not Found (that might not be the exact verbiage but you get the point).

    Because Im dealing with 1000's of files and files name (coming from separate sources, directory of pics and DB with ids to rename them) it makes it tough to work around. I was hoping to rename the ones I could (probably 90%) and then have a clear listing of the ones that need personal attention. I hope I explained that well enough. I know Im using the spreadsheet for a purpose that it was not defined for so this 'change/mod' might not be doable, but it would help me out if its an easy enough change.

    Maybe add a check in the For-loop in the 'Change Name in Folder' macro to fetch the value from the cell, verify file exists, if exists, change name, if NOT exists, skip to next grid value?

    Thanks for the help, and for the original spreadsheet.
    Last edited by judoGTI; 04-29-2009 at 05:48 PM. Reason: Spell checking/typos

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1