By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,841 Members | 1,362 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,841 IT Pros & Developers. It's quick & easy.

To read the full subfolder path and write the path into textfile

P: 12
I am trying to read folder and subfolder and print their name into a text file.I am able to read the folder and its content and write it to the textfile but I am not able to read the content of the subfolder and write its content..FOR Eg : I have
folder1\text1
folder1\text2
folder1\folder2\text3
I am reading folder1 and it is writing text1,text2 and folder2 in another textfile.I need to print the total structure of the subfolder ..like folder2\text3..not just folder2.my code is as following
Expand|Select|Wrap|Line Numbers
  1. #!C:\home\bin\perl -w
  2.  
  3. open (MyHandle,">C\:\\ravi\\ravilist.txt") or die("File Open Error");
  4. opendir MYDIR, "C\:\\folder1";
  5. @contents = grep !/^\.\.?$/, readdir MYDIR;
  6. closedir MYDIR;
  7. foreach $listitem ( @contents )
  8. {
  9.   print MyHandle $listitem;
  10.   print MyHandle "\n";
  11. }
  12.  
Mar 7 '08 #1
Share this Question
Share on Google+
2 Replies


nithinpes
Expert 100+
P: 410
You are reading only from the main directory, if you want to read contents of subfolder you have to open them using opendir() again:

Expand|Select|Wrap|Line Numbers
  1. foreach $listitem(@contents)
  2. {
  3.   if(-d "C:\\folder1\\$listitem") { #check for directories
  4.   opendir TEMPDIR, "C:\\folder1\\$listitem" or die "failed:$!";
  5.   my @subcontents = grep !/^\.\.?$/, readdir TEMPDIR;
  6.   close TEMPDIR;
  7.   print MyHandle "$listitem\\$_ \n" foreach(@subcontents);   
  8.    }
  9.  else {
  10.    print MyHandle "$listitem\n";
  11. }
  12. }
  13.  
Mar 7 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
File::Find will find all the subfolders of any top level folder.
Mar 7 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.