For Each Item, The files in directory and error message

I have a collection of files in a folder, with some of the files contained in subfolders, eg:

I wished to search and replace various things from the file names and used a For Each Item, the files in directory action:

In order for the macro to go into the sub-folder I had to tick the Recursive option.

This macro works fine (that is it searches all the files and replaces what it should) , but I receive an Action Failed notification with a set of error messages are generated:

2016-10-31 00:41:27 Execute macro ‘Rename file names in finder selection copy’ from trigger Duplicate Macro Palette
2016-10-31 00:41:27 Move file /Users/victor/Downloads/pdfs/file failed with Error Domain=NSCocoaErrorDomain Code=512 "“file” couldn’t be moved to “file”." UserInfo={NSSourceFilePathErrorKey=/Users/victor/Downloads/pdfs/file, NSUserStringVariant=(
Move
), NSDestinationFilePath=/Users/victor/Downloads/pdfs/file/file, NSFilePath=/Users/victor/Downloads/pdfs/file, NSUnderlyingError=0x60800224eee0 {Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"}}
2016-10-31 00:41:27 Move file /Users/victor/Downloads/pdfs/file failed with Error Domain=NSCocoaErrorDomain Code=512 "“file” couldn’t be moved to “file”." UserInfo={NSSourceFilePathErrorKey=/Users/victor/Downloads/pdfs/file, NSUserStringVariant=(
Move
), NSDestinationFilePath=/Users/victor/Downloads/pdfs/file/file, NSFilePath=/Users/victor/Downloads/pdfs/file, NSUnderlyingError=0x60800224eee0 {Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"}} In macro ‘Rename file names in finder selection copy’ (while executing Move or Rename File ‘%Variable%Path%’).
2016-10-31 00:41:27 Move file /Users/victor/Downloads/pdfs/file failed with Error Domain=NSCocoaErrorDomain Code=512 "“file” couldn’t be moved to “file”." UserInfo={NSSourceFilePathErrorKey=/Users/victor/Downloads/pdfs/file, NSUserStringVariant=(
Move
), NSDestinationFilePath=/Users/victor/Downloads/pdfs/file/file, NSFilePath=/Users/victor/Downloads/pdfs/file, NSUnderlyingError=0x60800224eee0 {Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"}}
2016-10-31 00:41:27 Move file /Users/victor/Downloads/pdfs/file failed with Error Domain=NSCocoaErrorDomain Code=512 "“file” couldn’t be moved to “file”." UserInfo={NSSourceFilePathErrorKey=/Users/victor/Downloads/pdfs/file, NSUserStringVariant=(
Move
), NSDestinationFilePath=/Users/victor/Downloads/pdfs/file/file, NSFilePath=/Users/victor/Downloads/pdfs/file, NSUnderlyingError=0x60800224eee0 {Error Domain=NSPOSIXErrorDomain Code=22 "Invalid argument"}} In macro ‘Rename file names in finder selection copy’ (while executing Move or Rename File ‘%Variable%Path%’).

What do I need to do to avoid these messages?

[The above is just an example as I try to learn how to use For Each properly.]

It’s because the macro is somehow trying to copy the folder /Users/victor/Downloads/pdf/file into itself.

For debugging add a Display Text action at the end and disable the move action. Like this:

Then you see the contents of the variables.

It’s also visible in the log:

NSDestinationFilePath=/Users/victor/Downloads/pdfs/file/file, 
NSFilePath=/Users/victor/Downloads/pdfs/file

To exclude the folder from the actions you should test if the file in question is a file or a folder:

PS:

As a quick-and-dirty solution you could also just disbable the Failure options for the Move action:

I’m not sure ATM if this behavior always was like that:

  • In the For Each Item in a Collection action a folder is treated as a file.
  • However, in the If All Conditions Met action KM distinguishes between files and folder.

At least, the terminology seems inconsistent.

Hey Victor,

In general it's a good idea to make certain you're working on a file and not a folder (unless renaming folders is part of the job of course).

It's also a good idea to NOT rename the item unless the find/replace actually made a change.

Here's one way to filter out folders:

Generic-Test 01.kmmacros (3.9 KB)

I like doing it this way, because I get a path-list to work from – and that is easy to visualize in a Display Text action or a text editor for debugging.

Then I'll move on to a For Each Item in a Collection action with Lines in a Variable to operate on the actual files.

-Chris