?
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
Medium Priority
?
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 27

Accepted Solution

by:
wilcoxon earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
Suggested Courses

649 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