in reply to Check multiple file names exist in directory and create another trigger file after existing files processed

Three filenames are :
alphat.XXX.zip numerict.XXX.zip updatet.XXX.zip
note:XXX could be any numeric value
  • Comment on Re: Check multiple file names exist in directory and create another trigger file after existing files processed
  • Download Code

Replies are listed 'Best First'.
Re^2: Check multiple file names exist in directory and create another trigger file after existing files processed
by roboticus (Chancellor) on Nov 21, 2017 at 21:46 UTC

    vickycanada:

    Generally, you'd just check whether they all exist, then do your thing. For example:

    my $XXX = ... from somewhere ...; if (-e "$srcDir/alphat.$XXX.zip" and -e "$srcDir/numerict.$XXX.zip" an +d -e "$srcDir/updatet.$XXX.zip") { # all files are found, so go do some work } else { # Not all files are present yet. }

    Generally, that's the easy part. For a real task, files won't always show up simultaneously. So you'd have to scan your input directory for files and extract a list of XXX values from them. You can use that list in a for loop with the above code to process any group where all three XXX parts exist.

    The harder bit comes where you need to allow some time to pass before declaring an error and causing someone to go in and debug the situation. To do so, I'd suggest tracking the current time when you find a new file, and then in your chunk where you don't have all files present, you can compare the current time with the time that the file was discovered, and if enough time has elapsed, then emit a warning with the files that are missing. You'll also likely want to stash away those files so they don't keep warning you. But that's the fun of making a production system.

    Update: Changed -E to -e (thanks for catching that, choroba!)

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      What I've done that covers many of my use cases, is:

      1. Watch & wait for the first type of file to appear.
      2. Sleep for a few seconds to a few minutes depending on the task
      3. Proceed to verify the other matching files exist, and carry on with the task, else loop back to 1

      Sometimes the verification is a check to see if the file has grown larger; if so, then it goes back to sleep repeatedly until the download/copy is finished.

      For checking the names, you may want to use a regex to capture the relevant part of the filename, and then look for specifically named files using that.

      For example,

      if ($file =~ /alphat_(.*)\.zip) { my $key = $1; # whatever was between alphat_ and the .zip bits. if (-f "numerict_${key}.zip && -f "updatet_${key}.zip") { print "Caught 'em all!\n"; } }

        Hi, Thank you for the advise. I did make some changes and posted an update. Could you please take a look as well. Appreciate your help!
      Thank you. Yes I'll try to incorporate that in the script and see how it turns up. Thank you again for the quick response! BTW, If I need to compare the exact files in the above if statement, where xxx could be of any values(numeric or string). Is there way to do that? Say If I need to compare the filename which starts with ABC and read the rest half "as is" in the if statement, how do I do that? Sorry if my questions sound so dumb (lol), I am just trying to pick this knowledge and be a perl monks like you!!!!

        vickycanada:

        Sorry, but I can't seem to figure out what you're asking.

        So I'll just throw something out and see if it helps... ;^)

        # Fetch all .zip files in the incoming directory my @zipfiles = glob "$srcDir/*.zip"; # Scan through the files to find out which XXX bits may exist my %XXXlist; for my $file (@zipfiles) { if ($file =~ /(alphat|numerict|updatet).(.*).zip/) { # OK one of our desired prefixes match, second capture group is +a valid XXX value $XXXlist{$2}++; } } # Now we can go on and see what we can process for my $XXX (keys %XXXlist) { if (-E "$srcDir/alphat.$XXX.zip" and -E "$srcDir/numerict.$XXX.zip" and -E "$srcDir/updatet.$XXX.zip") { # all files are found, so go do some work } else { # Not all files are present yet. } }

        Did that help? If not, please try to expand your question a bit so I can figure out what you mean.

        Note: I normally try to test my code before posting it, but I definitely didn't even try this time, so you may have to fiddle with it a bit.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.