How to rename files based on Excel?

I have studied this repeatedly: Rename Files Based on Excel File [Example]
It feels a bit complicated. My request is very simple, as shown in the figure, How to rename files based on Excel?
35

You can download this zip file for data to demo and test the macro:
Rename.zip (8.3 KB)

Reference:
Rename Files Based on Excel File [Example].kmmacros

I guess "feels" is in the hands/eyes of the beholder. :wink:
It feels simple to me. Of course, I wrote it. :wink:

IAC, I think all you should need to do to use this macro, is change the first Action:

image

In your case, it looks like you would set this KM Variable to "FileName2"

Have you tried that?

1 Like

Yes, I tried it ten times and it is still wrong. I just changed the file in the example to my own, but I still get an error.

Macro I have not made any changes.

image

Here's the problem:

image

Still failing, if it is convenient, please download the file I provided and test it on your computer?

@guxianbang, OK, my bad. I apologize. I had forgotten that the Excel lookup that used the KM Variable "Local__ID" was based on extraction of a digits from the file name, not the entire file name.

So, please replace this Action:

image

with this Action:

Split Path Action (v9.0.3)

Split Path.kmactions (479 B)

Make sure you have changed the data in the first Action of the Macro to this, which matches the Excel file in your Zip file:

image

and that you have:

  1. Open Excel file to the worksheet that contains the lookup data for renaming
    • Must have a Header Row with the Names of your Columns
    • The Lookup ID must be in Column A
    • The Returned Renaming Data must be in a Column between B-ZZ
  2. Open the Folder in Finder that contains the files to be renamed.
    • This is known as the "Finder Insertion Folder"

Here's the entire revised macro.
Please let us know if this now works for you.

MACRO:   Rename Files Based on Excel File Ver 2 [Example]

**Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+**
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

#### DOWNLOAD Macro File:
<a class="attachment" href="/uploads/default/original/3X/1/e/1eea623ddfd5ee4b27c63d9f2b3891a4b1911b26.kmmacros">Rename Files Based on Excel File Ver 2 [Example].kmmacros</a>
**Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.**


---

### ReleaseNotes

Author.@JMichaelTX		KM Forum Topic 

**PURPOSE:**

* **Rename All Files in Folder Open in Finder Using Excel Data**

**REQUIRES:**

1. **KM 8.2+**
2. **macOS 10.11.6 (El Capitan)**

**NOTICE: This macro/script is just an _Example_**

* It has had very limited testing.
* You need to test further before using in a production environment.
* It does not have extensive error checking/handling.
* It may not be complete.  It is provided as an example to show you one approach to solving a problem.

**How To Use**

1. First, be sure you have setup the Macro following the instructions in "Macro Setup" section.
2. Open Excel file to the worksheet that contains the lookup data for renaming
   * Must have a Header Row with the Names of your Columns
   * The Lookup ID must be in Column A.  Ths is now the base File Name to be changed.
   * The Returned Renaming Data must be in a Column between B-ZZ
3. Open the Folder in Finder that contains the files to be renamed.
   * This is known as the "Finder Insertion Folder"
4. Trigger this macro.
5. The Macro will then loop through each File in the Folder
    * Extract the Lookup ID as the first number in the File Name
    * Search Excel for that ID, and return the text in the Column specified by the ExcelHeaderName variable
    * Rename the file with that text using the orginal file extension

**MACRO SETUP**

* **Carefully review the Release Notes and the Macro Actions**
  * Make sure you understand what the Macro will do.  
  * You are responsible for running the Macro, not me.  ??
.
1. Assign a Trigger to this maro..
2. Move this macro to a Macro Group that is only Active when you need this Macro.  Most likely this will be the Finder app.
3. ENABLE this Macro.
.
* **REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:**
(all shown in the magenta color)
   * Set Local__ExcelHeaderName to Header Cell Value That is Used for Renaming
     * If you need to use more than one Excel Column for the renaming data, just add another Column where you combine the other Columns

TAGS: @Excel @Files @Finder @RegEx @Example

USER SETTINGS:

* Any Action in _magenta color_ is designed to be changed by end-user

ACTION COLOR CODES

* To facilitate the reading, customizing, and maintenance of this macro,
      key Actions are colored as follows:
* GREEN   -- Key Comments designed to highlight main sections of macro
* MAGENTA -- Actions designed to be customized by user
* YELLOW  -- Primary Actions (usually the main purpose of the macro)
* ORANGE  -- Actions that permanently destroy Variables or Clipboards,
OR IF/THEN and PAUSE Actions


**USE AT YOUR OWN RISK**

* While I have given this limited testing, and to the best of my knowledge will do no harm, I cannot guarantee it.
* If you have any doubts or questions:
  * **Ask first**
  * Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.
<img src="/uploads/default/original/3X/2/1/21bd36b61b2338f533df910f6fb668022643cfd4.png" width="723" height="1268">
1 Like

Thank you so much, your macro has succeeded. Thank you for taking the time to solve my problem.

2 Likes