Clicky

I have an external program that creates files to be emailed as attachments. For each email, it creates two files - a text file with the email "header" information and an attachment (at this point, just PDF files). Right now, there is only one attachment per email. However, at some point in the future they may be more than one (eg a PDF and a CSV file attachment for the same email). So, it drops files in a directory which looks like:

FILE-1.txt
FILE-1.pdf
FILE-2.txt
FILE-2.pdf
.
.
.
FILE-10.txt
FILE-10.pdf
FILE-11.txt
FILE-11.pdf

I wrote a Perl script to read, for example, FILE-1.txt to find out From, To, Subject and then send an email attaching FILE-1.pdf using list_dir from File::Util. (This is using ActiveState on Windows)

The problem is with my --pattern. My intent was to have it match the file name and any extension other than .txt and attach those to the email. However, it's matching everything that starts with FILE-1.  So, for example, for FILE-1.txt, it's attaching FILE-1.pdf (but not FILE-1.txt), FILE-10.txt, FILE-10.pdf, FILE-11.txt, etc.

Here's a snippet of the code:

 @attachfilelist = $attachfiles->list_dir($config->sourcedir(),'--files-only', '--with-paths', "--pattern=^$name\.(?!txt)");                             
1: 

Select allOpen in new window



Can anyone offer suggestions on what the correct pattern is to find all FILE-1.* attachments (excluding .txt)?

asked 07/30/2010 02:17

shersker's gravatar image

shersker ♦♦


7 Answers:

Try this instead:

1:
2:
@attachfilelist = $attachfiles->list_dir($config->sourcedir(),'--files-only', '--with-paths', "--pattern=^$name.*?(?<!txt)$");
link

answered 2010-07-30 at 10:44:06

kaufmed's gravatar image

kaufmed

Thanks, all.

No, I'm not good with Regex and clearly that's what the issue is.

Kaufmed, thanks for the new line. I tried that and it errored out with:

[code]Final $ should be \$ or $name at c:\edi\smtp\bin\smtpedi.v2.03.pl line 221, within string
syntax error at c:\edi\smtp\bin\smtpedi.v2.03.pl line 221, near ", "--pattern=^$name.*?(?<!txt)$""
Execution of c:\edi\smtp\bin\smtpedi.v2.03.pl aborted due to compilation errors.[/code]

At this point, I'm not above throwing random punctuation in to the regex...so I tried as it suggested with the final $ being \$. It matched both files (in this specific case, FILE-1.pdf and FILE-10.pdf) - which is an improvement as my previous code would have also included FILE-10.[b]txt[/b]...so at least it's now only attaching files that do not have .txt as the extension. But still only matching "FILE-1" in FILE-1 and FILE-10.

link

answered 2010-07-30 at 11:28:42

shersker's gravatar image

shersker

If the value of $name is "FILE-1", then you will only ever match filenames which have a 1 as the first digit. If you would like to vary that part to be various digits, then remove the "1" from $name and use the following:

1:
2:
@attachfilelist = $attachfiles->list_dir($config->sourcedir(),'--files-only', '--with-paths', "--pattern=^$named+.+?(?<!txt)$");
link

answered 2010-07-30 at 12:03:23

kaufmed's gravatar image

kaufmed

The actual file name format isn't under my control, unfortunately. The file name (and resulting $name) is a 3 letter code followed by the date and then an incrementing number for that date:

LND2010JUL26-1.txt (and matching .pdf)
LND2010JUL26-2.txt (-3, -4, -5 etc on up)
LND2010JUL26-10.txt
LND2010JUL26-11.txt
OSH2010JUL26-1.txt
OSH2010JUL26-2.txt
OSH2010JUL26-10.txt
OSH2010JUL26-11.txt
LND2010JUL27-1.txt (new date, start increment at 1 again)
LND2010JUL27-2.txt
LND2010JUL27-10.txt
LND2010JUL27-11.txt

I need to get it to match $name to everything to the left of the period and anything but "txt" to the right of the period.

Does that make sense?
Thx for your help!!

link

answered 2010-07-30 at 12:14:37

shersker's gravatar image

shersker

I didn't mean to rename the files. I meant you should change the value of $name from "FILE-1" to "FILE-"...  then the pattern should match. If you leave the 1 in there, then only files which have a 1 immediately following the dash.

If it's a matter of you having no control over $name, then why not create a new variable that only has what I mentioned above and pass that into list_dir().

link

answered 2010-07-30 at 12:28:18

kaufmed's gravatar image

kaufmed

Question PAQ'd and stored in the solution database.

link

answered 2010-07-30 at 13:44:32

ee_auto's gravatar image

ee_auto

Your answer
[hide preview]

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Tags:

×1

Asked: 07/30/2010 02:17

Seen: 247 times

Last updated: 12/17/2011 05:21