Solved

Rename files by adding the folder name they're located in to the file name with Perl recursively

Posted on 2015-01-08
5
145 Views
Last Modified: 2015-01-09
I have a bunch of files in folders with names like this:
scan0001
scan0002
scan0003
scan0004
....

And each folder has jpeg files named:
page_001.jpeg
page_002.jpeg
page_003.jpeg
etc. (the number of jpegs in the folder vary, but they follow this standard.)

I would like to know if there's a way to rename the files so that the folder location is added to the filename of the jpeg, like so:
scan0001page_001.jpeg
scan0001page_002.jpeg
scan0001page_003.jpeg
scan0002page_001.jpeg
scan0002page_002.jpeg
scan0002page_003.jpeg
scan0003page_001.jpeg
scan0003page_002.jpeg
....

Thanks
0
Comment
Question by:hadrons
  • 3
5 Comments
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40539515
Sure.  You'll need to make a few changes to this script (such as replacing /root/dir with the actual root directory of your hierarchy).
#!/usr/local/bin/perl

use strict;
use warnings;
use File::Find ();
use File::Copy qw(mv);

# for the convenience of &wanted calls, including -eval statements:
use vars qw/*dir/;
*dir    = *File::Find::dir;

use Cwd ();
my $cwd = Cwd::cwd();

# Traverse desired filesystems
File::Find::find({wanted => \&wanted}, '/root/dir');
exit;

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);
    return unless ((($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) && -f _);
    my $pre = (split m{/}, $dir)[-1];
    mv "$dir/$_", "$dir/$pre$_" or die "could not rename $_ to $pre$_ in $dir: $!";
}

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 40539575
perl -e 'rename $_,s#/##r or die "$_ $!" for<scan*/page_*.jpeg>'
0
 
LVL 84

Expert Comment

by:ozo
ID: 40539587
On second thought, maybe what you wanted was
perl -e 'rename $_,s#(.*)/#$1/$1#r or warn "$_ $!" for<scan*/page_*.jpeg>'
0
 

Author Closing Comment

by:hadrons
ID: 40541593
As you said, I changed the directory ref. and it worked perfectly.
0
 
LVL 84

Expert Comment

by:ozo
ID: 40541666
Are you saying mine didn't work?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
format the code in java 6 94
upcasting down casting object remains same but refernece changes 8 63
Specific format 21 198
word0 challenge 4 68
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question