By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,851 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,851 IT Pros & Developers. It's quick & easy.

Renaming files

P: 2
Hi I'm relatively new to Perl.

I'm attempting to read in all the file names which are located in a directory to an array, modify each file name by removing the last 4 characters of each file then replacing them with a new 4 character extension. I then would like to set symbolic links using the new file names.

eg:- <filename>.img to <filename>_trn.img

So far i have the following code which works fine at creating the symbolic links, but i cant seem to get the file name change.
Please help :)

Expand|Select|Wrap|Line Numbers
  1. my $dirs = "$folder";
  2. opendir (BIN, $dirs) || die "cant open directory: $!";
  3. my @array = grep { -d "$dirs" } readdir BIN;
  4. foreach my $file (@array) {eval {
  5. symlink ("$folder/$file","$file/_trn.img");
  6.  
Feb 26 '08 #1
Share this Question
Share on Google+
4 Replies


KevinADC
Expert 2.5K+
P: 4,059
actually you are reading in a list of folders in the directory:

Expand|Select|Wrap|Line Numbers
  1. my @array = grep { -d "$dirs" } readdir BIN;
the -d flag checks if the expression is a directory. There is no need to quote a single scalar, you should stop doing that now, it is a bad habit to get into when writing perl code:

Expand|Select|Wrap|Line Numbers
  1. my @array = grep { -d $dirs } readdir BIN;
Feb 26 '08 #2

eWish
Expert 100+
P: 971
I like to use File::Find::Wanted module to seek out files. Then I started using File::Basename to parse the file name. You can use the rename() function to rename a file.

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2. use File::Find::Wanted;
  3.  
  4. my $path = '/path/to/directory';
  5. my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;
  6.  
  7. foreach my $file (@files) {
  8.  
  9.  
  10.     my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
  11.     my  $new_filename = $old_filename . '_new' . $ext;
  12.  
  13.     rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
  14.  
  15. }
--Kevin
Feb 26 '08 #3

P: 2
I like to use File::Find::Wanted module to seek out files. Then I started using File::Basename to parse the file name. You can use the rename() function to rename a file.

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2. use File::Find::Wanted;
  3.  
  4. my $path = '/path/to/directory';
  5. my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;
  6.  
  7. foreach my $file (@files) {
  8.  
  9.  
  10.     my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
  11.     my  $new_filename = $old_filename . '_new' . $ext;
  12.  
  13.     rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
  14.  
  15. }
--Kevin

Hi Kevin

Thanks for this and for your quick response,

However I don't necessarily want to change the original file names located in the folder. I would like to read in the names to an array then remove the last 4 characters of each file within the array. once this is done add a new extension to each of the files within the array then use these new file names to link to the original.
So the end result should look something like this below

<original filename><new exstention> symbolicaly linked to /<folder>/<original file name>
OR
<original filename>_trn.img symbolically linked to <original filename>.img

Hope this makes sense, think I lost it there :)
Feb 27 '08 #4

P: 12
However I don't necessarily want to change the original file names located in the folder. I would like to read in the names to an array then remove the last 4 characters of each file within the array. once this is done add a new extension to each of the files within the array then use these new file names to link to the original.
So the end result should look something like this below

<original filename><new exstention> symbolicaly linked to /<folder>/<original file name>
OR
<original filename>_trn.img symbolically linked to <original filename>.img

Hope this makes sense, think I lost it there :)
Kevin's example code is correct in large part. It is only his misunderstand of your desire to create symlinks rather than renaming the files that is an issue. No doubt this was in large part due to the unfortunate choice of subject in the original post. Still his code stands up to the task. You'd simply need to change:

Expand|Select|Wrap|Line Numbers
  1. rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
  2.  
to something like:

Expand|Select|Wrap|Line Numbers
  1. symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
  2.  
Incidentally, the use of File::Find::Wanted is really only needed if you intend to restrict the changes to particular patterns of filenames, and the use of File::Basename is handy only if your file extension is to remain the same, with only a few characters appended to the basename (as in your examples).

If you are also intending to map certain extensions to new extensions: For instance creating symlinks with .jpg extensions for all files that currently have .jpeg extentions, then File::Basename is still handy. You'd just insert some logic to test for a given extension and rewrite it in the name for the symlink. The following code would still add to the basename, but would create symlinks to .jpeg files as .jpg instead of .jpeg:

Expand|Select|Wrap|Line Numbers
  1. use File::Basename;
  2. use File::Find::Wanted;
  3. my $path = '/path/to/directory';
  4. my %ext_map = ( '.jpeg' => '.jpg' );
  5. my @files = find_wanted( sub { -f && /\.jp.*g$/ }, $path ) ;
  6. foreach my $file (@files) {
  7.     my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
  8.     my $new_ext = $ext_map{$ext} || $ext;
  9.     my  $new_filename = $old_filename . '_new' . $new_ext;
  10.     symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
  11. }
  12.  
I only mention this because it was not entirely clear from your most recent post if you also wanted such a capability in the script.
Feb 27 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.