Heyhey!

Perl-virgin here, be gentle, pretty please with sugar on top. + English is not my native tongue, but i pride myself in my capabilities (in written English anyways, I sound a bit like a mix of a Frenchman and a German trying to speak American).

I have a question about the very first script I'm trying to write.

I just took an existing script and with pretty much no experience with scripting tried to understand what was in there and now trying to re-write it with the help of the Google-Gods. Needless to say, I need some better (more specific/efficient) help. Please consider the following 'code':

#!perl -w # # Archiver for processed EDI messages # - move all messages to existing subfolder *\yyyy\mm\dd use strict; use warnings; use Time::Piece; use File::Find; use File::Copy; # no idea if I need all of these or not my $base = 'D:\Some\Specific\Folder\\'; my @time = localtime; my $mday = $time[3]; my $month = $time[4] + 1; $month = sprintf("%02d", $month); my $year = $time[5]+1900; my $from = $base.*\.'.txt'; # THE PROBLEM print ($from); # just a check to see what is used as $from, if anything # will obviously be left out when the rest works my $to = $base.$year."\\".$month."\\".$mday."\\"; # this folder already exists move($from,$to) or die $!; # might also be (part of) the problem print "moved ".$from." to ".$to; # a check to see what has been moved, if anything exit 0;

I know this lacks a bunch of things (like writing to a logfile), don't bother summing them up, I won't bother adding them anyway. Right now at least, might do when I get better at this stuff and have some left-over time on my hands. The only thing I need it to do on a daily basis is move a number of .txt-files (could be 500, could be 10,000 files) from folder 'D:\Some\Specific\Folder\' to the subfolder 'D:\Some\Specific\Folder\yyyy\mm\dd\' (obviously for today, this would be 'D:\Some\Specific\Folder\2017\05\23\'). As commented in the code, this subfolder will already exist when needed.

The big(gest) problem in this code is the attempt at using a wildcard, I just can not get my head around it. I know *\ will not work here, but I can't figure out what will. The *.txt files can have several different kinds of formatting. * always starts with a set code of 3 letters and numbers, but there are more than 10 different set codes; each set code has a different build for what comes between the set code and the '.txt'. Defining every one of these possibilities seems a bit redundant.

The script simply needs to check: Is this a .txt file? Yes: move to given subfolder! No: do not touch and move on! This looks like a quest easily achieved. But I have read some pieces about Perl that have made steam come out of my ears and stuff that has made me see the light (or so I thought until I tried it).

I have tried a large number of combinations with $ . / \ ' in my attempt at making "my $from = [...]" work - all based on stuff I have found via named websearch, none has cleared a path. Please don't make me sum them all up, I have no idea anymore, there have been too many attempts and I have not saved the losing versions apart from the one given above.

I'm sure this will be resolved in a jiffy by the first person who bothers to read this whole thing. Will you be that person?


In reply to Wildcards for a wide array of strings? by zarath

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.