* Erik Price <[EMAIL PROTECTED]> [2002-06-25 12:11]:
> I'm just starting learning Perl and I'm trying to write a simple Perl 
> script that will change a file's name for me.
> 
> All of my filenames match the following pattern:
> 
> \w+-\d{2,2}\.php\.noheadscript # ex: "index0-18.php.noheadscript"
> 
> and I want to remove the ".noheadscript" part.  This can be done
> really easily in bash using a foreach loop and then using bash's
> awkward globbing to "mv" the filename to the new filename, but two
> things -- I don't have a reference handy for the advanced globbing
> syntax and I'd rather do it in Perl as an exercise anyway.  I think
> what I have so far would work except that I don't know how to print to
> the file's name -- it's not the same thing as printing to a file's
> contents as far as I can tell.

for i in *.php.noheadscript; do mv $i `echo $i | sed -e "s/\.noheadscript$//"`; done

> #!/usr/bin/perl -w
> 
> # hsnamerewrite.pl
> # stupid script that changes the name of a file
> 
> use strict;
> 
> foreach my $file (@ARGV) {
>       $file =~ s!^([\w-]+\.php)\.noheadscript!$1!;
    (my $new_file = $file) =~ s!^([\w-]+\.php)\.noheadscript!$1!;
    rename $file, $new_file
      or warn "Can't rename $file to $new_file: $!";
> }
> 
> The above script would work fine if I was attempting the substitution
> against a line or some kind of file data, but not the filename itself.
> Can someone give me a pointer?

The problem is that you are manipulating a string containing the name of
the file, not the name of the file itself.

(darren)

-- 
As soon as man does not take his existence for granted, but beholds it
as something unfathomably mysterious, thought begins.
    -- Albert Schweitzer

Reply via email to