Moving All Sub-Contents of Parent Folder Into Top-Level

Hi all,

I've been trying to accomplish this with a number of tools, but cannot seem to grasp the KM logic here and I'm pretty sure Hazel cannot do what I am looking for.

My goal is to have a kind of Sorter folder. The folders dropped into this "sorter" will sometimes have many subfolders each containing a mix of movie, photo, and text files (sometimes other files as well). When another folder is dropped into this sorter, let's call it Folder A, I would like all of its contents and sub-contents to be moved to the top level and then I would only like to keep the photo and video files. In other words, I would like Folder A to have only videos and photos when I open it it—with all other file types and empty folders removed.

Here is a photo for reference of what the folder structure might look like when I drop folder A into the "sorter" folder.
Screenshot 20210810 at 07.34

If for example, there were movie files in the those subfolders, I would want them moved into Folder A's top level like this:

Screenshot 20210810 at 07.46

I'm wondering how to best make a KM script that will go into the subfolders and essentially move everything up to the top level. I want to ensure that all those single files that move up are contained with the top level of Folder A and do not move into the top-level of the Sorter folder (if that makes sense). I was not able to do this with hazel and have tried and failed miserably with KM.

Any help is much appreciated!

You can use the For Each action with the Folder Contents collection to scan through a folder and its contents.

So you could scan Folder A recursively, and then for each item you find:

  • If it is a folder, ignore it for now
  • If it is a file, then:
    • if it is a movie or photo, move it to Folder A if it is not already in Folder A
    • Otherwise, trash it

Then rescan the Folder A, and trash any folders.

You can use the Get File Attribute action to get the extension or content type to determine if the file is a photo or image.

You can use the Get File Attribute action to get the parent path to determine if the file is already in Folder A directly.

You will have issues if the file already exists with the same name in Folder A, which is non-trivial to deal with.

But make sure you have complete and current backups before trying any of this, as one small mistake and it could destroy large amounts of your data.

Thank you for this reply. I am clearly a novice and stuck, but willing to learn and fail.

I cannot seem to get the final destination correct. I want the individual files moved up into their original parent folder (e.g. Folder A), but I do not want those files to be moved into the directory that houses Folder A - if that makes sense. I am continuously getting an error with my file path.

I don't think I was explaining myself clearly either. My ultimate goal is to have a monitored folder. When Folder A is dropped into this monitoring folder, all the files in folder A's subfolders (photos and movies) will move into Folder A. The rest will be deleted.

I really can't seem to figure this one out. I got close and was able to get the files to move into the Monitoring folder ("Test3" on my drive) but can't figure out how to get the file path of the folder that is one below the parent path...

Here is what I have so far. I'm sure there plenty of errors here. The parts highlighted in red is what I can't really figure out now. I also do not know how to address your comment:
"You will have issues if the file already exists with the same name in Folder A, which is non-trivial to deal with.
"You will have issues if the file already exists with the same name in Folder A, which is non-trivial to deal with."

So stuck for the past few days. Again, any help from is much appreciated.

Move Files to Parent.kmmacros (4.2 KB)

Hey @Slugworth,

Take note of this action:


It's more efficient than using separate file attributes.

Create a dummy test folder with empty text files renamed with your desired file extensions and in an appropriate hierarchy.

Zip the folder and upload it here, so we have something to test with.




That is a very good idea. Thank you!

Ok here is a zip file of possible messy folder structure. My goal would be to drag and drop Folder A and B in the "Sorter" folder which would then trigger a macro that brings all video and photo files to the Folder A and Folder B levels, and the rest would potentially be deleted.

But I'm just trying to wrap my head around how to tell KM to move files to the Folder A and B levels and not the Sorter folder level.

These are text files with their extensions changed! (29.2 KB)

cc: @peternlewis
I made some progress (I think).

I'm attaching the macro here that does partly what I need it to do. It scans the folder that gets dropped into the "Sorter" and moves all the photo and movie files into that folder. I did some cut and paste style stuff with the variables. If there are easier functions I am missing, feel free to educate me :laughing:.

Move to Parent Test copy.kmmacros (4.9 KB)

What I cannot get to work is the trashing of everything else. For example I just want to trash all other folders/files that are not movies or photos. When I add the trash action (in red)...

Screenshot 20210819 at 08.36

I do not get the intended result of having all files that do not meet the above criteria trashed. I think there is a problem in my understand of how that action works or how my variables are set up. Can someone educate me on this?

Thank you!

Assuming the FolderItems variable contains the full path of the file you are checking, that looks ok.

Check the Engine.log for any reported errors, and log the value of %Variable%FolderItems% to verify it is as you expect.