Problem when writing filenames to text file ...

Hiya,

I’m struggling with a script to take the filenames of files in a folder, and write them to a text file. The files are named File1, File2, File3, etc, but the resulting list that Applescript produces it this way: File1, File10, File11 … File19, File2, File20, etc.

Is there any way to force Applescript to read the file names and list them numerically? Here’s the basics of the code:

set theItems to every file of theFolder
	repeat with i in theItems
		set theFilename to name of i
		set k to open for access file (i as text) with write permission
		set eof of k to 0
		
		write "<img src=\"images/" & theFilename & ".jpg" & "\" alt=\"" & theFilename & "\"" & "/>" & return to k starting at eof
end repeat


Cheers,

Kev.

Hello.

This works for me, at least when the numbers are at the end of the filenames. A little bit slow maybe, but fast to implement.


considering numeric strings
	set mf to POSIX file (do shell script "ls -1d ~/Library/Caches/TemporaryItems/junk/") as alias as text
	set L to {}
	tell application "Finder"
		set theList to (sort every file of folder mf by name)
		repeat with af in theList
			set end of L to name of af
		end repeat
	end tell
end considering
L

Brilliant! Thank you!

I just needed to add the following line before writing the filenames to my text file:

set theItems to (sort every file of folder theFolder by name)

Cheers,

kev.

Hello.

At least eariler you would need to have the considering numeric strings clause, I’d add that if I were you, just to be on the safe side. :wink:

Hiya,

Okay, will do. The script seems to run fine without the numeric strings, but I’ve added it anyway.

Cheers again,

Kev.