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
148 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 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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 …
Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

696 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