473,394 Members | 1,642 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,394 software developers and data experts.

Reading Files From Directories

24
hi guys,
iam doing a project on linguistic for which i have to extract the news from a local web site,i have done so and later i have saved all the files in a directory,now i want all the files in a single file so that iam trying to read the file from the directory,which iam not able to...
here is the code
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $dir = "D:\\perl program\\perl\\bin\\malar";
  3. opendir DH,$dir;
  4. @files = readdir(DH);
  5. foreach $files(@files){
  6.     print "$files\n";
  7.  
here iam getting the names of the files which is in the dir,but i want to read the files.
Dec 10 '07 #1
5 1938
hi guys,
iam doing a project on linguistic for which i have to extract the news from a local web site,i have done so and later i have saved all the files in a directory,now i want all the files in a single file so that iam trying to read the file from the directory,which iam not able to...
here is the code
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $dir = "D:\\perl program\\perl\\bin\\malar";
  3. opendir DH,$dir;
  4. @files = readdir(DH);
  5. foreach $files(@files){
  6.     print "$files\n";
  7.  
here iam getting the names of the files which is in the dir,but i want to read the files.
Can you clearly tell abt your questions..
do you want to open and read all files in a directory one by one or something else?

Regards,
Mohan.
Dec 10 '07 #2
mehj123
55
hi guys,
iam doing a project on linguistic for which i have to extract the news from a local web site,i have done so and later i have saved all the files in a directory,now i want all the files in a single file so that iam trying to read the file from the directory,which iam not able to...
here is the code
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. $dir = "D:\\perl program\\perl\\bin\\malar";
  3. opendir DH,$dir;
  4. @files = readdir(DH);
  5. foreach $files(@files){
  6.     print "$files\n";
  7.  
here iam getting the names of the files which is in the dir,but i want to read the files.
Hi...

After getting the names of the files of the directory, you would have to open all these files one by one and read the contents... you can copy these contents to another file...

Try doing something like this

Expand|Select|Wrap|Line Numbers
  1. #open the temporary file in append mode
  2. open (OUT,">>temp.txt") || die ("Cant open");
  3. foreach $files(@files)
  4. {
  5. #open the files one by one
  6.  open(FH,$files)|| die("cannot open the file");
  7. # read the contents
  8.  @new_array = <FH>;
  9. # store in the temporary file
  10.  print OUT @new_array;
  11. close(FH);
  12. }
  13. close(OUT);
  14.  
Dec 10 '07 #3
humaid
24
Can you clearly tell abt your questions..
do you want to open and read all files in a directory one by one or something else?

Regards,
Mohan.
yes i want to open a dir and read all files at a time and not one by one.
Dec 10 '07 #4
numberwhun
3,509 Expert Mod 2GB
yes i want to open a dir and read all files at a time and not one by one.

Then, as previously stated, you would have to take a directory listing, parse it to get JUST the file names (with a regex), put the list into an array and use that to cycle through all the files, opening each one in turn and doing what you need to do.

Regards,

Jeff
Dec 10 '07 #5
KevinADC
4,059 Expert 2GB
yes i want to open a dir and read all files at a time and not one by one.
You have to read the files one at a time. It sounds like what you want to do is concatenate/append all the files into one big file. But if you're use readdir() to get a list of the files they will be in whatever order the system stores them in. That means they might not print in the correct order when concatenated together, if that is a concern.

Since you are on windows you can probably use the DOS command "copy" to append all the files together or use perl or a perl module, like File::Spec, to concatenate the files.
Dec 11 '07 #6

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

Similar topics

5
by: Tum | last post by:
Hi folks, I've been trying to make a decision and it's driving me crazy. Is a directory a file or is a directory NOT a file but a node? Should I have A)
6
by: bart_nessux | last post by:
Hello, I have some Macbinary files on a PC. I want to recursively read these files and remove the first 128 bytes of the files if they contain the macbinary header info. I know how to read...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
3
by: Jim | last post by:
Is it possible to read the Temporary Internet Files folder using C#? I'm messing with FileIO (newbie here) and everything seems to work fine until I try to read the list of files in this Temporary...
4
by: Jerry | last post by:
I'm having just a bit of trouble wrapping my brain around the task of working with folders that are above the site's root folder. I let users upload photos (.jpg/.gif files) which can...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
2
by: Alan Bak | last post by:
HI I am running Active Perl on a Windows XP machine. I am hoping to get some advise on a strategy to monitor and copy files that are arriving in a directory and need to be copied to a second...
2
by: supra91 | last post by:
Hi, I have to read files from the parent directory and delete those files. This parent directory contains logs files. This directory also contains some sub directories and I do not want to delete...
4
by: Laharl | last post by:
My Operating Systems professor has assigned homework that basically boils down to implementing ls -lra, but with a different output format. In other words, list the files and subdirectories (and a...
2
by: koti688 | last post by:
I have a directory named "X" which has many sub directorys "Y","Z","W" and many files about 20 files. and these sub directories also contain some more sub directories and files extra check the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.