Re: script to rename files (1)
Re: script to rename files (1)
- Subject: Re: script to rename files (1)
- From: has <email@hidden>
- Date: Wed, 7 Aug 2002 16:10:09 +0100
brian wrote:
>
the problem is this:
>
He spends hours weekly renaming image files from whatever their old name was
>
to a new name.
[...]
>
Today he accomplishes this task by manually researching and naming the files
>
armed with countless spreadsheets detailing the translation. Typically
>
there are three key pieces to this puzzle, the new name (our SKU), the
>
vendor part number (or whatever) which is the rosetta stone in each equation
>
and the filenames of the images.
Sounds like this might be handled best using a relational database - each
spreadsheet would become a table, with the vendor part number being used to
link the tables together.
>
What I've built (script at the end of this message) is a script that is run
>
as an applet that takes in tab delimited text files saved from an Excel
>
document where column a contains the new names and column b contains the
>
existing image filenames. Point it to a folder of source images and tell it
>
where the renamed files should be deposited. All files it can rename based
>
on the data are copied, renamed and the source files are labeled in their
>
original location. Those that are not labeled are exceptions that must be
>
researched manually.
Thoughts: instead of labelling processed files (which may be fickle, and
certainly won't work on X), you could move the processed source files to
their own "source files (processed)" folder? That way, only unprocessed
files will be left in the original source folder, which might make for a
tidier workflow. Worth considering.
>
Also, no doubt my ignorance at fault here the period separating file type
>
extensions from the main body of the file name stymied my TID manipulations
>
to no end, so to get around it I took the easy way out by searching in excel
>
for every '.' (period) and replacing it with '_' (underbar) so the items in
>
column b read 'abc_jpg' instead of 'abc.jpg' I set everything back the right
>
way in the script so file refs work, but I'm curious if someone could
>
enlighten me what I'm not quite doing right ?
This is a poor approach: the user shouldn't have to mess with their data to
suit your script. It just means that there's more things that can go wrong
at the end of the day (e.g. user forgets to modify the spreadsheet; any
data that originally contained underscores will get screwed up when your
script converts those into periods as well). You really need to figure out
and fix the original problem, not kludge your way around it. If you want
more help then you need to be more specific: post some examples of the
source data and the problem code and explain where it's going wrong.
>
Finally, the success of this tool in its current form is predicated on my
>
colleague's ability to do vlookups or several of a hundred other gyrations
>
to get the existing filenames lined up with the new filenames which testing
>
has shown can be done (he needs to do it now to do it manually after all).
>
I'd prefer if it weren't necessary to get everything lined up quite so
>
perfectly instead employing some pattern matching or regex type stuff to
>
allow him to take the data as it's provided with SKU and vendor part number
>
already lined up and point it to a folder of files whose names might contain
>
the vendor part number, or a reasonable facsimile of it.
Can you clarify more? Again, it sounds like you ought to be using a
relational DB for combining the incoming data so you can extract out just
the bits you need. You can certainly thrash it using AS alone, but you're
creating quite a bit of extra work for yourself if you do. The cost of
developing such functionality for yourself in AS may be quite a bit more
than it'd take to purchase and set up a copy of FileMaker to do most of
this gruntwork for you. Also, a homegrown solution may well be considerably
slower in use (AS isn't the fastest language in the world, and writing
fast, efficient AS routines can be an artform in itself), which may be
another issue to consider. Use the best tool for the job, etc.
>
As impetus and proof of concept I have been told (by yet another
>
colleague) that he has a very similar piece of functionality built using
>
Access. My desire has never been greater.
If the Access solution does [almost] what you need, why not use [modify]
that instead? (Access is a relational DB after all, and no doubt scriptable
using VBScript.) It could be much simpler than implementing a complete new
system instead.
--
Comments on the code: you really should try to use handlers to organise
your code - breaking it up into small, self-contained functions make it
much easier to develop, test, modify and expand. You'll thank yourself
later if you do (and curse yourself if you don't). For example, in very(!)
rough, pseudo-codish fashion, you might have something like:
getUserSettings() -- configure source/destination files/folders
convertTabDelimFileToTable() -- prepare table data for use
processSourceFolder()
on processSourceFolder()
repeat with eachEntry in table
set oldName to first item of eachEntry
set newName to composeNewName(second item of newEntry)
try
renameFile(oldName, newName) -- change old name to new
(prefix/name/extension; whatever)
on error e number n
--handle errors caused if file not found (e.g. by writing
filenames to a log file)
end
moveFileToDestinationFolder()
end
end
Or it may be that you want to drive the system the other way, by iterating
across the files in the folder instead of entries in the table. In which
case, you'd do it differently: getting each file in turn, querying the
table for a matching entry, and changing the file's name if the entry is
found, or logging an error if not. Work it all out on paper first. Then
write and test each part *in isolation* before finally combining them into
one big program. (Oh, and avoid using lots of globals when you can just as
easily pass values as handler parameters; too many globals make code hard
to read, test and maintain, no matter how tempting they may seem at the
time.)
HTH
has
--
(My email address has changed from <email@hidden> to
<email@hidden>. Please update your address books accordingly.)
_______________________________________________
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.