zarath has asked for the wisdom of the Perl Monks concerning the following question:
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.
#!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?
|
---|