From owner-freebsd-questions@FreeBSD.ORG Mon Jan 4 20:52:16 2010 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 28E70106566C for ; Mon, 4 Jan 2010 20:52:16 +0000 (UTC) (envelope-from nvidican@envieweb.net) Received: from gateway10.websitewelcome.com (gateway10.websitewelcome.com [64.5.38.11]) by mx1.freebsd.org (Postfix) with SMTP id DC75A8FC1A for ; Mon, 4 Jan 2010 20:52:15 +0000 (UTC) Received: (qmail 25116 invoked from network); 4 Jan 2010 21:07:53 -0000 Received: from armada.websitewelcome.com (74.52.142.66) by gateway10.websitewelcome.com with SMTP; 4 Jan 2010 21:07:53 -0000 Received: from localhost ([127.0.0.1]:59710) by armada.websitewelcome.com with esmtpa (Exim 4.69) (envelope-from ) id 1NRtuM-0004rS-4L; Mon, 04 Jan 2010 14:52:10 -0600 Received: from 19.1.212.137 (19.1.212.137 [19.1.212.137]) by www.envieweb.net (Horde MIME library) with HTTP; Mon, 04 Jan 2010 15:52:09 -0500 Message-ID: <20100104155209.rxtj6y2vk0w8o0g8@www.envieweb.net> Date: Mon, 04 Jan 2010 15:52:09 -0500 From: nvidican@envieweb.net To: "=?utf-8?b?RMOhcmlv?= \"P." References: <1262624558.9656.16.camel@dasp-laptop> <20100104145854.gdhdk5mb288oswgw@www.envieweb.net> <1262637277.12080.9.camel@dasp-laptop> In-Reply-To: <1262637277.12080.9.camel@dasp-laptop> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; DelSp="Yes"; format="flowed" Content-Disposition: inline Content-Transfer-Encoding: quoted-printable User-Agent: Internet Messaging Program (IMP) H3 (4.1.6) X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - armada.websitewelcome.com X-AntiAbuse: Original Domain - freebsd.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - envieweb.net Cc: freebsd-questions@freebsd.org Subject: Re: Rename pictures in the command-line interface X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 04 Jan 2010 20:52:16 -0000 No problem. You might also consider extending it to support '.jpeg' as =20 well as '.jpg', or even alter to work recursively through =20 sub-directories. Like I said though, it's more or less a starting =20 point. It will continue to extend beyond the current number each time =20 it's run too - so it should never over-write an existing file and =20 never need to be altered to support one more digit, (until system =20 limitations come in to play - but that's a whole other ball game in =20 terms of scale and probably the least of your worries at that point). Personally I find things like this a LOT easier to do in Perl for the =20 power and simplicity of Perls ability to handle and manipulate =20 strings, (again like I'd mentioned in my original reply), I'm sure =20 this is do-able in a shell script too just seems simpler to =20 read/write/work with written in Perl to me and it just gets the job =20 done. -- Nathan Vidican nathan@vidican.com Quoting "D=C3=A1rio \"P." : > Seg, 2010-01-04 =C3=A0s 14:58 -0500, nvidican@envieweb.net escreveu: > >> Dario, >> >> I'm not personally aware of any single commands which allow >> substitution using a counter like you're asking, or of a decent way to >> do what you're asking from the shell script either; however, >> personally I'd write a simple Perl script to do it. The trick being to >> be able to find the bsd###.jpg where it left off at in a directory so >> you don't overwrite existing files if repeatability is important. >> >> Here's something quick/dirty to work with, you can build from here, >> but try copy/pasting the following code into a new Perl script and run >> it from withing the directory you want to work: >> >> #!/usr/bin/perl -w >> >> >> use strict; >> >> my @files =3D `ls`; # gets a list of all files in the current dir >> # start a counter at zero, then increment it below: >> my $cntr=3D0; >> # set counter to the largest bsd###.jpg file in this directory: >> map { if (/^bsd(\d+)\.jpg/) { $cntr =3D $1 if($1>$cntr); } } >> grep(/bsd\d+\.jpg/,@files); >> >> print "Left off last time at $cntr, going to start this time at >> ",++$cntr,".\n"; >> >> foreach (@files) { >> chomp(); >> # skip all files which are already named bsd###.jpg >> # or are not in ending .jpg >> next if ($_ =3D~ /bsd\d+\.jpg/ || $_ !~ /(\.jpg)$/i); >> >> my $new =3D $_; >> # use a regular expression to substitute the name >> # (note /i =3D=3D case insensative so it will match '.JPG' as we= ll) >> $new =3D~ s/^(.+)\.jpg$/bsd$cntr\.jpg/i; >> >> print "Renaming $_ to $new\n"; >> # un-comment the line below to actually do the rename: >> # rename($_,$new); >> $cntr++; >> } >> >> ### END OF SCRIPT ### >> >> An example given a directory with files like: >> >> blah.Jpg >> bs432.jpg >> bsd11.jpg >> bsl.jpg >> uh-oh.jpG >> yourSelf.JPG >> >> Will give you an output like: >> >> Left off last time at 11, going to start this time at 12. >> Renaming blah.Jpg to bsd12.jpg >> Renaming bs432.jpg to bsd13.jpg >> Renaming bsl.jpg to bsd14.jpg >> Renaming uh-oh.jpG to bsd15.jpg >> Renaming youSelf.JPG to bsd16.jpg >> >> >> My $0.02 ... like anything, sure you could do this 100 different other >> ways, and sure it's not going to be really efficient for large >> volumes, but in a pinch it'll work pretty reliably. >> >> -- >> Nathan Vidican >> nathan@vidican.com >> >> > > Worked just the way I wanted! :) > Thank you so much for the time you spent doing this Perl script. > >