Renaming files

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • al bundy
    New Member
    • Feb 2008
    • 2

    #1

    Renaming files

    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>.im g 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 :)

    [code=perl]
    my $dirs = "$folder";
    opendir (BIN, $dirs) || die "cant open directory: $!";
    my @array = grep { -d "$dirs" } readdir BIN;
    foreach my $file (@array) {eval {
    symlink ("$folder/$file","$file/_trn.img");
    [/code]
  • KevinADC
    Recognized Expert Specialist
    • Jan 2007
    • 4092

    #2
    actually you are reading in a list of folders in the directory:

    Code:
    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:

    Code:
    my @array = grep { -d $dirs } readdir BIN;

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      I like to use File::Find::Wan ted 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.

      [CODE=perl]use File::Basename;
      use File::Find::Wan ted;

      my $path = '/path/to/directory';
      my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;

      foreach my $file (@files) {


      my ($old_filename, undef, $ext) = fileparse($file ,qr{\..*});
      my $new_filename = $old_filename . '_new' . $ext;

      rename( "$path/$old_filename$e xt", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";

      }[/CODE]

      --Kevin

      Comment

      • al bundy
        New Member
        • Feb 2008
        • 2

        #4
        Originally posted by eWish
        I like to use File::Find::Wan ted 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.

        [CODE=perl]use File::Basename;
        use File::Find::Wan ted;

        my $path = '/path/to/directory';
        my @files = find_wanted( sub { -f && /\.jpg$/ }, $path ) ;

        foreach my $file (@files) {


        my ($old_filename, undef, $ext) = fileparse($file ,qr{\..*});
        my $new_filename = $old_filename . '_new' . $ext;

        rename( "$path/$old_filename$e xt", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";

        }[/CODE]

        --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.i mg symbolically linked to <original filename>.img

        Hope this makes sense, think I lost it there :)

        Comment

        • minowicz
          New Member
          • Feb 2008
          • 12

          #5
          Originally posted by al bundy
          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.i mg 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:

          Code:
          rename( "$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't rename file $old_filename: $!\n";
          to something like:

          Code:
          symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
          Incidentally, the use of File::Find::Wan ted 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:

          Code:
          use File::Basename;
          use File::Find::Wanted;
          my $path = '/path/to/directory';
          my %ext_map = ( '.jpeg' => '.jpg' );
          my @files = find_wanted( sub { -f && /\.jp.*g$/ }, $path ) ;
          foreach my $file (@files) {
              my ($old_filename, undef, $ext) = fileparse($file,qr{\..*});
              my $new_ext = $ext_map{$ext} || $ext;
              my  $new_filename = $old_filename . '_new' . $new_ext;
              symlink("$path/$old_filename$ext", "$path/$new_filename" ) || die "Can't create symlink $new_filename to file $old_filename: $!\n";
          }
          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.

          Comment

          Working...