Reading folder content and permissions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 31special
    New Member
    • Jul 2008
    • 4

    Reading folder content and permissions

    I want to tweak a small program I'm creating. It will read a directory and if it finds a file inside the directory with executable permissions it should do something.

    Code:
    #!/usr/bin/perl
    
    my $dir_to_process = "/tmp";
    opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
    
    while (my $name = readdir DIR) {
    
    next if $name =~ /^\./;
    
    
    if (-x $name)
    {
     print "Do something";
    }
    
    
    }
    
    closedir DIR;
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    Have a look at File::Find or File::Find::Wan ted either one will make this task simple.

    --Kevin

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      Originally posted by 31special
      I want to tweak a small program I'm creating. It will read a directory and if it finds a file inside the directory with executable permissions it should do something.

      Code:
      #!/usr/bin/perl
      
      my $dir_to_process = "/tmp";
      opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
      
      while (my $name = readdir DIR) {
      
      next if $name =~ /^\./;
      
      
      if (-x $name)
      {
       print "Do something";
      }
      
      
      }
      
      closedir DIR;

      OK, is there a problem with the code you have? Does it not work? Did you look up what the -x file test operator does? You also have this question on multiple forums, check them too.

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        Absolute filename is required for a file test like -x to pass. That means you should include the complete path of the file for the test to pass, not just the filename.
        Change the code to:
        Code:
        #!/usr/bin/perl
         
        my $dir_to_process = "/tmp";
        opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
         
        while (my $name = readdir DIR) {
        next if $name =~ /^\./;
        $name = $dir_to_process.'/'.$name;  ##append the directory path 
        if (-x $name)
        {
         print "Do something";
        }
         
        }
         
        closedir DIR;
        - Nithin

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          Absolute filename is required for a file test like -x to pass
          I suggested that on another forum where he posted this same question. The file test operator will work if the script is in the directory where the files are. If not he has to use the full path or chdir() into the directory where the files are.

          Comment

          • 31special
            New Member
            • Jul 2008
            • 4

            #6
            I uncluded the line $filename = "/tmp/".$name."\n "; to make the path absolute and is not detecting that is executable.

            The -x should find out if the file is executable then it will perform the action inside the the braces?

            Code:
            #!/usr/bin/perl
            
            my $dir_to_process = "/tmp";
            opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
            
            while (my $name = readdir DIR) {
            next if $name =~ /^\./;
            
            $filename =  "/tmp/".$name."\n";
            
            if (-x $filename)
            
            {
             print "Do something";
            }
            
            }

            Comment

            • numberwhun
              Recognized Expert Moderator Specialist
              • May 2007
              • 3467

              #7
              Originally posted by 31special
              I uncluded the line $filename = "/tmp/".$name."\n "; to make the path absolute and is not detecting that is executable.

              The -x should find out if the file is executable then it will perform the action inside the the braces?

              Code:
              #!/usr/bin/perl
              
              my $dir_to_process = "/tmp";
              opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
              
              while (my $name = readdir DIR) {
              next if $name =~ /^\./;
              
              $filename =  "/tmp/".$name."\n";
              
              if (-x $filename)
              
              {
               print "Do something";
              }
              
              }
              Why are you putting the "\n" newline in the path? In this context I believe it literally means "\n" and not a newline. You should remove it as you are setting the value of the variable $filename, not printing its value.

              Regards,

              Jeff

              Comment

              • 31special
                New Member
                • Jul 2008
                • 4

                #8
                The line print "Do something"; now prints 3 times but of the 3 files inside /tmp onle 1 of them is executable.

                Code:
                #!/usr/bin/perl
                
                my $dir_to_process = "/tmp";
                opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
                
                while (my $name = readdir DIR) {
                next if $name =~ /^\./;
                
                $filename =  "/tmp/".$name;
                
                if (-x $filename) {
                
                print "Do something";
                
                }
                
                }
                
                closedir DIR;

                Comment

                • numberwhun
                  Recognized Expert Moderator Specialist
                  • May 2007
                  • 3467

                  #9
                  Originally posted by 31special
                  The line print "Do something"; now prints 3 times but of the 3 files inside /tmp onle 1 of them is executable.

                  Code:
                  #!/usr/bin/perl
                  
                  my $dir_to_process = "/tmp";
                  opendir (DIR, $dir_to_process) || $dir or die "Cannot open $dir: $!";
                  
                  while (my $name = readdir DIR) {
                  next if $name =~ /^\./;
                  
                  $filename =  "/tmp/".$name;
                  
                  if (-x $filename) {
                  
                  print "Do something";
                  
                  }
                  
                  }
                  
                  closedir DIR;
                  Ah, interesting I haven't had anything to do the the -x test operator so I am guessing it was needed. Sorry about that.

                  Regards,

                  Jeff

                  Comment

                  • KevinADC
                    Recognized Expert Specialist
                    • Jan 2007
                    • 4092

                    #10
                    I posted several questions for you to answer but you either ignored them or felt they were not worth answering for some reason. No sense in me trying to help if the questions I post are going to be ignored.

                    Comment

                    • 31special
                      New Member
                      • Jul 2008
                      • 4

                      #11
                      I had to do it this because that test condition using the -x was not working for me.

                      Code:
                      #!/usr/bin/perl
                      my $dir_to_process = "/tmp";
                      my @files = split("\n", `find $dir_to_process -type f -perm -o+rx`);
                      foreach my $filename (@files) {
                      print "Do something on ($filename)\n";
                      }

                      Comment

                      Working...