Solved

How to do a find replace on file names within a directory with a script.

Posted on 2011-03-21
6
401 Views
Last Modified: 2012-05-11
I have a an excel sheet that will have first/last name and an account number.

I have a directory of files that are named via acct number, I would like to script a way to rename all those account numbers to first/last name.  For example find replace "account number" with the name listed by the account number.

Does anybody know of a good way to do that, or have a working script
0
Comment
Question by:Preston55
6 Comments
 
LVL 9

Accepted Solution

by:
McOz earned 250 total points
ID: 35182813
Here is a very simple code which should do what you need:
Sub renameFiles()
    myPath = "C:\New Folder\" 'the path to your directory (remember backslash at end)
    i = 2 'start on second row, assuming you have headers
    On Error Resume Next 'if a matching file is not found in the directory, skip it
    Do Until Cells(i, 1).Value = "" 'loop down the excel sheet until the first empty cell
        oldName = myPath & Cells(i, 1).Value & ".xlsx" 
        Name oldName As myPath & Cells(i, 2).Value & Cells(i, 3).Value & ".xlsx"
        i = i + 1
    Loop
End Sub

Open in new window


It works with a sheet such as the one attached. Book2.xlsx
0
 
LVL 32

Assisted Solution

by:DrDamnit
DrDamnit earned 250 total points
ID: 35182850
Fart.

Find And Replace Text.

http://sourceforge.net/projects/fart-it/files/

You can do a find and replace on file names, their contents, type, etc...

Easily run from a batch file, or from a single command line.
0
 

Author Comment

by:Preston55
ID: 35189222
side question on that, some of the files have extended names for example

john smith is account number 34576,

so he may have two files which would be 34576_abcdef.tif and 34576_mnop.tif

With using those scripts can it preserver the _abcdef, yet change the 34576 to John Smith

Hope that makes sense, Thanks Again.

0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 9

Expert Comment

by:McOz
ID: 35189472
OK, if that is the case you will need to use this code:
Sub renameFiles()
    myPath = "C:\New Folder\" 'the path to your directory (remember backslash at end)
    i = 2 'start on second row, assuming you have headers
    Do Until Cells(i, 1).Value = "" 'loop down the excel sheet until the first empty cell
        strFile = Dir(myPath & Cells(i, 1).Value & "*.xls*", vbNormal)
        Do While strFile <> ""
            Name strFile As Replace(strFile, Cells(i, 1).Value, Cells(i, 2).Value & Cells(i, 3).Value)
            strFile = Dir
        Loop
        i = i + 1
    Loop
End Sub

Open in new window


This will replace the account number in any filename that begins with the account number.

Cheers
0
 
LVL 10

Expert Comment

by:wls3
ID: 35199222
Is this Excel--as in .xls/.xslx--or .csv?  Also, do you have a preference on which scripting language?  I see you put several.

Also, as someone who has a predilection for using Excel to manipulate stuff in a pinch, I am wondering why not try this low-rent solution: assuming your have columns A (first name), B (last name) and C (account number), why not use something this is in column D:

="ren C:\your\folder\path\"&C1&".ext C:\your\folder\path\"&A1&B1&".ext"

Open in new window


The output would be:

A: Will
B: Steele
C: 12345
D: ren C:\your\folder\path\12345.ext C:\your\folder\path\WillSteele.ext

Drag this down to your last row and then copy/paste Column D values into a .bat file.  Double-click the .bat file and renaming is complete.  I would also STRONGLY recommend testing and backing up your directory before running this.
0
 
LVL 10

Expert Comment

by:wls3
ID: 35199232
Sorry, posted too soon.  The command in traditional cmd shell would be:
="ren C:\your\folder\path\"&C1&".ext "&A1&B1&".ext"

Open in new window

Do not use the full path to prefix the second file name.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Set OWA language and time zone in Exchange for individuals, all users or per database.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

20 Experts available now in Live!

Get 1:1 Help Now