Count file name repetition

If you have the need to count how many files in a folder have something in their name, or like to save files with the same name but append a number after them, this macro will help.

As an example, if you have a macro that will save a file called 'Big Store.pdf' to a folder, but if one or more files with that name already exist, this macro can be run first to find out how may files exist with a specified name and then append the returned number after it.

Big Store.pdf
Big Store 2.pdf
Big Store 3.pdf

I use it by having a primary macro set a variable (DestinationPath) to the full path of the location I am saving the file to and then execute this macro. This macro then returns the results by setting a fileCount variable. If the fileCount is is zero I know the file name is unique. If it is greater then zero, I append the returned number (fileCount) + 1 to the end of the file name.

Here is the Macro you can download and import:
File Name Repetition Count.kmmacros (5.6 KB)

1 Like

I see the use of Repeat 1 Time as a grouping mechanism.

You might want to use Get Base Name rather than Get File Name - that will get the name without the extension. Otherwise you will find “test.pdf” and “test.pdf #1”, but you will not find “test #1.pdf”. Although then you will also find “test.jpg”, so you may want to also only count paths ending with the same extension.

1 Like