get list of folder items, rename them

Hi again and again :D!

My ultimate goal is to create a list in texedit of the items present in a specific folder and then rename all of these items in the order of the list. More specifically, let say that my folder (called “toto” on the dasktop) contains a.zip and b.zip, I would like to create a list of these (item name list) and then rename a.zip to 1.zip and b.zip to 2.zip. Could you help me? I am just to inexpericended for this…

thank you so much again and again !!!

PS : really helpfull site! I discovered applescript here few days ago and I wondered how I could have lived without till now. However, the more I learn and the more I realise I also could not leave without your site :-D!!!

Hi,

I assume that the text file (plain text) contains the file list with one file per line
The script asks for the text file, the target folder and an optional prefix (no prefix if left empty)

set fileList to paragraphs of (read (choose file with prompt "Choose file which contains the file list" without invisibles))
set theFolder to choose folder
set renameText to text returned of (display dialog "Please enter prefix" default answer "")
if renameText is not "" then set renameText to renameText & "_"

tell application "Finder" to set these_files to items of theFolder
-- 	set these_files to sort these_files by name
set renameIndex to 0
repeat with this_item in these_files
	set {name:fName, name extension:fExt} to info for this_item as alias
	if fName is in fileList then
		set renameIndex to renameIndex + 1
		if fExt is missing value then
			set fExt to ""
		else
			set fName to text 1 thru ((count fName) - (count fExt) - 1) of fName
		end if
		set newName to renameText & (renameIndex as Unicode text) & "." & fExt
		tell application "Finder" to set name of this_item to newName
	end if
end repeat

thanks again stefank. Actually, in between, I found some similar posts and came up with the following script mixing your suggestion and some scripts found on this site. I would like the process to be automatic, not asking me anything but the folder where the changes should be made. The latest version of my script is working but only for the first file in my folder… it does not change the next ones. I figure I just need to specify my third line better but I do not know how…

thanks a lot !

PS : I would like to keep the q value as it could be equal to more than 0 (e.g. 80990) depending of the cases.

set q to 0
	set f to (choose folder)
	tell application "Finder" to set files_ to f
	
	
	repeat with file_ in files_
		set q to q + 1
		tell application "Finder"
			set old_name to name of file_
			set new_name to (q as Unicode text) & ".rar"
			
			try
				tell application "Finder" to set name of file_ to new_name
			on error e
				display dialog e
			end try
		end tell
	end repeat

f is the chosen folder, but you want to process its files

tell application "Finder" to set files_ to (get files of f)

just one word : perfect, brilliant, fantasic, marvellous ! I know there are more than one word but they are all deserved !!!