?
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
?
149 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 26

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
This video teaches viewers about errors in exception handling.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

777 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