I'm new here. I'm trying to put some code to copy files with specific patterns/keywords from a target location. I encountered a pattern matching (regexp) issue where the filename didn't contain the keyword but got returned as matching. The script is looking for a list of PROD in the setup file and dynamically changing the keyword by replacing the PROD into the filename. The keyword will be updated every loop.
File: setup_demo.txt
==============================
Expand|Select|Wrap|Line Numbers
- [COPY]
- PROD=DATA,GRAPH
- filename=$PROD_GROUP2_*
- filepath=C:\temp
In this setup, I'm expecting the script to search for DATA_GROUP2_* in the 1st loop and then GRAPH_GROUP2_* during the 2nd loop. The code worked on the 1st loop but failed on the subsequent loop.
Output on screen shows:
==============================
Expand|Select|Wrap|Line Numbers
- INFO : Searching File with Keyword [DATA_GROUP2_*]
- INFO : Found Local File [DATA_GROUP2_20080720.zip] matches DATA_GROUP2_*
- ..
- ..
- INFO : Searching File with Keyword [GRAPH_GROUP2_*]
- INFO : Found Local File [DATA_GROUP2_20080720.zip] matches GRAPH_GROUP2_*
This should not happen as the keyword should not match the filename: Found Local File [DATA_GROUP2_20080720.zip] matches GRAPH_GROUP2_*
I trimmed the script so that the error can be re-produced.
==============================
Expand|Select|Wrap|Line Numbers
- use strict;
- use warnings;
- # initialization
- my %setup = ();
- my $section = "";
- my $key = "";
- my $val = "";
- my $keyword = ();
- # read setup file
- print "INFO : Reading setup file \[setup.txt\]\n";
- open(SETUP,"<setup_demo.txt") or die "Cannot open setup.txt\n";
- while(<SETUP>) {
- chomp;
- next if (/^\s*$/);
- if (/\[(.*)\]/) {
- $section = $1;
- next;
- }
- ($key,$val) = split(/\=/);
- $setup{$section}{$key} = $val;
- }
- close(SETUP);
- my @PROD = split(',',$setup{'COPY'}{'PROD'});
- print "INFO : Total of " . ($#PROD + 1) . " Product(s)\n";
- for my $product (@PROD) {
- my @localfiles = ();
- my $localfile = "";
- my %varsetup = ();
- print "INFO : Processing Product \[$product\]\n";
- # Reset setup into varsetup to populate the product
- foreach $section (keys %setup) {
- foreach $key (keys (%{$setup{$section}})) {
- my $myval = $setup{$section}{$key};
- $varsetup{$section}{$key} = $myval;
- }
- }
- foreach $section (keys %varsetup) {
- foreach $key (keys (%{$varsetup{$section}})) {
- next if ($key =~ /^PROD$/i); # to avoid infinite loop
- $varsetup{$section}{$key} =~ s/\$PROD/$product/iog;
- }
- }
- # Determine filename matching keyword
- $keyword = $varsetup{'COPY'}{'filename'};
- $keyword =~ s/\*/\.*/g;
- print "INFO : Searching File with Keyword \[$varsetup{'COPY'}{'filename'}\]\n";
- # Read the local files - to avoid duplicate copy
- print "INFO : Reading keyword from LOCAL \[$varsetup{'COPY'}{'filepath'}\] files\n";
- opendir(DIR,"$varsetup{'COPY'}{'filepath'}") or die "Cannot open local directory $varsetup{'COPY'}{'filepath'}\n";
- @localfiles = readdir(DIR);
- closedir(DIR);
- foreach $localfile (@localfiles) {
- print "INFO : Checking $localfile\n";
- if ($localfile =~ /$keyword/io) {
- print "INFO : Found Local File \[$localfile\] matches $varsetup{'COPY'}{'filename'}\n";
- }
- }
- print "######## COMPLETED FOR $product ########\n";
- }
I have created 3 dummy zip files in the path for the debug.
Expand|Select|Wrap|Line Numbers
- c:\temp\DATA_GROUP2_20080720.zip
- c:\temp\GRAPH_GROUP2_20080720.zip
- c:\temp\TABLE_GROUP2_20080720.zip
Thanks.