Parsing filenames
Parsing filenames
- Subject: Parsing filenames
- From: brian mather <email@hidden>
- Date: Wed, 11 Jun 2003 22:38:06 -0400
I have a folder full of files that follow a standard naming convention:
[prefix][digits]_[string].extension
The [prefix] part of the name is either "p" or "pANYTEXT-".
The digits is a string of 6 or 7 numbers.
the _[string] is sometimes present and sometimes absent.
And finally the extension while typically present does not necessarily have
to exist and I'd like the script to be able to gracefully handle either
instance.
(e.g.: p123.tif; pFOO-456; pBAR-789_yippee.jpg; ...)
I'm interested to parse the filename extracting and writing to a file the
digits and the string following the underbar where applicable.
My plan is (was?) to set TIDs for key markers in the name, collect the bits
store them in variables then write the variables out. I've no trouble with
the latter from previous experience, but I'm stumbling on how to strip the
"p" from files named like "p123.tif"
I expect the answer to the question that plagues me is simple, but I'm
unable to figure out why this beginning of the script isn't working:
tell application "Finder"
activate
set the_folder to choose folder
set the_files to every file of the_folder
repeat with a_file in the_files
set long to the name of a_file
set AppleScript's text item delimiters to "-"
-- following cuts off the "pANYTHING-" perfectly
if the number of text items of long is greater than 1 then
set trimmed to text item 2 of long
else
-- Following is where I'm stuck
-- error "Finder got an error: Can't get
-- characters 2 thru last character of "p123.tif".
set trimmed to characters 2 thru the last character of long
-- tried substituting items, text items, adding parens
-- with no joy.
end if
end repeat
end tell
Once over this hurdle the remainder of the parsing will follow a similar
pattern:
set TIDS to "."
-- error proofing for no extension required.
-- solving my first problem should teach me how to avoid it here.
set digits_string to text item 1 of trimmed
if digits_string contains "_" then
set TIDS to "_"
set the_digits to text item 1 of digits_string
set the_string to text item 2 of digits_string
else
set the_digits to digits_string
set the_string to ""
end if
--write it all out
Any advice greatly appreciated.
Thanks-
brian
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.