Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to restrict the search to one level

Status
Not open for further replies.

sriramus

Technical User
Nov 11, 2005
8
GB
Hi All,

Can any one tell me how to restrict my search to one level. If i have a folder
master which contains 2 folders "pata" and "thread".
the folder pata contains 2 folders "telus" and "high".
the folder thread contains 1 folder "give" and "give" folder contains a file long.txt.

If my perl script is as below
-----------------------------------------------------
use File::Find;

# Print out all directories below current one.
chdir('D:\required');
find sub { print "$File::Find::name\n" if -d }, ".";

-----------------------------------------------------

i get the output as

./pata
./thread
./pata/telus
./pata/high
./thread/give

but i want my output to only display ./pata and ./thread
that is my search should end after searching the current folder it should not search the subfolder.

Any help is highly appreciated.

Thanks in advance.
 
If you don't want a recursive find, then File::Find is not for you. Try the readdir or glob built-ins.

If what you're really after is something fancier, like not a first-level query, but say "anything less than 3 levels deep" or "exactly two levels deep" then life gets more complicated.

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo
 
The examples of "anything less than 3 levels deep" or "exactly two levels deep" can easily be handled by File::Find::Rule
 
I agree with icrf, don't bother with file::find if all you want is to search to same level you are on. file::find is meant to search recursively through all sub directories, if thats not what you want then you are using the wrong tool. You can try file::find::rule but grep should work easy enough.

Code:
chdir('D:\required') or die "$!";
opendir(DIR,'.') or die "$!";
my @folders = grep (-d, readdir(DIR));
close(DIR);
print "$_\n" for @folders;
 
Many Thanks to icrf,ishnid,KevinADC.

with readdir i am able to read the folders but again i face another problem that is if my folder contains only 2 folders with in it my script output display

.
..
pata
thread

and assiging the array varible to a scalar valible display 4 whereas i want it to display 2 because i have only 2 folders, why is it considering . and ..

i there a way of ignoring this.

Please reply.

Thanks in advance.
 
Code:
my @folders = grep (-d && !/^\.+$/, readdir(DIR));
 
yeah, that's why I usually take the easy way out and go with glob, it's less efficient, but it generally takes fewer lines of code. No open or closing of a directory, no . or .. in the results.

- Andrew
Text::Highlight - A language-neutral syntax highlighting module in Perl
also on SourceForge including demo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top