Renaming Files in Directory B from Files in Directory A

I am copying the names of files in one folder and then pasting those same names to replace the file names of each equivalent file in a second folder. The macro works exactly as I would like, however I’d like to further automate the process by having the copy act on each file in the source folder and then paste onto each file of the destination folder. In other words, I’m just looking for the way to do a "For Each File in these collections" but instead of a specific path, just have the collection be the files in the currently-selected folder. Is that possible?
Thanks in Advance,
Tom

It sounds like you are looking for this (instead of a path, it will use the currently selected folder in the Finder). The Token %FinderSelection% is what gets the path of the currently selected Folder.

Note that the folder has to be selected. It won't work if you are in the open Folder and simply viewing the files.

An alternative is this, which will act on the selected files in the Finder. So, you would open the folder where the files are and select them first.

1 Like

Hi, @TomMxEdit. @Zabobon showed you two ways. There is a third: The lines in %FinderSelections% (note the s at the end)

Here's an example macro that also includes the menu item to select all of the items in a folder that you have open...

Download: Process Files In Folder.kmmacros (33 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2

1 Like

Thank you, Zabobon for those pointers. Unfortunately, the way the macro works is to start at the first file selected in the list and then it copies the name, selects the equivalent file in a different Finder window, selects that file and pastes, etc. So I guess what I am looking for is a way to perform the macro on each file in the open folder — exactly as you say it won’t work in your first example.

My current workaround, which doesn’t take too much longer to execute, is to open the Keyboard Maestro editor and manually select the folder to define the path to the files I wish to perform the macro on. If that is the best way to do it, that is fine. Since there are often up to 40 files within each of the two folders (source names and destination names), it still saves me a tremendous amount of time and protects me from mistakes to manually select the folder from which to start.

Thanks again,

Tom

1 Like

Hi again, @TomMxEdit. After posting the above I remembered that %FinderSelections% has a minor bug: if the selection includes a symbolic link, then it returns the target file, not the symbolic link. @peternlewis is aware of this issue and told me that it will be fixed in the next release. In the meantime, this version of the above macro will avoid that issue.


UPDATE 1 (2023-05-12 2253): I updated the macro below with AppleScript code that @ccstone kindly included in a DM to me. Here's are the comments he included with the improved code:

Download: Process Files In Folder (with AppleScript).kmmacros (34 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2

Hi Jim,

Thank you very much for both replies. Does your Macro with the AppleScript essentially perform a "For each file in the folder…" without selecting all files? The reason I ask is that I select (highlight) the top file in the source folder and select the top file in the destination folder. Then I return focus to the source folder with the top file selected and perform the copy and paste function, using the return key and arrow keys to move through the list. So, ideally, I’d like to be able to perform a macro similar to the following, but have the folder selected be the enclosing folder when I start to run the macro.

Sorry for my noobie lack of syntax. I’m simply a music editor, rather than a programmer and am trying to label each track in a 3K CD library. So, as I’ve said below, even if I have to manually pick the path to the folder enclosing the files — as I’ve successfully done — I’m still ahead of the game.

Tom

No, %FinderSelections% and the AppleScript assume that all of the items are selected.

I'm not sure I understand your objective, but for any path, you can use the Get File Attribute action and specify the parent path (a.k.a. the enclosing folder).

If you look at the macro I provided above, you'll see that I used that action, but in the example I was returning the file name attribute. (Note: When I provided that example, I wasn't at all clear regarding your objective.)

@TomMxEdit, take a look at the macro I provided. (Based on what you just stated it seems like you'll want to remove the Select All at the beginning). You can still use that improved AppleScript (even if you only have one file selected).

Hope that helps (some), @TomMxEdit. I'd be glad to help more if needed if you restate your objective in the context of what I provided. Also, it would be very helpful if you provided an example source and destination folder and described your objective relative to the examples folder contents.

Hey Tom,

It's always a good idea to provide a reasonably detailed overview of the task you're trying to accomplish.

  • What's the initial state?
  • What processes are involved?
  • What's the desired outcome?

What you're doing sounds painfully cumbersome and slow.

I would probably build a table in BBEdit (or CotEditor, or Numbers, or Excel) of what name in folder 1 is supposed to match up with what name in folder 2 and then let Keyboard Maestro go to town on the renaming.

Copy ⇢ Move Focus ⇢ Paste ⇢ Repeat

Is not an efficient process...

-Chris

Hi Chris and Jim,

Yeah, I know…I think this might be better done with an AppleScript and I will try yours, Jim when I get a chance.

• The initial state is: I have a folder of new transfers of audio files with generic names (i.e. 01 Track01, 02 Track02, etc.) in a left window and a folder of older transfers with the correct names (i.e. 1_Title Music from A Clockwork Orange, 2_The Thieving Magpie (Abridged), etc.)

• I have a good KM macro set up which copies the name from the first file in the right window, switches focus to the equivalent file in the left window, pastes the name onto that file in the left window, then changes focus back to the right window and copies the name of the next file. It then moves back to the left window and continues the process. It’s actually quite fast and works flawlessly as long as the windows stay in the same positions and I have the top file selected in each window when I first run the macro. There are always the same number of tracks in the same positions in both the source (right) window and the destination (left) windows.

• The desired outcome is to select the containing folder of the source (right) window automatically. Currently I do it manually by using the “For Each Item in a Collection Execute Actions” and select the enclosing folder by navigating to it and selecting it via KM. Doesn’t take that long, but I was wondering if there was a way to do so automatically. In other words, if I am focused on the the source folder, could I automatically have KM make that selection of the enclosing folder? Sounds like that is what your AppleScript will do with a small tweak, Jim.

Thanks both for the great advice!! I’ll look at this over the next few days. I’ve attached a screen shot of the menu item with the correct folder selected as I currently have it set up. In this case for the soundtrack to “A Clockwork Orange.”

Tom

Hey Tom,

Okay, that's what I thought you were doing.

Here's a better way.


Download: Rename Files in Right-Hand Folder from Left-Hand Folder v1.00.kmmacros (8.3 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

USAGE NOTES:

  • The Hotkey Trigger is presently F1 – change as desired.
  • The LEFT-HAND folder is the correct version name folder – as you describe in your process – and MUST be FRONTMOST.
  • The RIGHT-HAND folder contains the items to be renamed and MUST be index 2 (or right behind the frontmost folder).

Make sure you test with duplicates, before you use this macro in earnest.

-Chris

1 Like

Hi, @TomMxEdit. Now that I understand your objective, here's the approach I would take.

Download: Rename Folder Files Using a Model Folder.kmmacros (53 KB)

Macro-Image


Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 13.3.1 (22E772610a)
  • Keyboard Maestro v10.2

In the macro header comment, you'll see this note:

Hi Chris,

That is a fantastic solution! It works nearly instantaneously and will be the way I change these names going forward.

I’ll study it some more as I go along to understand exactly how it works.

It’s much more elegant than my solution, which was basically to have the macro type the return key to select the name of the file, command-C to copy the name and then click a screen location to select the target folder. Then another return to select the destination file name, command-V to paste, arrow down, wash, rinse repeat. Entertaining to watch it progress, but somewhat slow in the execution.

Thanks for taking the time to program this! It will be a huge time saver.

Tom

2 Likes

Hi Jim,

Thanks for the macro! I wasn’t able to progress past the OK button. As you say, by clicking on the button, you exit the macro. How would you continue stepping forward without clicking that OK? In other words, how does one enter nothing? I’m sure it’s quite simple, but I’m not seeing it right now.

Thanks again,

Tom

1 Like

Hi, @TomMxEdit. I suspect you had the Finder windows open but then triggered the macro using the Keyboard Maestro Editor. When you do that, the Finder window is no longer in the foreground. If you had left the Finder window in the foreground and triggered the macro with a hot key, you would have seen a different dialog.

I just uploaded a revised version of the macro that presents different options when this scenario occurs.


Regardless, if you try the revised macro you will see that you now have two good options (@ccstone's and this one). One difference, however, is that my macro will present at least one dialog each time you run it. (It will direct you to open the destination folder.) That may be a pro or con. On the plus side, you can have as many Finder windows open as you want. In fact you can have just one with the source is one tab, and the destination in another. I like the approach of guiding use, particularly if it's with a macro that might sit dormant for weeks and then I come back later and try to use it.

If you have everything set up correctly before running @ccstone's macro, it will work great! If my memory serves me correctly, he's not a big fan of dialogs. (He's trying hard to not make fun of the approach I used. :wink: I kid; if fact @ccstone saw my macro and gave me a tip to improve it. As you probably know, he's an incredible resource–and a great dude!!)


Here's a short video that shows the macro flow. Two dialogs appear because the Finder is not in the foreground when the macro was triggered.

2023-05-18 09.28.51

Hi Jim,

You are correct, Sir! I was running it from inside KM. I’ve now tried it out as designed — from within Finder — and it works as advertised. Another good solution from a different angle and I suppose a bit more idiot-proof, which in my case could come in handy!

Thanks for the revision and the explanation,

Tom

1 Like

That macro is just basic AppleScript, except where I use AppleScriptObjC to perform the sort.

You have to use AppleScript to get the targets of Finder Window 01, and Finder Window 02 in a "simple" automated fashion.

The FinderInsertionLocation text-token uses AppleScript behind the scenes to get the path of the frontmost Finder window (or the Desktop if no other accessible window is open).

And of course you can use the Prompt for File action to painfully, and slowly select files or folders.

In general I prefer to set things up just so in the Finder and then run a macro that does all the other work, and I have a bunch of Finder macros to make this easier and reduce the likelihood of error.


The appended macro uses AppleScript only to acquire the PATHs for Finder Window 01 (source names) and Finder Window 02 (items to be renamed), and it uses Keyboard Maestro native actions to do everything else.

I've added some extra error-handling to this macro to attempt to make sure the user has the correct windows on the left and right and that their indexes (front-to-back) are correct.

In any case – you now have a model for performing this task with mostly Keyboard Maestro native actions.


Download: Rename Files in Right-Hand Folder from Left-Hand Folder (KM) v1.00.kmmacros (16 KB)

Macro-Image

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.

System Information
  • macOS 10.14.6
  • Keyboard Maestro v10.2

2 Likes

Excellent and works equally quickly. Thanks for the update!

Tom

This is why when asking for help on the forum users should always give an overview of what they're trying to do.

It saves everyone time.

When we know what a user is trying to do we can frequently offer methods superior to what they're attempting (if and when we have a deeper understanding of the capabilities of Keyboard Maestro).

1 Like

Point taken, Chris…

1 Like

A slight question with your macro, Chris:

In a case where the source file name and the destination file name are the same except the case varies in a letter or two between the source and destination, I’m finding the case doesn’t change in the destination. In other words, it almost seems to ignore case differences. For instance if the source is

16 Old Granddaddy.wav and the destination is
16 Old granddaddy.wav the destination does not change case.

In fact this second file with the small "g" is a copy after the macro was run. Is there a check of some type that says not to paste if the file name is the same, ignoring case differences? Or would that possibly be a Mac OS autocorrect feature? It seems pretty consistent when all else is the same. If there are any changes other than the case in the file name, then it pastes the name exactly, with the correct case.

Tiny issue and easy to spot and correct in any case.

Thanks and I’m really enjoying the macro!

Tom