How Do I Write a Macro to Delete folders and files within a given folder?

Hi - I was hoping for some help in learning how to create a KM macro to automate deleting folders and the files within. The screenshot shows an example of my folder structure.

  1. The main folder "Example" is on my desktop.
  2. Within will be a series of folders that I need to repeat the operation on.
  3. I would select a subfolder with "Example", in this case "2008_08_27 - Movie_example_1", and then ideally activate the macro.
  4. The macro would then delete all the folders that start with "iMovie*", including if the file contains a folder called "iMovie data".
  5. BONUS: I would then activate an app called EXIFRenamer and drag all the clips remaining in the folder there to be renamed".

Appreciate any help that can be provided to this newbie!

Hi, welcome to the forum
Please look at

and then google
"Keyboard Maestro Forum " with file move, then rename, then create and I think you will find plenty of examples.

I suggest you create a small test directory and a backup copy ( so you can repopulate it easilly) for you tests and make sure you do not delete your hard drive by mistake....

Back that up first..... :slight_smile:

The trick to Keyboard Maestro is to treat it like lego and build what you want in small testable steps.

1 Like

Thanks for pointing me in a direction . . . I will read through this and see where it leads me!

While @jonathonl has pointed you to some resources to help build your macro, it occurred to me after thinking about your requirements that they are fairly complex and somewhat different from other file management macros that have been posted.

So, I have written a macro that largely does what you ask, but you will need to revise and extend as needed.

Please test this macro and let us know if it meets your needs.

Note that since you did NOT specify how you wanted to rename the "clip" files, I have just provided an example which you will need to change.

image
image

image

MACRO: Delete Files and Folders in Selected Folder; Then Rename Files [Example]

1 Like

Thank you for this as I have not used regular expressions yet and am not a regular programmer. I did have a few questions about the macro before testing it:

  • There is one action I believe called Execute text Script - I believe you have it labeled as "Select Finder Files with Criteria in Folder (AS)". Do I need to change anything in the first 4 rows? What is the function of this action?

  • Is there an action that could be added in to prompt for a different clip to be appended each time the macro is run?

Thanks again for taking the time!

No, you do not need to change anything.
Is there something you think you need to change?
Here's the AppleScript;

property ptyScriptName : "Select Finder Files with Criteria in Folder"
property ptyScriptVer : "1.0"
property ptyScriptDate : "2020-08-01"
property ptyScriptAuthor : "JMichaelTX"

set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
  set sourceFolderPath to getvariable "Local__SourceFolder" instance kmInst
  set fileStartsWithStr to getvariable "Local__FileStartsWith" instance kmInst
end tell

set sourceFolder to POSIX file sourceFolderPath as alias
tell application "Finder"
  select (files in sourceFolder whose name starts with fileStartsWithStr)
end tell

I'm not sure what your mean by "a different clip", but I have just updated the Macro that now provides all of the rename criteria in the Prompt. You can change the criteria in this Prompt:

If this does not meet your needs, please provide more details.
A detailed example with before and after would be very helpfu.