Copying Files From Multiple Subfolders That May Have Same Filenames to a Single Folder

OK, so let's say I have a bunch of PNGs that could be an level deep within a directory. I want to find all those images and copy them to a new directory, but once they are in the same directory, there will be filename issues. As in:

  • Dir1
    • SubDir1
      -File.png
    • SubDir2
      -File.png
    • SubDir3
      -File.png
    • SubDir4
      -File.png
  • Dir2
    • SubDir1
      -File.png
    • SubDir2
      -File.png
    • SubDir3
      -File.png
    • SubDir4
      -File.png
  • Dir3
    • SubDir1
      -File.png
    • SubDir2
      -File.png
    • SubDir3
      -File.png
    • SubDir4
      -File.png

That's 12 files called File.png

I need to copy them, but I can't rename the original file, so how could I go about getting all these files, copying (and renaming - just appending an ID will be fine) into a new directory? Does anyone have a macro floating around that could do such a thing? Or close enough that I could amend it to suit my needs :slight_smile:

Thank you

Here is an untested idea, but based on similar workflows:

  1. Get list of paths of all files you want to move
    • Since you need to search subfolders, I would probably use the Bash find command.
    • Of course, you will need to know the search criteria. Is it all ".png" files, or something else
    • For a good example to get you started, see MACRO: Files Move Files with Ext in Folder [Example]
      • You will need to change, or remove, the maxdepth parameter to search all sub-folders, or limit to a specific depth.
      • image
  2. Use the KM Move and Rename File Action to move to your destination folder and rename it, add some sequential ID to each file.

Hopefully that will get you started. Try to develop your macro, and if you get stuck reply back with the specific question or issue you have (be sure to upload your macro. See How to upload your macro. Instructions for uploading to an existing post are just below the main instruction.

Each file has, of course, a unique identifier in the form of its full path, and you could obtain a distinct file name for each by:

  • starting with its full path,
  • replacing the directory-dividing slashes with something like a hyphen,
  • and then perhaps discarding some part from the left if you don't need the entire pathname to keep files distinct in the new target folder.

distinct file name from full path.kmmacros (19.7 KB)

1 Like

Well, I gave you an outline above, but since I'm stuck at home in Houston in sub-freezing temps, I had some time so I went ahead and built an example Macro for you.

You will need to test and modify or use to fit your specific workflow. But it should get you started.

Let us know if this works for you.

Example Input -- Source Folder

Example Output -- Destination Folder

Results

MACRO: Move & Rename Files with Ext in Folder Using Bash Find [Example] Macro

Move & Rename Files with Ext in Folder Using Bash Find [Example].kmmacros (24 KB)

Well, I get your current situation as I have a colleague in TX (Dallas) who gave up all hope of doing proper work yesterday. No power, no heating, no fun!

Thank you so much for this. I have made some slight adaptations to it and it's almost perfect, but there is just one tweak I'd like to make but am not sure if it's possible.

If you look at the results panel where you tested it on three files, note that the results are processed in the order SubFolder 1,then 3, then 2. I would like it to process it based on the order of those Subfolders. Instead of using a date stamp to make the resulting filenames unique, I had a variable that I increment by 1 each time.

However, the resulting files, e.g. file1.png, file2.png, file3.png actually relate to the files that wear in 1, 3, then 2.

Is there some kind of command that I can add somewhere to make sure that it processes in the order that the directory and subdirectories would appear in Finder?

Once again, thank you so much for your hep with this. Seeing how much is possible with KM makes me realise that I should try to really learn this in a structured way (any recommendations about that also welcome).

Thanks for this suggestion too. It has potential, but I would opt for appending an ID or similar, one because I know that the parent folder(s) will be quite long, which would result in some very long filenames in the final versions. But I'm going to store this one away for later use for something else :slight_smile:

It appears that the Bash find command finds the files in the order the folders were created. As long as you always end each subfolder with digit(s), then this is easily solved by sorting the output of the find command. I have added that in my below update.

The reason I used the date/time stamp, rather than a seq number, is that if you later have the same subfolders that you wish to process again, then you will need to keep track of the seq number you use. This can be done using a KM Global Variable, but it seemed simpler to use the date/time stamp.

Having said that, I have changed the Macro to use the last digit(s) of the subfolder as the seq number. This will identify which subfolder it comes from, but does NOT deal with the above issue of future duplicates. If it were me, I'd do both: Seq number from the subfolder AND a date/time stamp. Your choice, of course.

So here's the update that addresses both of your issues:

  1. Process in order of subfolder number
  2. Use seq number from subfolder number

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Move & Rename Files with Ext in Folder Using Bash Find [Example]

-~~~ VER: 3.1    2021-02-16 ~~~
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:

Move & Rename Files with Ext in Folder Using Bash Find [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Files Move & Rename Files with Ext in Folder Using Bash Find [Example]
    • Can optionally include sub-folders

REF: Copying files from multiple subfolders that may have same filenames to a single folder

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
    • Including setting of Source and Destination Folders.
  2. Trigger this macro.

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. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • Set Source Folder
      • You can easily replace this with a KM Choose Folder Action
    • Set Destination Folder
    • PROMPT for File Extension to Search For

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Bash @Find @ShellScript @Files @Move @Rename @Example

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

==USE AT YOUR OWN RISK==

  • While I have given this a modest amount of 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.

OK, I've got a 3rd version for you that does exactly what you want: FileSeq is calculated by incrementing a Global Variable for each file that is found.
To provide others with an example of the various methods for calculating FileSeq, I have left the other method in place, but disabled the Actions.

image

Example Output

image

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

MACRO:   Move & Rename Files with Ext in Folder Using Bash Find [Example]

-~~~ VER: 3.2    2021-02-16 ~~~
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:

Move & Rename Files with Ext in Folder Using Bash Find [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


ReleaseNotes

Author.@JMichaelTX

PURPOSE:

  • Files Move & Rename Files with Ext in Folder Using Bash Find [Example]
    • Can optionally include sub-folders

REF: Copying files from multiple subfolders that may have same filenames to a single folder

HOW TO USE

  1. First, make sure you have followed instructions in the Macro Setup below.
    • Including setting of Source and Destination Folders.
  2. Trigger this macro.

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. ??
      .
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    .
  • REVIEW/CHANGE THE FOLLOWING MACRO ACTIONS:
    (all shown in the magenta color)
    • Set Source Folder
      • You can easily replace this with a KM Choose Folder Action
    • Set Destination Folder
    • PROMPT for File Extension to Search For

REQUIRES:

  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @Bash @Find @ShellScript @Files @Move @Rename @Example

USER SETTINGS:

  • Any Action in magenta color is designed to be changed by end-user

==USE AT YOUR OWN RISK==

  • While I have given this a modest amount of 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.

Once again, thanks for the very thorough response - I definitely learn heaps from these!

1 Like