Re: file name mutations
Re: file name mutations
- Subject: Re: file name mutations
- From: Ric Phillips <email@hidden>
- Date: Mon, 17 Sep 2001 14:26:28 +1000
On 17/9/01 11:18 AM, "Andrew Simpson" <email@hidden>
wrote:
>
can somebody tell me how i would do the following...
>
>
check for file names with the following name convention:
>
>
file name = nz1711jr-11028812.eps
>
>
sometext-six digit number plus incrementer.
>
>
and convert the file name to the following:
>
>
file name = 11028812.eps
>
>
ie chop the front off to the hyphen.
>
>
i'm sure its quite easy to those in the know but i haven't got my head
>
around the whole text manipulation routines as i'm used to VB with all the
>
functions built in.
>
>
split it and right it and away we go...
>
>
any help appreciated.
>
>
Andrew.
Step 1: Get the name.
----------------------------------
tell app "finder"
Set fName to the name of file "nz1711jr-11028812.eps"
end tell.
Step 2: Get the Regex extension from
http://www.lazerware.com/software.html
And install it - then,
--------------------------------------
set fName to REReplace fName with "\\2" pattern "([^-]*-)(.*)"
This defines two patterns to match the file name string against,
1: ([^-]*-), meaning any number of characters that are not a dash ending in
another character that is a dash, or, all characters up to and including the
first dash. (For those puzzled "([^-]*-)" is more reliable than "(.*-)")
Note: this won't work if any of your file names have >1 dash.
2: (.*), meaning any string of characters of any length, which, by coming
after the first pattern, means all the rest of the string.
The "\\2" is called a 'back reference' and means all the characters matched
by the second pattern. Because the back reference to the first pattern
(which would be "\\1" ) is omitted it won't be part of the replacement -
ie., everything up to and including the first dash will be dropped from the
string.
Step 3: Change the file name.
--------------------------------------
Tell app "finder"
set the name of file "nz1711jr-11028812.eps" to fName.
End tell
You will have to build either one or three loops around these steps, but you
will find that using the regular expression is very very fast compared to
apple scripted comparisons.
Step 4: Have some fun playing around with other regular expressions to see
how incredibly useful they are......
Cheers,
Ric Phillips
Computer Laboratory Support Officer
Faculty of Humanities and Social Sciences
La Trobe University
9479 2792