I have a series of files I need to copy into folders.
The file names will always start with a number (i.e. 2015_071, 2015_072).
The files will always be consistently named at their start in this way.
The folders were I am copying to will also start with the same number but have a ‘second’ part which is different (i.e. ‘2015_071 (Adam)’, ‘2015_072 (Brown)’).
Is it possible to copy files to a folder where you only ‘know’ part of the name of a folder? That is what functions/actions I need to use and how to modify them. Rather than detailed code I would like to know whether it is possible so I can investigate myself.
I think the only mapping you will be able to get is:
strStartOfName --> [ foldersAvailable ] --> folderBestMatch
Whether you then go ahead and actually copy to your best matching folder, depends, I suppose, on the level of confidence.
Essentially, it’s going to be a search through a list.
There are a couple ways. Probably the easiest would be to avail yourself of the shell script’s ability to pattern match and do something like this:
Execute Shell Script: echo "/path/to/directory/2015_071"*
Assuming there is only one match, you’ll get only one line back in response, and that will be your path which you can use in the Move or Rename File action.
Alternatively, you can actually do the move in the shell like this:
Execute Shell Script: mv "$KMVAR_Path" "/path/to/directory/2015_071"*
And finally, if you prefer to stay away from the shell expansion, you can do something like this in Keyboard Maestro actions:
For Each file Path in Folder Contents "/path/to/directory"
if variable Path contains "/2015_071"
Exit from Loop
Then, assuming the Path matches exactly once, you’ll finish with Path being the path to the folder.
In both cases, if the path matches more than once, or does not match, you’re going to be in trouble, so you may want to check for that and report the problem if that is the case.
and stepping back a bit, I wonder if there is any scope for adjusting your folder naming scheme ?
Might let you:
- Find a definite 2015_NNN folder,
- copy to the [ Adam | Brown | etc ] subfolder if found, and
- do something simple and sensible if not found.
Thank you for very much for the alternative suggestions.
I am new to the For Each action etc and cannot interpret what you have written to what to put in the various boxes. Could you provide an example?