How to move all files in a specific folder and replace

Hello,
I am unable to find the actions which would allow me to:

  • move all files in a folder (for example the scansnap folder)
  • into another folder (for example documents folder)
  • replace all files in the destination.
    thank you

You would need to use these two Actions in a loop, such as the For Each action with Folder Contents Collection

image

Note that the Trash File Action has NOT enabled the abort on failure, so that if the file does not exist the macro just continues:

image

So, putting this all together, it would look something like this:

image

Be sure to test carefully using test folders/files before using in production.

2 Likes

thank you VERY MUCH for the detailed response.
I will work on trying to understand your reply.
Would allowing for file versioning instead of replace make things easier ?
I am perplexed at how complicated it is. I thought that the answer would be simple and straightforward.
Would the following command line (Terminal) commands do the job assuming I want to move files from Downloads to Scansnap? Could you just add a flag to indicate replace files or allow for file versioning in the destination?
mv ~/Downloads/. ~/Scansnap
rm ~/Downloads/.
I could not find a KBM action to run command line or terminal commands.
thanks again very much for your time and help

See Execute a Shell Script action (KM Wiki)

It doesn’t seem complex to me. A loop with 3 statements. <shrug>

I’m sure there are bash commands that will do this. Just do some googling and I’m sure you find them.

This could also be done easily in AppleScript, but it would be about the same number of statements as the KM actions.

1 Like

thank you very much

Hey @ronald

This largely mindset. If you’re used to QuicKeys then you’re accustomed to being spoon-fed quite a few functions, and it can be a shock when Keyboard Maestro hands you the spoon and says “Feed yourself!”

Here’s a kinder, gentler method using AppleScript:

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/03/11 22:10
# dMod: 2018/03/11 22:14 
# Appl: Finder
# Task: Move Files from one folder to another and replace any collisions.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Move, @Files,@Folder, @Replace, @Collisions
----------------------------------------------------------------

set sourceFolder to alias "Mercury:Users:chris:test_directory:Test_Move_Files_Source:"
set destinationFolder to alias "Mercury:Users:chris:Downloads:Test_Move_Files_Destination:"

tell application "Finder"
   set filesToMoveList to files of sourceFolder as alias list
   move filesToMoveList to destinationFolder with replacing
end tell

----------------------------------------------------------------

The same thing can be accomplished via the shell like so:

src='/Users/chris/test_directory/Test_Move_Files_Source/'
dest='/Users/chris/Downloads/Test_Move_Files_Destination/'
cd "$src"
mv -f * "$dest"

Keyboard Maestro’s move action will NOT overwrite an existing item, but Bash will when the -f (force) switch is used.

The above can be written as a one-liner like so:

mv -fv '/Users/chris/test_directory/Test_Move_Files_Source/'*  '/Users/chris/Downloads/Test_Move_Files_Destination/'

However – it is easier to make a potentially dangerous mistake when using this form.

When writing shell scripts that can overwrite something it is essential to use great caution, because a small syntax mistake could erase your whole user-directory.

-Chris

1 Like

Thanks very much.
I will work on decrypting the Apple Script.
The spoon feeding metaphor is excellent !

1 Like

I have just uploaded a complete macro for transferring (moving) all files from one folder to another, which provides:

  1. Selection of Source and Destination Folders
  2. Folders can have default selection based on selection in Finder
  3. Prompt to Confirm continue of macro, showing folders
  4. Files with same name in Destination folder will be overwritten, but sent to macOS Trash.
  5. Log of Results

See:

MACRO: Transfer Files Between Folders [Example]

I think you will find this a “kinder, gentler,” and safer method using mostly native KM Actions.

Please feel free to post in this macro topic with any questions/issues/suggestions.

2 Likes

thanks very much !

1 Like