Connecting Tech Pros Worldwide Forums | Help | Site Map

rename files in a directory

Newbie
 
Join Date: May 2007
Posts: 6
#1: Jun 5 '07
The script will rename
all files in the current directory whose names contain the first argument
by replacing that part of the filename with the second argument.

so far i could just get the file names that matches $ARGV[0].I need help on how to rename the files .For ex if argumnets are a bbb
if a file name is rename.txt
then O/P must be renbbbme.txt.

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3.  
  4. use File::Find;
  5. die"Usage:$0 filename newname\n" if @ARGV!=2;
  6. my ($filename, $newname) = @ARGV;
  7.  
  8. opendir(D,".") or die "Cannot open .: $!";
  9.  
  10. my @allfiles = ();
  11. foreach my $file (readdir D) {
  12.     next if -d "$file";
  13.     next if $file !~ m/[\Q$ARGV[0]\E]/;
  14.     push @allfiles, $file;
  15. }
  16. print "@allfiles\n";
  17.  
Thanks
Mythili

KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#2: Jun 5 '07

re: rename files in a directory


Is this school work?
Newbie
 
Join Date: May 2007
Posts: 6
#3: Jun 5 '07

re: rename files in a directory


yes.I am trying my best.Any clue on how to proceed will really help me.
mythili
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#4: Jun 5 '07

re: rename files in a directory


look into the rename() function.
miller's Avatar
Moderator
 
Join Date: Oct 2006
Location: San Francisco, CA
Posts: 830
#5: Jun 5 '07

re: rename files in a directory


perldoc rename

- Miller
Newbie
 
Join Date: May 2007
Posts: 6
#6: Jun 5 '07

re: rename files in a directory


thank u. .
Reply