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;

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

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.