Re: File name search & replace question
Re: File name search & replace question
- Subject: Re: File name search & replace question
- From: has <email@hidden>
- Date: Tue, 29 Oct 2002 19:03:11 +0000
Rips Ethan J wrote:
[SNIP]
I'm unsure if you've worked out exactly what your script needs to do, as
your written requirements as posted are somewhat unclear. I suggest writing
these up in full to clarify the requirements in your own mind, as well as
for us here. Describe the structure(s) of the old filenames, the
structure(s) of the new filenames, and the rules to follow when converting
from one to the other.
Even so, it looks like you're trying to tackle several problems
simultaneously: string-tokenising, custom-sorting and renaming all at once.
This may be why you're having a bit of a headache in finding a solution:
it's just too much to deal at once. I recommend breaking the process into
small, sequential, single-purpose steps. (I've tried to do this below,
though since I may not have understood the requirements correctly it may
require further modifcation.)
The three main stages I've split the task into are: breaking down the
filenames for all the files; ordering those files according to your master
'codes' list; (re)assembling and applying the new filenames. Below is a
more detailed breakdown:
--------------
1. Initialise an associative array object. Prepeare it by adding an entry
for each alpha code: the key should be the code and the value an empty list.
-----
2. For each file in your folder...
2a. Using [e.g.] regex to break up the file's name string, create a
fileInfo record of form:
"385SOI2.txt" -->
{
numberPart:"385",
textPart:"SOI",
hasTrailingNumber:true,
trailingNumber:"2"
suffixPart:".txt",
fileRef:[Finder file reference]
}
[Note: if hasTrailingNumber is false, you should set trailingNumber to "1".]
2b. Use the textPart's value to retrieve the appropriate value list from
the assoc. array, and add the record to the end of that list.
-----
3. Initialise a counter value/object that'll create your "001", "002",
"003", etc. prefixes.
-----
4. For each code in your custom-ordered alpha code list...
4a. retrieve the appropriate value list from the assoc. array.
4b. Sort the value list on the numberPart, the trailingNumber, and then, if
necessary, the suffixPart as well.
--
4c. For each fileInfo record in the sorted value list...
4ci. Increment the prefix counter and get a prefix string from it.
4cii. Assemble the new file name string: counterPart + textPart +
numberPart + trailingNumber (if hasTrailingNumber is true) + suffix
4ciii. Get the file using the fileInfo record's fileRef and change its name.
--------------
>
Obviously, I'll want to modularize the whole mess via handlers, but first I
>
need to figure out a process that performs the steps outlined above.
I think you'll find it easier going the other way around: breaking the
process into a series of nice, modular steps that will be easy to code. If
it's proving hard to modularise, that's generally because you haven't
broken it down into the right modules, so try again. As long as you're
doing this on paper, it's quick and easy to change your mind; once you
start writing code it is not.
If you've got an idea/concept/theory that you need to code and test to
check it'll work in practice, do this... but don't get bogged down in this
(i.e. don't start writing production code just to do a quick
proof-of-concept test, and don't start writing your system around such
experimental code either).
Write and test each section one at a time. Make sure each section works
_before_ putting them all together at the end, as testing and debugging
several small, simple components is _far_ easier than testing one huge,
complex application.
-----
For an associative array library, I suggest using associativeArrayLib from
my site. To sort a list of records with weighting, you might try Serge's
qSort library which you'll find on AppleMods at macscripter.net.
For creating prefixes, I've knocked the following code together for you to
try. It creates a NumberStringGenerator object that produces padded
numerical strings. Very simple, and you should be able to figure out how to
use it from the test code.
======================================================================
on newNumberStringGenerator(stringLen)
if (stringLen's class is not integer) or (stringLen is less than
[NO-BREAK]1) then error "Invalid string length." number 200
set zeroString to ""
repeat stringLen times
set zeroString to zeroString & "0"
end repeat
script -- NumberStringGenerator
property _zeros : zeroString
property _len : stringLen
property _max : (10 ^ stringLen) - 1
property _count : 0
on incrementCount()
set _count to _count + 1
if _count is greater than _max then error "Max count
[NO-BREAK]reached." number 210
end incrementCount
on numberString()
return text -_len thru -1 of (_zeros & _count)
end numberString
end script
end newNumberStringGenerator
-------
--TEST
set x to newNumberStringGenerator(3)
repeat 111 times
x's incrementCount()
log x's numberString()
end repeat
======================================================================
HTH
has
--
http://www.barple.pwp.blueyonder.co.uk -- The Little Page of AppleScripts
_______________________________________________
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.