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: Perl Programming Help: Regular Expressions:
Organizing Files Using Regular Expressions

 



valefor
New User

Aug 25, 2011, 2:03 AM

Post #1 of 4 (7030 views)
Organizing Files Using Regular Expressions Can't Post

I have a number of files that I want to organize into their own folders based on part of their filename. So far I've managed to read in all of the files in said directory properly, but for whatever reason the regular expressions that I'm using to match the filenames and strip off the unnecessary portions of the filename to create the directory names aren't working properly. Specifically, it's returning things that I did not specify, such as folders and patterns that don't match the regex that I set up, and it doesn't seem to be lopping off characters properly in the $FolderName variable.

Here's what I have so far:


Code
#!/usr/bin/perl -w 

use strict;
use 5.010;
use File::Find::Rule;
use File::Path;
use File::Copy;


my $interesting = File::Find::Rule->new->file()->name(qr/\[*\]*\.*$/)->start('.');

while ( my $perl_file = $interesting->match ) {
my $FolderName = $perl_file;
$FolderName =~ m/](.*?)-/;
print $FolderName . "\n";
}


To avoid potentially screwing up the folder structure while I'm working this out I'm just printing off the folder name rather than moving stuff.


(This post was edited by valefor on Aug 25, 2011, 2:07 AM)


BillKSmith
Veteran

Aug 26, 2011, 6:42 AM

Post #2 of 4 (6935 views)
Re: [valefor] Organizing Files Using Regular Expressions [In reply to] Can't Post

I cannot tell what you expect your regular expressions to do so I cannot suggest corrections. I hope I can be of some help by describing what they actually do.


Code
qr/\[*\]*\.*$/



This matches an arbitrary number (including zero) of left brackets, followed by an arbitrary number (including zero) of right brackets, followed by an arbitrary number (again including zero) of arbitrary characters. Note: because everything is optional, this regex will match any string including a null string!




Code
$FolderName =~ m/](.*?)-/;



The regex matches everything from the first right bracket to the first hyphen. Everything in the match except the bracket and the hyphen are stored in $1. The return value indicating whether or not there is a match is ignored. The result $1 is also ignored.

I have not checked to see if your use of the modules is correct.
Good Luck,
Bill


valefor
New User

Aug 26, 2011, 4:40 PM

Post #3 of 4 (6828 views)
Re: [BillKSmith] Organizing Files Using Regular Expressions [In reply to] Can't Post

Thank you for the clarification. This program is meant to find all of the files that follow a specific format and organize them into folders based on part of their filename. The format of the filenames in question is [text]more text - number.ext. The first regex is intended to just create a list of those files within the folder that needs to be organized. The next regex strips off everything between the ] and the - to create a folder name for that particular file. Once the folder name's found the program would then check to see if the folder actually exists. If it does than it'd move the file into that folder, if it doesn't exist than it'll create the destination folder before doing the move.

It looks like I'll have to tweak the regex code so it only searches for words rather than something that allows for a null length check.


BillKSmith
Veteran

Aug 28, 2011, 7:23 PM

Post #4 of 4 (6390 views)
Re: [valefor] Organizing Files Using Regular Expressions [In reply to] Can't Post

I assume that all your source files are in the same directory. In that case, File::Find is overkill. Use glob (perldoc -f glob). Glob patterns are much easier than regular expressions.
Good Luck,
Bill

 
 


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

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