CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Can swap the x and y values in an "x-y.mpg" format

 



hranick
New User

Jun 17, 2010, 9:02 AM

Post #1 of 7 (3009 views)
Can swap the x and y values in an "x-y.mpg" format Can't Post

In my search to find a renaming program that could do this seemingly easy task I came across the Bulk Rename Utility which has PERL 5 support, and I am hoping this could do it.

I have about 6000 .mpg music videos that are all named "Title-Artist.mpg" (varying file name sizes of course). All I'm looking to do is swap everything before and after the "-" in the file name so I end up with "Artist-Title.mpg"

Can someone help me out, or maybe point me in the right direction so I can figure this out?


Bianca
User

Jun 18, 2010, 9:51 AM

Post #2 of 7 (2993 views)
Re: [hranick] Can swap the x and y values in an "x-y.mpg" format [In reply to] Can't Post

It's better to make it step by step.

Please test this script with your files:

Code
#!/usr/bin/perl -w 
use strict;
use warnings;
use diagnostics;
print "Attention: please start this script in console (f.e. cmd.exe)\nand change to correct device first (f.e. d:)\n";
print "Please insert path to test (f.e. \\my place\\music): ";
my $input = <STDIN>;
chomp $input;
my @filenames;
if (opendir my $fh,$input) {
foreach my $file (readdir $fh) {
next if $file eq '.' || $file eq '..';
if (-f "$input/$file" && !-z "$input/$file") {
push @filenames,$file;
}
}
}
else {
print "Sorry, can not open directory '$input', error '$!'\n";
print "Simulation starts:\n\n";
@filenames = (
'auf dem hohen berg-roger wittaker.mpg',
'schoene welt-heino.mpg',
);
}
foreach (@filenames) {
if ($_ =~ /^([^-]+)-([^\.]+)\.(.+)$/) {
print "old: $_ ** new: '$2-$1.$3'\n";
}
}



hranick
New User

Jun 18, 2010, 6:33 PM

Post #3 of 7 (2983 views)
Re: [Bianca] Can swap the x and y values in an "x-y.mpg" format [In reply to] Can't Post

Ok forgive me for not understanding this in this first place, but I cannot use a full code like that. The program only accepts 1 line of code for match and 1 line of code for replace as regular expressions.

I think I'm starting to understand it, I need to set everything prior to the "-" as group 1 (\1) and then everything after as group 2 (\2) then it's as simple as telling the program how to structure the groups. So if someone could help me figure out how to set those groups, I would much appreciate it, I'm off to try to figure it myself.


Bianca
User

Jun 19, 2010, 4:12 AM

Post #4 of 7 (2969 views)
Re: [hranick] Can swap the x and y values in an "x-y.mpg" format [In reply to] Can't Post


In Reply To
The program only accepts 1 line of code for match and 1 line of code for replace as regular expressions.

?????? Which program?

You ask in a Perl forum "I need a program that..." and then you tell me, you need 2 lines????


Zhris
Enthusiast

Jun 19, 2010, 12:59 PM

Post #5 of 7 (2953 views)
Re: [hranick] Can swap the x and y values in an "x-y.mpg" format [In reply to] Can't Post

Took me a while to figure out what you actually required. You are using a program "Bulk Rename Utility" and in order for you to rename your files you are asked to provide 2 regular expressions (1 for match and 1 for replace). Therefore your question would have been better suited in the regular expressions section of this forum.

I think you could use the regular expression from Bianca's print loop e.g.

Match:
^([^-]+)-([^\.]+)\.(.+)$

Replace:
$2-$1.$3

Chris


(This post was edited by Zhris on Jun 19, 2010, 1:06 PM)


hranick
New User

Jun 26, 2010, 10:07 AM

Post #6 of 7 (2867 views)
Re: [Zhris] Can swap the x and y values in an "x-y.mpg" format [In reply to] Can't Post

I'm sorry for any confusion I may have created and thank you so much for your help, I was able to finish my work


Bianca
User

Jun 28, 2010, 1:43 AM

Post #7 of 7 (2807 views)
Re: [hranick] Can swap the x and y values in an "x-y.mpg" format [In reply to] Can't Post

You're welcome

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives