Match for today's date doesn't match correctly











up vote
0
down vote

favorite












i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv









share|improve this question




















  • 2




    All files meet the criteria because you already filtered them with grep.
    – toolic
    Nov 22 at 16:13

















up vote
0
down vote

favorite












i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv









share|improve this question




















  • 2




    All files meet the criteria because you already filtered them with grep.
    – toolic
    Nov 22 at 16:13















up vote
0
down vote

favorite









up vote
0
down vote

favorite











i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv









share|improve this question















i'm writing a basic Perl script to check if there're files in certain directories their names contain today's date, the script works fine when there are files in the directory and returns TRUE, but doesn't return FALSE when no files withe the given criteria exist



the script is as below:



#!/usr/bin/perl -w
use POSIX qw(strftime);
my $datestring = strftime "%Y%m%d", localtime;
opendir(DIR, 'C:Perl');
@files = grep (/$datestring/,readdir(DIR));
closedir(DIR);
foreach $file (@files) {
my $count = () = $file =~ /$datestring/;
if ($count > 0) {
print ("TRUE");
}
else
{
print ("FALSE");
}
}


the files names are like



export_opportunities_20181111-040005_20181124-040010.csv
export_opportunities_20181111-040005_20181122-040010.csv






perl date






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 16:19









choroba

153k14139201




153k14139201










asked Nov 22 at 16:10









Kareem Hamed

1114




1114








  • 2




    All files meet the criteria because you already filtered them with grep.
    – toolic
    Nov 22 at 16:13
















  • 2




    All files meet the criteria because you already filtered them with grep.
    – toolic
    Nov 22 at 16:13










2




2




All files meet the criteria because you already filtered them with grep.
– toolic
Nov 22 at 16:13






All files meet the criteria because you already filtered them with grep.
– toolic
Nov 22 at 16:13














2 Answers
2






active

oldest

votes

















up vote
1
down vote













You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



@files = grep (/$datestring/,readdir(DIR));
# this is more commonly written
# @files = grep {/$datestring/} readdir(DIR);

print @files > 0 ? "TRUE" : "FALSE";
# or
print scalar(@files)." files matched $datestring";


See also List::Util.



use v5.10;    
use List::Util qw<all any none>;
###
@files = readdir(DIR);
say 'all match' if all {/$datestring/} @files;
say 'at least one match' if any {/$datestring/} @files;
say 'no matches' if none {/$datestring/} @files;





