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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RaviRajhulk
    New Member
    • Feb 2008
    • 12

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

    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
    [code=perl]
    #!C:\home\bin\p erl -w

    open (MyHandle,">C\: \\ravi\\ravilis t.txt") or die("File Open Error");
    opendir MYDIR, "C\:\\folde r1";
    @contents = grep !/^\.\.?$/, readdir MYDIR;
    closedir MYDIR;
    foreach $listitem ( @contents )
    {
    print MyHandle $listitem;
    print MyHandle "\n";
    }
    [/code]
    Last edited by numberwhun; Mar 7 '08, 01:14 PM. Reason: add code tags
  • nithinpes
    Recognized Expert Contributor
    • Dec 2007
    • 410

    #2
    You are reading only from the main directory, if you want to read contents of subfolder you have to open them using opendir() again:

    Code:
    foreach $listitem(@contents)
    {
      if(-d "C:\\folder1\\$listitem") { #check for directories
      opendir TEMPDIR, "C:\\folder1\\$listitem" or die "failed:$!";
      my @subcontents = grep !/^\.\.?$/, readdir TEMPDIR;
      close TEMPDIR;
      print MyHandle "$listitem\\$_ \n" foreach(@subcontents);   
       }
     else {
       print MyHandle "$listitem\n";
    }
    }

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      File::Find will find all the subfolders of any top level folder.

      Comment

      Working...