Directory access

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • homesick123
    New Member
    • Oct 2006
    • 8

    Directory access

    How can I open a directory and check if the content inside has another directory .If there is a directory, I want it to be opened again and again checked for any other directory.This process has to be repeated till it reaches a folder called "STAGING". Then the script generates the absolute path till the parent directory of STAGING.
  • deep022in
    New Member
    • Sep 2006
    • 23

    #2
    Originally posted by homesick123
    How can I open a directory and check if the content inside has another directory .If there is a directory, I want it to be opened again and again checked for any other directory.This process has to be repeated till it reaches a folder called "STAGING". Then the script generates the absolute path till the parent directory of STAGING.

    ########


    I have partial solution to your problem

    you can verify whether the directory contains subdirectory by the following code

    Code:
    opendir(DIR1,"?home/xyz");
    while(my $dir = readdir(DIR1))
    {
    if( -d $dir)
    {
    print "$dir is subdirecotry\n";
    }
    }
    closedir(DIR1);
    let me know if this helped you

    regards,
    deep
    Last edited by numberwhun; Oct 14 '08, 02:16 PM. Reason: Please use code tags

    Comment

    • heto
      New Member
      • Oct 2008
      • 1

      #3
      Originally posted by homesick123
      How can I open a directory and check if the content inside has another directory .If there is a directory, I want it to be opened again and again checked for any other directory.This process has to be repeated till it reaches a folder called "STAGING". Then the script generates the absolute path till the parent directory of STAGING.
      For Windows in my mind comes a quick trick - command "dir"

      A little example:
      Code:
      use strict;
      #define variables
      my ($path) = @ARGV;
      my $absolutePath; 
      
      #open virtual file -> see dir /?
      open(DIR, "dir $path /A:D /S /B|");
      
      #until end of file do something
      while(<DIR>){
       if(/STAGING/){
       $absolutePath = $_; #set your needed value
       last; #end the while, we found what we want
       }
      }
      
      #print result
      print $absoultePath;
      this example works only for windows

      for linux is command "ls", but i don't have too much experience with unix/linux systems.

      Other way is to use a recursive function with opendir, chdir and verify if the name is what we are looking for. The problem there is that we look into a matrix and you have to determinate how to look(depth or breadth), it's a little more complicate. If this example doesn't suite you, and you didn't get your answer yet, please let a note and i will try to find time to write the recursive example for you.

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        deep and heto,

        Both of you need to please remember to use code tags to enclose ANY code that you post into the forums. If you don't do it, the moderators, like myself, must come in after you and clean up. We really prefer it if you add them during your posting. If you don't know how to use them, take a look at the "Reply Guidelines" box to the right of your reply window when replying.

        Regards,

        Jeff

        Comment

        • KevinADC
          Recognized Expert Specialist
          • Jan 2007
          • 4092

          #5
          And you all need to check the thread dates:

          October 5th, 2006

          3 months is an old thread, 2+ years is ancient history.

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            Well that is one less unanswered question. :-)

            --Kevin

            Comment

            • KevinADC
              Recognized Expert Specialist
              • Jan 2007
              • 4092

              #7
              Originally posted by eWish
              Well that is one less unanswered question. :-)

              --Kevin
              The better perl solution is to use File::Find and exit() when the file/folder is found.

              Comment

              • gaurav1086
                New Member
                • Oct 2008
                • 4

                #8
                hi ther.e.. i am a newcomer to perl so all i can give is an algorithm for ur solution if u like implement it. and let me know if it helped.
                the algo goes like this____
                you have to go on opening directories recursively.,wi th the exit condition.that (until a directory with a particular name is reached.)
                and then do the backtracking.. to see.. if there.. are more files in the directory..
                all you have to imagine is that.. its like a tree structure that..contains parents(of any subinstance as the parent directory) and its children as the subdirectories.
                i think its going to work.. plz let me know if it does..

                Comment

                • KevinADC
                  Recognized Expert Specialist
                  • Jan 2007
                  • 4092

                  #9
                  Originally posted by gaurav1086
                  hi ther.e.. i am a newcomer to perl so all i can give is an algorithm for ur solution if u like implement it. and let me know if it helped.
                  the algo goes like this____
                  you have to go on opening directories recursively.,wi th the exit condition.that (until a directory with a particular name is reached.)
                  and then do the backtracking.. to see.. if there.. are more files in the directory..
                  all you have to imagine is that.. its like a tree structure that..contains parents(of any subinstance as the parent directory) and its children as the subdirectories.
                  i think its going to work.. plz let me know if it does..
                  File::Find already does this. Please note you have posted in a thread that is more than two years old.

                  Comment

                  Working...