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
143 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
Comment Utility
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
Comment Utility
perl -e 'rename $_,s#/##r or die "$_ $!" for<scan*/page_*.jpeg>'
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
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
Comment Utility
As you said, I changed the directory ref. and it worked perfectly.
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
Are you saying mine didn't work?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
why to use string 10 55
recursion example 16 66
Not needed 13 53
Problem to open Excel file 15 37
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

772 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now