share|improve this answer




























    up vote
    1
    down vote













    As commented by toolic, when you are already removing non-matching files here :



    @files = grep (/$datestring/,readdir(DIR));


    Hence your for loop will never see non-matching files.



    Here are a few other comments on your code :




    • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


    • always check the return code of system calls such as opendir


    • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


    • you don't need to use parentheses around argument to the built-in print function



    Here is a cleaner version of your code :



    #!/usr/bin/perl -w
    use strict;
    use warnings;
    use POSIX qw(strftime);

    my $datestring = strftime "%Y%m%d", localtime;
    opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
    my @files = readdir(DIR);
    closedir(DIR);

    foreach my $file (@files) {
    if ($file =~ /$datestring/) {
    print "TRUEn";
    } else {
    print "FALSEn";
    }
    }





    share|improve this answer























    • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches
      – Kareem Hamed
      Nov 23 at 11:38










    • @Kareem ok I edited the answer to produce the desired output
      – GMB
      Nov 24 at 9:52











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434758%2fmatch-for-todays-date-doesnt-match-correctly%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    1
    down vote













    You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



    @files = grep (/$datestring/,readdir(DIR));
    # this is more commonly written
    # @files = grep {/$datestring/} readdir(DIR);

    print @files > 0 ? "TRUE" : "FALSE";
    # or
    print scalar(@files)." files matched $datestring";


    See also List::Util.



    use v5.10;    
    use List::Util qw<all any none>;
    ###
    @files = readdir(DIR);
    say 'all match' if all {/$datestring/} @files;
    say 'at least one match' if any {/$datestring/} @files;
    say 'no matches' if none {/$datestring/} @files;





    share|improve this answer

























      up vote
      1
      down vote













      You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



      @files = grep (/$datestring/,readdir(DIR));
      # this is more commonly written
      # @files = grep {/$datestring/} readdir(DIR);

      print @files > 0 ? "TRUE" : "FALSE";
      # or
      print scalar(@files)." files matched $datestring";


      See also List::Util.



      use v5.10;    
      use List::Util qw<all any none>;
      ###
      @files = readdir(DIR);
      say 'all match' if all {/$datestring/} @files;
      say 'at least one match' if any {/$datestring/} @files;
      say 'no matches' if none {/$datestring/} @files;





      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote









        You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



        @files = grep (/$datestring/,readdir(DIR));
        # this is more commonly written
        # @files = grep {/$datestring/} readdir(DIR);

        print @files > 0 ? "TRUE" : "FALSE";
        # or
        print scalar(@files)." files matched $datestring";


        See also List::Util.



        use v5.10;    
        use List::Util qw<all any none>;
        ###
        @files = readdir(DIR);
        say 'all match' if all {/$datestring/} @files;
        say 'at least one match' if any {/$datestring/} @files;
        say 'no matches' if none {/$datestring/} @files;





        share|improve this answer












        You can use @files in scalar context to tell if grep returned any matches from readdir. In scalar context, @files is the number of elements in the array.



        @files = grep (/$datestring/,readdir(DIR));
        # this is more commonly written
        # @files = grep {/$datestring/} readdir(DIR);

        print @files > 0 ? "TRUE" : "FALSE";
        # or
        print scalar(@files)." files matched $datestring";


        See also List::Util.



        use v5.10;    
        use List::Util qw<all any none>;
        ###
        @files = readdir(DIR);
        say 'all match' if all {/$datestring/} @files;
        say 'at least one match' if any {/$datestring/} @files;
        say 'no matches' if none {/$datestring/} @files;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 at 18:02









        beasy

        923411




        923411
























            up vote
            1
            down vote













            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }





            share|improve this answer























            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches
              – Kareem Hamed
              Nov 23 at 11:38










            • @Kareem ok I edited the answer to produce the desired output
              – GMB
              Nov 24 at 9:52















            up vote
            1
            down vote













            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }





            share|improve this answer























            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches
              – Kareem Hamed
              Nov 23 at 11:38










            • @Kareem ok I edited the answer to produce the desired output
              – GMB
              Nov 24 at 9:52













            up vote
            1
            down vote










            up vote
            1
            down vote









            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }





            share|improve this answer














            As commented by toolic, when you are already removing non-matching files here :



            @files = grep (/$datestring/,readdir(DIR));


            Hence your for loop will never see non-matching files.



            Here are a few other comments on your code :




            • always use strict and use warnings (there are several variables in your code snippet which are not properly declared)


            • always check the return code of system calls such as opendir


            • you can use the smartmatch operator in boolean context instead of assigning to the $count variable


            • you don't need to use parentheses around argument to the built-in print function



            Here is a cleaner version of your code :



            #!/usr/bin/perl -w
            use strict;
            use warnings;
            use POSIX qw(strftime);

            my $datestring = strftime "%Y%m%d", localtime;
            opendir(DIR, 'C:Perl') or die "cannot open dir : $!";
            my @files = readdir(DIR);
            closedir(DIR);

            foreach my $file (@files) {
            if ($file =~ /$datestring/) {
            print "TRUEn";
            } else {
            print "FALSEn";
            }
            }






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 24 at 9:51

























            answered Nov 22 at 21:40









            GMB

            2,028114




            2,028114












            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches
              – Kareem Hamed
              Nov 23 at 11:38










            • @Kareem ok I edited the answer to produce the desired output
              – GMB
              Nov 24 at 9:52


















            • Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches
              – Kareem Hamed
              Nov 23 at 11:38










            • @Kareem ok I edited the answer to produce the desired output
              – GMB
              Nov 24 at 9:52
















            Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches
            – Kareem Hamed
            Nov 23 at 11:38




            Thanks fort the feedback, the output really matches for the result but it returns many trues along with each file name, i need only one true if there's one file match and one false if no file matches
            – Kareem Hamed
            Nov 23 at 11:38












            @Kareem ok I edited the answer to produce the desired output
            – GMB
            Nov 24 at 9:52




            @Kareem ok I edited the answer to produce the desired output
            – GMB
            Nov 24 at 9:52


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53434758%2fmatch-for-todays-date-doesnt-match-correctly%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            Alexandru Averescu

            Trompette piccolo