Update files from another folder

Hi All,

I've tried searching for this to no avail.

I have a folder which has a large number of files in it (approx 200 files) and I have another folder (updated versions of the files which contains the 200 files) however the newer folder has over 20k files. I only want/need to replace the 200 files with the updated ones.

Is there a way of making a macro that will find all the names of the files from the folder with 200 files in, then search the updated folder with 20k files in, and then copy/move/replace the older files with the newer ones please?

Thanks Jeff

A very simple "find" command in macOS should do the trick. Are the folders always the same names, or can they change?

1 Like

I haven't got the solution working yet, so don't try this command, but I'm pasting this command here in case anyone wants to fix it. This is getting close to the solution, but it's not working yet. It's close, so someone may find the mistake in it and fix it with little effort.

find ../folder1 -type f -maxdepth 1 -exec echo cp ~/folder2/`basename {}` ~/folder1 \;

In the above attempt, folder1 is the smaller folder, folder2 is the larger folder, and in this example you have to set your current working directory to folder2. Also, the echo command is just there to debug it. Once it works, the echo command will be removed.

What that command does is take all the names in the current folder and copy the files with those names from folder2 to folder1. That's what it's supposed to do, but it's not quite there yet.

I think I got it working, as follows:

find ../folder1 -type f -maxdepth 1 -exec sh -c 'cp ../folder2/"$(basename "$1")" ~/folder1' _ {} \;

However I have not carefully tested it yet. But you can test it if you want. You would have to replace the names of ~/folder1 and ~/folder2 with whatever your folder names are. Actually, for testing purposes, you should probably create ~/folder1 and ~/folder2 so that you don't accidentally overwrite any files.

1 Like

Thank you for getting back to me.

So the 2 folders have different names, but they will always remain those names.

thanks :slight_smile:

I just updated my previous post with a command that I think will work. I recommended that you create the same folders that I was testing it on (folder1 and folder2 in your home directory) to test that it's working correctly. Once you are sure it works, try it on your real folders.

If it works, it's easy to put this into a KM macro using the Execute Shell Script action.

1 Like

Have a play with this:

ls -1 testDest > /tmp/inc_list.txt;rsync -a --include-from /tmp/inc_list.txt --exclude '*' testSource/ testDest/

The first command writes a one-filename-per-line listing of the testDest directory to a file in tmp, the second command uses that file as an rsync inclusion list (with everything else explicitly excluded) to copy only files with matching names from testSource to testDest.

End result -- only files from testSource with names matching a file in testDest are copied to testDest. Which I think is what OP wants...

Should be easy enough to wrap that in a macro to make setting source and destination easier, etc.

1 Like

I''ve used many shell commands in my life, but I don't recall ever seeing rsync. Thanks.

Hi Cefasil753,

Thank you for your reply, I tried this out, but I got a debug error 'Set fso = CreateObject("Scripting.FileSystemObject")' on this line. I'm not familiar with VBA if I'm honest, do you understand what this means please?

p.s. I'm not sure if it makes a difference but I'm on Mac OS

Thanks Jeff

If a user's post has been removed, it usually means that the user was identified as a bot. In theory, a bot can write something useful, but I've never seen anything useful, so you should ignore bots for now.

The "KM way" would be to look at each file in your target folder and if there is a matching file in the source folder copy the source folder file to the target folder.

The wrinkle is that KM's "Copy a File" action doesn't overwrite an existing file -- you have to delete the original first. But you don't want to delete the original if there's no file to be copied! So we'd better check that as well...

This will let you choose a source (your 20k files folder) and a target (your 200 file folder) -- once you're happy you can replace the prompt actions with hard-coded file paths.

Copy Matching Files the KM Way.kmmacros (6.2 KB)

Image

I've taken you at your word -- this only copies files. If you want to include folders too you'll need to make some additions, since KM has separate "Delete"s for files and folders.

1 Like

rsync is the dog's whotsits for synchronising files/folders, either locally or with remote systems, using various criteria. And it gives you a whole bucketload of options -- well worth investigating.

The version that ships with macOS is very out of date (because of licensing) but you can install the latest with Homebrew.

1 Like

(Note: @Nige_S posted their reply while I was typing mine; sorry for the double dose of rsync advocacy!)

rsync is an amazing tool; you can do some really cool stuff with it. I use it in MacroBackerUpper, as one example. It's also incredibly flexible in that it can work both locally and remotely.

So if you have a server you connect to, and you set up passwordless ssh access, you can do this:

rsync /path/to/local/folder/ server.name:/path/to/folder

As it only copies changed files, it's incredibly speedy. I use a variation of this to make a local "Time Machine like" backup of all our web files:

https://robservatory.com/create-time-machine-like-backups-via-rsync/

One note: If you want to use rsync, I recommend using Homebrew to install a newer version. The bundled rsync (version 2.6.9) is the final version licensed on the Gnu General Public License v2 (Apple won't ship apps that use Gnu GPL v3), and as such, was last updated in 2006. The current version is 3.4.2, and is updated regularly.

-rob.

1 Like

lol I didn't realise it was a bot

Hi Nige_S,

Thank you very much for the macro. I've just tried it and it seems to have worked perfectly including replacing the files rather than adding them and having to delete them afterwards.

Thank you once again.
Jeff

Thank you everyone who took time to post :slight_smile:

I had a similar need and resolved it easily. While I am a near-rabid fan of KM it isn't always the best answer to specific situations. In my case, after many failed attempts and a lot of research, I used a very small, low-resource applet called SyncTime that allows me to easily sync two folders applying many conditions. While you can accomplish the same thing with KM it may be prudent to take a look at this purpose-built tool that does almost precisely what you need.

KM rocks for about a thousand other things I do every day but in this case SyncTime was a fast, simple and 100% effective tool to get it done.