[geeks] Re: [SunRescue] perl
Greg A. Woods
geeks at sunhelp.org
Thu May 24 12:09:26 CDT 2001
[ On Thursday, May 24, 2001 at 16:23:25 (+0100), Paul Sladen wrote: ]
> Subject: Re: [SunRescue] filenames..
>
> find ./ -type f -exec perl -e '$to=$from=$ARGV[0]; $to =~ tr/A-Z/a-z/; `mv "$from" "$to"`' {} \;
[ On Thursday, May 24, 2001 at 10:05:56 (-0500), Dan Debertin wrote: ]
> Subject: Re: [SunRescue] filenames..
>
> for i in `find thedir/ -type f -print`; do
> mv $i `echo $i | perl -e '$thing = <STDIN>;print lc($thing);'`
> done
Now would you look at that. Not only two completely different ways to
write the script in perl, but neither of them is a proper and complete
self-contained perl script! (the first at least avoids using the shell,
but still ends up running a massive perl interpreter once per name)
What a terrible waste!
I think I'll have to cleanse my mind now and go off and write a
smalltalk method that can do the same entirely from within smalltalk and
without having to define any new classes (at least for Squeak).
There, done: (20 min. total to create a new project window, fiddle with
the internal windows a bit by creating new workspace, transcript and
browser windows; read the whole class FileDirectory (I've never done
anything like this before in Smalltalk!); then write some examples that
simply showed a script in the Transcript window which would do what I
wanted; and finally write the following statement!)
FileDirectory default fullNamesOfAllFilesInSubtree do: [:filename |
self primRename: filename to: (filename asLowercase).
].
Obviously that implementation is no safer and no more efficient than the
first pure shell version I posted, and it also assumes Squeak was
started in the "top" directory of the subtree which needs renaming.
A safe, efficient, implementation would have to be a little more
complex, obviously. Maybe something like this:
"this code is untested! ;-)"
"but the overal structure was stolen from an existing method..."
|todo dir|
todo := OrderedCollection with: (FileDirectory on: '/subtree/to/rename').
[todo size > 0] whileTrue: [
dir := todo removeFirst.
dir fileNames do: [:fn |
ln := (fn asLowercase).
(ln = fn) ifFalse:
dir rename: fn toBe: ln.
].
dir directoryNames do: [:dn |
todo add: (dir directoryNamed: dn).
].
].
--
Greg A. Woods
+1 416 218-0098 VE3TCP <gwoods at acm.org> <woods at robohack.ca>
Planix, Inc. <woods at planix.com>; Secrets of the Weird <woods at weird.com>
More information about the geeks
mailing list