Renaming files in folder problem

Hi,

I need to rename all the files in a folder. The problem is, they have to be renamed as follows:

a file named “SF02D004.pdf” should be renamed to “40204.pdf”
and a file “SF12E123.pdf” to “1231203.pdf”

the source file name consists of “SF”+issue number+“E”+pageNumber+“.pdf
but I need pageNumber(without leading zeros)+issueNumber+year(2digits that are given as a variable in the script)+”.pdf"

so the script should do the following
-remove the “SF”, the issue number, and the following Letter (thats always “E” or “D”)
-make a variable consisting of the issueNumber and the year variable
-replace the “.pdf” with this variable+“.pdf”
-remove leading Zeroes in the Page Number

Can anyone tell me, how this can be done. I’m quite new to Applescripting, so please explain it for beginners :slight_smile:

I would suggest trying soemthing and then when you run into problems ask for help. (ie, post non-working code)

Hints:

  • a filename is a string
  • a string is a list of characters
  • conversion between strings and numbers is nearly automatic
  • the first step would be to pull all of the information from the filename, the second to build the new filename and the third to finally rename the file.

Well, I may have to clarify my problem.
I know how to do this in other languages, my main question was, if Applescript offers something like substr() and especially strstr() as I didn’t find any commands like that though I looked up quite a few sites and tutorials.

Your problem was clear – and PCS is right, it makes it much more palatable to help folks who demonstrate that they’ve actually tried to help themselves. In vanilla AppleScript, there is no substring command but you can get close to it by using “text 3 thru 5 of x” or using text item delimiters. There are countless tutorials on the web (and on this BBS) that show you how to do this. That said, this is a very easy script. The following code is a handler you can call to clean the names to your specifications (although you were a bit contradictory in your examples, one name had a “D” and the other “E”. Since you said “E” was the delimiter, this code goes with that. A “D” will not work but the code could be modified if needed). This also does not include any code for listing files in folders, iterating through that list extracting names and renaming files. For that, you’re on your own:

set the_year to "03"
set the_name to "SF12E123.pdf"
return clean_name(the_name, the_year)

on clean_name(the_name, the_year)
	try
		set OLD_delim to AppleScript's text item delimiters
		my atid(".")
		set {the_name, the_extension} to text items of the_name
		my atid("E")
		set {the_issue, the_page} to text items of the_name
		my atid(OLD_delim)
		set the_issue to text 3 thru -1 of the_issue
		return {(the_page as integer), the_issue, the_year, ".", the_extension} as string
	on error the_error
		my atid(OLD_delim)
		return the_error
	end try
end clean_name

on atid(the_delim)
	set AppleScript's text item delimiters to the_delim
end atid

Jon

Thank you very much for the script.

I am sorry if my request offended anyone. I just needed such a script to further automate my file archive.
I didn’t know if AppleScript provided the necessary means for the task, and as I didn’t find anything on several sites I posted here to find out IF it is possible.

I didn’t exspect someone to write me the script (though I do appreciate it very much that you did) a hint at the “thru”-command would have been enough (that was the part I was missing).

So, thanks again.