473,396 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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
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
2 3245
nithinpes
410 Expert 256MB
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
4,059 Expert 2GB
File::Find will find all the subfolders of any top level folder.
Mar 7 '08 #3

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

Similar topics

5
by: Paul | last post by:
Hi, Using the code below I can extract the filename from a path but I would like to know how to get just the path too. So if the full path is "C:\A Long Time Ago\In A Galaxy\Far Far...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
2
by: Thomas A | last post by:
Hi, I write a string value to a textfile , if the string dont contains swedish Å,Ä and Ö , the file will be written in ANSI. So far so god but.... If the string contains Å or Ä or Ö ,the...
2
by: Sam | last post by:
Hi guys If I have to read/write a text file which has the following format, what would be the best way to read it? Should I readline method of the streamreader to a string variable and parse it...
19
by: Jerry M. Gartner | last post by:
Greetings: What is the best way to resolve paths within a document regardless of what path it is opened under? For example: I have x.php and it contains <img src="images...">, (amongst other...
4
by: tommaso.gastaldi | last post by:
Hi friends I was in the need to find a sort of "definitive" :) solution to transform a virtual path such as "~/MyDir/MyFile into a full web address. In particular I needed it * within web...
5
by: woolls01 | last post by:
I am using the following code Sub Ck() Dim strStartPath As String strStartPath = "d:\workpack\rra" ListFolder strStartPath End Sub
2
by: Man4ish | last post by:
Hi , How we can find the cyclic path and path between two nodes using Depth first search algorithm in Graph Data Structure. Can i get Pseudo code or code doing the same.I will be very much thankful...
2
by: lenniekuah | last post by:
Hi Good Guys, I need your help. Please help me. My office BA requested me to develop Window application to retrieve all the files from the SalesDepartment folder : F:\SalesDept and write the name...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.