Count file name occurrences in a directory

I scan in a large amount of documents and save them in a folder. Each document needs a unique name but may do so as a repetition number. As an example, if I have 3 documents to scan and I want the name to be ‘Big Store’ I want the file names to be:

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

Upon scanning I have Keyboard Maestro open a dialog and ask me the name of the file (vendor) and I set the path within Keyboard Maestro (kmPath). I want to enter in ‘Big Store’ and then have the macro continue to save the document and if there is already a file named ‘Big Store’ it would add the #[repetition number].

I currently have an Applescript that Keyboard Maestro runs during this process to count the number of ‘Big Store’ in the folder. This works great if the name of the file is one word like ’BigStore’ however, if there is a space in the name such as ‘Big Store’, Applescript returns the wrong file name count.

Here is the Applescript that interacts with my Keyboard Maestro macro:

tell application "Keyboard Maestro Engine"
set myVar to make variable with properties {name:“Vendor”}
set vendor to value of myVar
set myVar to make variable with properties {name:“kmPath”}
set folderPath to value of myVar
end tell

set theFolder to quoted form of folderPath

try
set vendorList to every paragraph of (do shell script "cd " & theFolder & “;” & "ls " & vendor & “*”)
on error
return {}
end try

set vendorCount to (count of vendorList) + 1
– adding 1 so Keyboard Maestro uses this next value

tell application "Keyboard Maestro Engine"
make variable with properties {name:“vendorCount”, value:vendorCount}
end tell

I am not an Applescript expert so the code I have above was the culmination of many hours trying to find Applescript examples online and making a hybrid of the items.

Is there a way to have Keyboard Maestro count the occurrences of a file in a path, rather than have Applescript do it? If not, does anyone know how to perfect the Applescript so it works with file names containing a space?

Well, you could do something like this in Keyboard Maestro:

 Set Variable Count to calculation 0
 Get File Attribute Parent Path of %Variable%Destination Path% to variable Parent Path
 For Each Path in files in directory %Parent Path%
    If variable Path contains %Variable%New Name%
        Set variable Count to calculation Count + 1

Something like that would count the number of matching files.

Note: If there is any chance that the New Name would match the parent path folder path, then you might want to use Get File Attribute Name on Path and test just that in the condition.

Thanks Peter. That put me on the right track without using Applescript!

I have created a post in the Macros area of this forum where others can see and download the macro I created.

. . . Stephen

1 Like

Hey Stephen,

Please don't spend many hours doing something like this unless you're having fun. That's what the Applescript Users List and Macscripter.net are for.

It took me about 5 minutes to work through this problem.

Using a uniform file name convention makes things easier.

Big Store 01.pdf
Big Store 02.pdf
Big Store 03.pdf

* Note that although the script defines baseFileName as "Big Store" by default the property will retain whatever you input in the dialog.

The script should be run as a script FILE, so the property baseFileName is retained.

property baseFileName : "Big Store"
set targetDir to alias "Ryoko:Users:chris:test_directory:Stephen:"

set baseFileName to text returned of (display dialog "Enter Base File Name:" default answer baseFileName)
set AppleScript's text item delimiters to {" ", "."}
tell application "Finder"
  try
    set fileName to name of last file of targetDir
  on error
    set newFileNum to baseFileName & space & "01" & ".pdf"
    return newFileNum
  end try
end tell
set fileNum to text item -2 of fileName
set newFileName to baseFileName & space & text -2 thru -1 of ("0" & (fileNum + 1)) & ".pdf"

set the clipboard to newFileName

You can of course save the result to a KM variable instead of the clipboard.

The advantage of using AppleScript for this job is that it is very easy to get the last file in a folder and parse it's name for a number to increment.

You could even keep more than one job in your output folder by doing something like this:

set fileName to name of last file of targetDir whose name contains baseFileName

-Chris

Thanks Chris for the code and the reference to resources!

I will give this a try.

. . . Stephen

Hey Stephen,

Actually you can get the file count quite a bit more easily.

property baseFileName : "Big Store"
set targetDir to alias "Ryoko:Users:chris:test_directory:Stephen:"
tell application "Finder"
  set fileCount to count of (files in folder targetDir whose name contains baseFileName)
end tell

I was thinking inside the box... :smile:

MacScripter.net:
http://macscripter.net

Applescript Users List Join
https://lists.apple.com/mailman/listinfo/applescript-users

Applescript Users List Archive
http://lists.apple.com/archives/applescript-users

You have to use Google to search the archive these days.

site:http://lists.apple.com/archives/applescript-users

-Chris

Thanks again. It is great getting several suggestions. I will kick the tire with this as well.

. . . Stephen