How to numbering the file with same name?

Hey, folks!

This is my first time posting on this forum. I have a problem that I have a lot of image files named Datum_20201025 (format: prefix+date) in Devonthink. I want to write a script that can number them and make them look like this Datum_20201025_1, and the number 1 means it is the first file to be renamed on the day of 20201025. And I hope that every time I run this script, it can inherit the result of the previous run.

For example, I have three files named Datum_20201025, two of them are in folder A, and one is in folder B. I first select two of A and run the script. They become File_20201025_1 and File_20201025_2. Then I come to B and run the script, it becomes File_20201025_1, is there a way to make it become File_20201025_3?

The following script is what I imagined. I guess it may have some changes afterwards, so I did not write it very carefully, so it may have many errors :stuck_out_tongue: Hope to discuss with you!

set theDate to {"0101","0102",.....,"1230","1231"}
set theValue to {"1","1",......,"1","1"}
set theRecords to the selection
repeat with thisRecord in theRecords
	set theName to the name of thisRecord
	set theName_date to characters -4 thru -1 of theName
	repeat with i from 1 to the count of theDate
		if item i of theDate = (theName_date as text) then
			set theName_number to item i of theValue
			set theName to (theName & "_" & theName_number)
			set item i to item i + 1
		end if
	end repeat
end repeat

Why do you not use this…

Its scriptable in the way you could run, load and execute command.
the application itself could do exactly what you want.

And if you are changing thousands of files I believe is also faster.

Is there a reason why you most use AppleScript and only ??

Thank you for your recommendation, I have actually downloaded similar software before, and they are indeed very easy to use! As for the reason I want to use applescript, I have imported most of my files into Devonthink and established many links between files. If I export them, rename them and import them again, I will lose these links… :frowning: For these files, I Had to use Applescript to modify…Thank you again! It will be very useful if I switch to indexing external files in the future!

Try this:

tell application "Finder"
	-- get first file of selection's list
	set someFile to item 1 of (get selection)
	-- get all filenames of root folder to compare with them later
	set theFileNames to name of every file of entire contents of container of container of someFile
	repeat with theFile in (get selection)
		-- get base name of current file
		set Extension to name extension of theFile
		set posixPath to POSIX path of (theFile as alias)
		set TID to AppleScript's text item delimiters
		set AppleScript's text item delimiters to {"/"}
		if (posixPath ends with "/") then
			set baseName to text item -2 of posixPath
			set baseName to last text item of posixPath
		end if
		if (baseName contains ".") then
			set AppleScript's text item delimiters to {"."}
			set nameWithoutExtension to text 1 thru text item -2 of baseName
			set nameWithoutExtension to baseName
		end if
		set AppleScript's text item delimiters to TID
		-- get unique index
		set i to 1
			if (nameWithoutExtension & "_" & i & "." & Extension) is in theFileNames then
				set i to i + 1
				exit repeat
			end if
		end repeat
		-- rename the current file
		set name of theFile to (nameWithoutExtension & "_" & i & "." & Extension)
	end repeat
end tell

I read your message again and after your respond…

If you count the files in folder A you will know what the index will begin in folder B…
If you have 10 files in folder A the index of the count will be 10…

next repeat loop for folder B

repeat with i from (count the files of folder A + 1) to ((count the files of folder A) + (count the files in folder B))…

Ps. I have not try this… not sure if my logic is correct… :slight_smile:

Thanks a lot!! Just wanted to discuss ideas with you before, but didn’t expect that you write it directly!! I’ll try it~ Thank you for sharing~ :slight_smile:

You are right! Actually, I just want to give the file a unique name. :stuck_out_tongue:
I thought that AppleScript might have a module such as a counter which I might not know before, but now I find that it seems that it still needs to check the existing file before determining the start Value~ If this is the case, maybe random numbers might make the problem easier? Thank you both for your help! :slight_smile:

Simple example to demo my approach or the logic I see it.

set fileCountFolderA to 3 -- The count of files in folder A
set fileCountFolderB to 2 -- The count of files in folder B

repeat with i from 1 to fileCountFolderA
	display dialog i
end repeat

repeat with i from (fileCountFolderA + 1) to (fileCountFolderA + fileCountFolderB)
	display dialog i
end repeat

ASObjC could do this for you instead of using a do shell script.

use framework "Foundation"
use scripting additions

set uniqueIdentifier to current application's NSUUID's UUID()
set theString to uniqueIdentifier's UUIDString()
return (theString as text)