Move files to folders

I have some zip files in a folder called 'surveys' that I need to move to a specific folder. The file names vary, but the first part of the file name will always contain the name of the folder they need moving to.

For example, these are the names of the files in the folder 'surveys' at the moment. The first part of the file name is always the job name and number:

upload/surveys/Woburn 8364 WBS 11.12.24.zip
upload/surveys/Woburn Phase 2 8233 WBS 11.12.24.zip
upload/surveys/Church Lane 6353 WBS 11.12.24.zip

These files need moving to:

jobs/current sites/Woburn 8364
jobs/current sites/Woburn Phase 2 8233
jobs/current sites/Church Lane 6353

I can get as far as figuring out how to move all files to the same folder (set variable to folder path then 'for each'), but not each file to a specific job folder.

Anyone know how to do this please?

Is the sub-folder always "the name of the file minus everything from 'WBS' to the end"?

If so, look at the "Filter" action with the "Basename" filter to get the name, then a bit of "Search and Replace" using a regular expression to get rid of what you don't want. Then use the result to build your destination path.

Have a go, see how you get on, and shout if you get stuck!

This is as far as I have got. But the regular expression only picks out the first file in the folder. Also cannot work out how to move file to specific folder.

Move files to folders.kmmacros (3.9 KB)

Don't use the shell to get your listing -- the KM "For Each" action will let you work through all the files in a directory, one at a time. And it's inside the "For Each" that you do your file path processing.

Worth noting at this point that "Search Using Regular Expression" only ever matches the first occurrence, it isn't "global" like some other regex engines are (eg regex.com), which is why you are having a problem. You get round that by using it inside a "For Each" -- another reason to skip the shell listing...

And never be afraid to use two (or more) simple text-based actions instead of a complicated regex -- your future self will thank you when they troubleshoot the macro next year :wink: So I've used "Filter" to get the base name, then chopped off the characters from <space>WBS to the end.

I've assumed your source folder is always /path/to/Survey Shapes/upload/surveys and that there are no sub-folders, the destination is always /path/to/current sites/<folderName>, and that the destination <folderName> already exists...

The macro will spit out a "Display Text" showing the move for each file, for testing. If you're happy with the results then delete that and enable the "Move File" action. You should still test on a small handful of known files, so you can easily revert, before letting rip on a folder of thousands!

Move files to folders v2.kmmacros (5.5 KB)

Image