partial rename of files in the finder

We have large pdf-files we split (using applescript and FullSwitch) into single pages to certain places on the network, the only problem is that the numbering of the files should be at least 2 digits.

Example:
filename “document_1.pdf” should become “document_001.pdf”

I would love to look for it myself, sorry enough the complexety of (scripting) all those different folders to watch and moving the files around is absorving huge amounts of my time… :confused:

Try something like this:


set myList to {"document_10.pdf", "document_1.pdf", "document_137.pdf"}
set tid to AppleScript's text item delimiters
repeat with N in myList
	set AppleScript's text item delimiters to "_"
	set Part_1 to text item 1 of N
	set Part_2 to text item 2 of N
	set AppleScript's text item delimiters to "."
	set Mid to text item 1 of Part_2
	set tEnd to text item 2 of Part_2
	set AppleScript's text item delimiters to tid
	set contents of N to Part_1 & "_" & pad(Mid, 3) & "." & tEnd
end repeat

myList --> {"document_010.pdf", "document_001.pdf", "document_137.pdf"}

on pad(num, howLong)
	set c to count num
	repeat howLong - c times
		set num to "0" & num
	end repeat
	return num
end pad

Note: I shortened “Pad” above, removing conversions to text since it is already text.