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.
Directory access
Collapse
X
-
Tags: None
-
Originally posted by homesick123How 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
let me know if this helped youCode:opendir(DIR1,"?home/xyz"); while(my $dir = readdir(DIR1)) { if( -d $dir) { print "$dir is subdirecotry\n"; } } closedir(DIR1);
regards,
deep -
For Windows in my mind comes a quick trick - command "dir"Originally posted by homesick123How 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.
A little example:
this example works only for windowsCode: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;
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
-
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,
JeffComment
-
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
-
File::Find already does this. Please note you have posted in a thread that is more than two years old.Originally posted by gaurav1086hi 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
Comment