vickycanada has asked for the wisdom of the Perl Monks concerning the following question:

How Do I check multiple filenames in one directory with different strings. I need to make sure below three files exist in one directory before I send them to another application, Also, I want to create a new file once above three files are sent. Below is my *.pl file linked with my *.wsend template( This is part of file transfer automation)
#!/usr/bin/perl $inputFileName = $ARGV[0]; $inputFilewithPath = $ARGV[1]; $destinationFileName = ""; $inputFileSubstring = substr $inputFileName, 0, 6; $inputFileSubstring1 = substr $inputFileName, 0, 8; $inputFileSubstring2 = substr $inputFileName, 0, 7; $destinationFilePath = "\\\\sharepath\\Test\\customoutput\\Test1\\"; $triggerFilePath = "\\\\sharepath\\cpa\\customoutput\\hubidp\\"; $triggerFileName = ""; $returnVal = 0; if( $inputFileSubstring eq alphat.*\.zip$/i && $inputFileSubstring1 eq + numerict.*\.zip$/i && $inputFileSubstring2 eq updatet.*\.zip$/i ) { $destinationFileName = sprintf("%s%s%s", $destinationFilePath, $in +putFileName); $triggerFileName = sprintf("%s%s%s", $triggerFilePath, "cpa_trigge +r.done"); } else{ printf "WARNING: EXPECTED FILES NOT FOUND. INVESTIGATION REQUI +RED"; exit ; } $returnVal = system("ftmscmd /tcp /send /file /node:XXX /DT:E /CrLf:Y +/ppa1:S,L,COMMAND,\"echo > $triggerFileName\" /TC:3 $inputFilewithPat +h $destinationFileName");
Appreciate your help.
  • Comment on Check multiple file names exist in directory and create another trigger file after existing files processed
  • Download Code

Replies are listed 'Best First'.
Re: Check multiple file names exist in directory and create another trigger file after existing files processed
by vickycanada (Novice) on Nov 21, 2017 at 21:29 UTC
    Three filenames are :
    alphat.XXX.zip numerict.XXX.zip updatet.XXX.zip
    note:XXX could be any numeric value

      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"; } }

        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!!!!