473,473 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Opening the files of a particular directory

2 New Member
Hello:

Iam triying to open all the files that are located in one particular directory using a perl script.
this is how I tryed :

Expand|Select|Wrap|Line Numbers
  1. unless (opendir (DIR,"$dirname")){
  2. print "Can't open the directory $dirname \n";
  3. exit;
  4. }
  5.  
  6. while ($file = readdir(DIR)){
  7. next if $file =~ /^\.\.?$/;
  8. foreach ($file){
  9. #si no se puede abrir, avisarlo y salir
  10. unless(open (POSFILE,"+< \ Cr2L\$file")){
  11. print "Can't open the file $file \n";
  12. next;
  13. }
the script failed because it is unable to open the files.

Does someone know what i must do?

Thank you very much
Patricia Carreira
Apr 2 '07 #1
5 2319
rickumali
20 New Member
I keyed in your program, and observed that the likely culprit is your file name escaping. On Windows, I had to use something like this:

$dirname = "C:\\cygwin\\home\\rick\\perl\\test";

and:

unless(open (POSFILE,"+< $dirname\\$file")){

Notice the use of the "\\" to escape the "backslash". You might want to sprinkle some printfs or use the Perl debugger to examine your dirname and filename.
Apr 2 '07 #2
KevinADC
4,059 Recognized Expert Specialist
You can use forward slashes with windows directory paths, this avoids the meta character interpolation that is a problem with perl when using backslashes:

Expand|Select|Wrap|Line Numbers
  1. unless(open (POSFILE,"+</Cr2L/$file")){
Apr 2 '07 #3
kershell
11 New Member
Hello:

Iam triying to open all the files that are located in one particular directory using a perl script.
this is how I tryed :

unless (opendir (DIR,"$dirname")){
print "Can't open the directory $dirname \n";
exit;
}

while ($file = readdir(DIR)){
next if $file =~ /^\.\.?$/;
foreach ($file){
#si no se puede abrir, avisarlo y salir
unless(open (POSFILE,"+< \ Cr2L\$file")){
print "Can't open the file $file \n";
next;
}
the script failed because it is unable to open the files.

Does someone know what i must do?

Thank you very much
Patricia Carreira
Have a look at the File::Find module. It is a part of Perl installation which means nothing needs to be installed.
Apr 4 '07 #4
nithinpes
410 Recognized Expert Contributor
The following script worked for me in reading each file from a directory:
Expand|Select|Wrap|Line Numbers
  1. $dirpath="C:\\Temp\\Directory\\";
  2. opendir(IN,"$dirpath") or die "opening directory failed:$!";
  3. while (defined ($file = readdir(IN)) ) {
  4. @files=split / /,$file;
  5. foreach $filename (@files)
  6. {
  7. open (INPUT, "$dirpath".$filename) or warn "Can't open the file $filename\n:$!";
  8. until (eof(INPUT)) {
  9. chomp($line = <INPUT>);
  10.  # print contents of the file or do anything
  11.               }
  12. }
  13. }
  14.  
  15.  closedir(IN);
  16.  
Dec 19 '07 #5
KevinADC
4,059 Recognized Expert Specialist
thats good, but it has nothing to do with the original question, which is now 8 months old.
Dec 19 '07 #6

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

Similar topics

7
by: Fred H | last post by:
Hi I've tried to find out if and how I can open directories in standard C++, the same way I can open files. To my great suprise, I haven't managed to find out how this is done! I'm using MS...
1
by: Alfons | last post by:
Hello, I have build a program that can do file transferring between a Windows XP computer and a DOS computer via a serial port. The Windows program I have build in C++ with Visual Studio 6.0....
5
by: Bas Hendriks | last post by:
Has anyone any idea how asp.net find it's files back after compiling them to the temporary asp.net directory? I found on numerous webpages that the directorynames are chosen random but cannot find...
3
by: Nick | last post by:
Is it possible to read a list of files from a specified directory using VB.net We have company intranet and I have created a page that displays photos from different events. I have coded a page...
18
by: UJ | last post by:
Folks, We provide custom content for our customers. Currently we put the files on our server and people have a program we provide that will download the files. These files are usually SWF, HTML or...
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...
8
by: theCancerus | last post by:
Hi All, I am not sure if this is the right place to ask this question but i am very sure you may have faced this problem, i have already found some post related to this but not the answer i am...
5
by: muwie | last post by:
Hello, I was browsing to see if I could find something similair to my problem. But I couldn't find anything.. I have this script that counts every word in a file. And then also says how many...
15
by: Andy B | last post by:
I need to count files in a certain directory that has the string -Contract Template.xml at the end of it. How would I do this?
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.