RE: ways to change the first char from a filename (with full path )

From: Bob Showalter (Bob_Showalter_at_taylorwhite.com)
Date: 06/29/04


To: 'Rod Za' <rodzadra@yahoo.com>, beginners@perl.org
Date: Tue, 29 Jun 2004 12:23:04 -0400

Rod Za wrote:
> Hi all,
>
> i'm trying to make a code that get a file name (with full path) and
> change the first char of the filename. Someone can say me if there's
> a better way to do this?:
>
> _BEGIN_
> #!/usr/bin/perl -w

  use strict;

> my($file) = $ARGV[0]; #receives the filename

  my $file = shift; (the traditional idiom)

> my @tmp = split(/\//,$file); #split the path

  use File::Basename;
  my ($name, $path) = fileparse($file); (more portable)

> $tmp[$#tmp] =~ s/.(\w+)/c$1/g; #change the first char of the
> filename

  substr($name, 0, 1) = 'c'; (no need for regex)

> my $IPPFile = join('/',@tmp); #join again path+'/'+filename
> print "Original Filename: $file - Changed Filename: $IPPFile\n";
> #print the result _END_

  use File::Spec
  print "Original name: ", $file,
    " Changed name: ", File::Spec->catfile($path, $name), "\n";

You might also have a look at the ubiquitous (and ancient) perl "rename"
script: http://www.cpan.org/scripts/nutshell/ch6/rename



Relevant Pages