Stupidity on my side - readdir(), chdir() ..
Stupidity on my side - readdir(), chdir() ..
am 28.03.2011 21:36:46 von Claus Kick
Hello all,
I am writing a small tool just to update contents of a couple of
directories. The directories are just stored in a flat text file like
this:
d:\updates\Imports\MCM_Import_CSG\Config
d:\updates\Imports\MCM_Import_CSV
[...]
f:\sap_import_prod\bin
I figured, I just leave windows notation and then switch to proper
notation before doing anything.
Well, I did not get very far.
If I do the following
use Data::Dumper;
my $list = "importe.properties";
my @directories;
my $file_types = ("bat|cfg|cmd|properties|pl|xml|xsl|xslt");
open (FILE, "<".$list) or die "cannot open file";
@directories = ;
close (FILE);
my @files;
foreach my $dir (@directories)
{
@files = undef;
chomp;
$dir =~ s/\\/\//g;
#if ($dir =~ /[A-Z]{1}:(.+)/)
#{
# $dir = $1;
#}
print "DIR: ".$dir."\n";
opendir(DIR, $dir) or die "cannot open directory: $dir - $!";
@files = readdir(DIR);
closedir(DIR);
print Dumper \@files;
}
the script dies at the die with - No such file or directory at
importe.pl line 50.
the directories are there, I manually copied each path from the
windows explorer address bar.
If I leave away the drive letter as in the commented out part, then I
do not find the files on the other disk.
I also tried using chdir($dir) and then chdir($Bin).
This only works for the last entry in the list, for the rest, the
script insists that cwd is still $Bin.
What am I doing wrong?
(This is perl, v5.8.8 built for MSWin32-x86-multi-thread) on Windows 2003.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Stupidity on my side - readdir(), chdir() ..
am 28.03.2011 21:57:47 von Claus Kick
I should mention that it fails at the second die at "cannot open
directory: $dir - $!".
2011/3/28 Claus Kick :
> Hello all,
>
> I am writing a small tool just to update contents of a couple of
> directories. The directories are just stored in a flat text file like
> this:
>
> d:\updates\Imports\MCM_Import_CSG\Config
> d:\updates\Imports\MCM_Import_CSV
> [...]
> f:\sap_import_prod\bin
>
> I figured, I just leave windows notation and then switch to proper
> notation before doing anything.
> Well, I did not get very far.
> If I do the following
>
> use Data::Dumper;
>
> my $list =3D "importe.properties";
> my @directories;
>
> my $file_types =3D ("bat|cfg|cmd|properties|pl|xml|xsl|xslt");
>
> open (FILE, "<".$list) or die "cannot open file";
> @directories =3D ;
> close (FILE);
>
> my @files;
>
> foreach my $dir (@directories)
> {
> =A0 =A0 =A0 =A0@files =3D undef;
> =A0 =A0 =A0 =A0chomp;
> =A0 =A0 =A0 =A0$dir =3D~ s/\\/\//g;
> =A0 =A0 =A0 =A0#if ($dir =3D~ /[A-Z]{1}:(.+)/)
> =A0 =A0 =A0 =A0#{
> =A0 =A0 =A0 =A0# =A0 =A0 =A0 $dir =3D $1;
> =A0 =A0 =A0 =A0#}
> =A0 =A0 =A0 =A0print "DIR: ".$dir."\n";
> =A0 =A0 =A0 =A0opendir(DIR, $dir) or die "cannot open directory: $dir - $=
!";
> =A0 =A0 =A0 =A0@files =3D readdir(DIR);
> =A0 =A0 =A0 =A0closedir(DIR);
> =A0 =A0 =A0 =A0print Dumper \@files;
> }
>
> the script dies at the die with =A0- No such file or directory at
> importe.pl line 50.
>
> the directories are there, I manually copied each path from the
> windows explorer address bar.
> If I leave away the drive letter as in the commented out part, then I
> do not find the files on the other disk.
> I also tried using chdir($dir) and then chdir($Bin).
> This only works for the last entry in the list, for the rest, the
> script insists that cwd is still $Bin.
>
> What am I doing wrong?
>
> (This is perl, v5.8.8 built for MSWin32-x86-multi-thread) on Windows 2003.
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Stupidity on my side - readdir(), chdir() ..
am 28.03.2011 22:05:37 von Ken Slater
>Claus Kick writes:
>
>Hello all,
>
>I am writing a small tool just to update contents of a couple of
>directories. The directories are just stored in a flat text file like
>this:
>
>d:\updates\Imports\MCM_Import_CSG\Config
>d:\updates\Imports\MCM_Import_CSV
>[...]
>f:\sap_import_prod\bin
>
>I figured, I just leave windows notation and then switch to proper
>notation before doing anything.
>Well, I did not get very far.
>If I do the following
>
use strict;
use warnings;
>use Data::Dumper;
>
>my $list = "importe.properties";
No need to pre-declare directories - see below
>my @directories;
>
>my $file_types = ("bat|cfg|cmd|properties|pl|xml|xsl|xslt");
>
use lexical file handles & three argument form of open
open (FILE, "<", $list) or die "cannot open file: $^E";
>open (FILE, "<".$list) or die "cannot open file";
my @directories = ;
>@directories = ;
>close (FILE);
>
No need to preclare @files - see below
>my @files;
>
>foreach my $dir (@directories)
>{
No need to set @files - see below
> @files = undef;
This is your problem. You are chomping $_; not $dir
chomp $dir;
> chomp;
Change for readability
$dir =~ s{\\} {/}g;
> $dir =~ s/\\/\//g;
> #if ($dir =~ /[A-Z]{1}:(.+)/)
> #{
> # $dir = $1;
> #}
> print "DIR: ".$dir."\n";
> opendir(DIR, $dir) or die "cannot open directory: $dir - $!";
my @files = readdir(DIR);
> @files = readdir(DIR);
> closedir(DIR);
> print Dumper \@files;
>}
>
HTH,
Ken Slater
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Stupidity on my side - readdir(), chdir() ..
am 28.03.2011 22:26:54 von Michael Ludwig
Claus Kick schrieb am 28.03.2011 um 21:36 (+0200):
> Hello all,
>
> I am writing a small tool just to update contents of a couple of
> directories. The directories are just stored in a flat text file like
> this:
>
> d:\updates\Imports\MCM_Import_CSG\Config
> d:\updates\Imports\MCM_Import_CSV
> [...]
> f:\sap_import_prod\bin
>
> I figured, I just leave windows notation and then switch to proper
> notation before doing anything.
> Well, I did not get very far.
> If I do the following
You should have the following at the beginning of your script:
use strict;
use warnings;
> use Data::Dumper;
>
> my $list = "importe.properties";
> my @directories;
>
> my $file_types = ("bat|cfg|cmd|properties|pl|xml|xsl|xslt");
This variable is never used.
> open (FILE, "<".$list) or die "cannot open file";
> @directories = ;
> close (FILE);
chomp @directories;
> my @files;
Delete the above.
> foreach my $dir (@directories)
> {
> @files = undef;
Replace the above with:
my @files;
> chomp;
You're chomping $_, but your data is in $dir. This is your bug. I'd say
it happened because you didn't chomp your data straight away, instead
leaving it in an unusable state requiring further action.
> $dir =~ s/\\/\//g;
> #if ($dir =~ /[A-Z]{1}:(.+)/)
> #{
> # $dir = $1;
> #}
> print "DIR: ".$dir."\n";
> opendir(DIR, $dir) or die "cannot open directory: $dir - $!";
> @files = readdir(DIR);
> closedir(DIR);
> print Dumper \@files;
> }
>
> the script dies at the die with - No such file or directory at
> importe.pl line 50.
There should be the dir name (containing the newline) on the preceding
line.
--
Michael Ludwig
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Stupidity on my side - readdir(), chdir() ..
am 28.03.2011 23:14:06 von Claus Kick
Hello,
yes, you were right. normally I use anonymus foreach, thats why I just
wrote chomp and didnt realize the problem.
Thank you very much!
2011/3/28 Michael Ludwig :
> Claus Kick schrieb am 28.03.2011 um 21:36 (+0200):
>> Hello all,
>>
>> I am writing a small tool just to update contents of a couple of
>> directories. The directories are just stored in a flat text file like
>> this:
>>
>> d:\updates\Imports\MCM_Import_CSG\Config
>> d:\updates\Imports\MCM_Import_CSV
>> [...]
>> f:\sap_import_prod\bin
>>
>> I figured, I just leave windows notation and then switch to proper
>> notation before doing anything.
>> Well, I did not get very far.
>> If I do the following
>
> You should have the following at the beginning of your script:
>
> use strict;
> use warnings;
>
>> use Data::Dumper;
>>
>> my $list =3D "importe.properties";
>> my @directories;
>>
>> my $file_types =3D ("bat|cfg|cmd|properties|pl|xml|xsl|xslt");
>
> This variable is never used.
>
>> open (FILE, "<".$list) or die "cannot open file";
>> @directories =3D ;
>> close (FILE);
>
> chomp @directories;
>
>> my @files;
>
> Delete the above.
>
>> foreach my $dir (@directories)
>> {
>> =A0 =A0 =A0 @files =3D undef;
>
> Replace the above with:
>
> my @files;
>
>> =A0 =A0 =A0 chomp;
>
> You're chomping $_, but your data is in $dir. This is your bug. I'd say
> it happened because you didn't chomp your data straight away, instead
> leaving it in an unusable state requiring further action.
>
>> =A0 =A0 =A0 $dir =3D~ s/\\/\//g;
>> =A0 =A0 =A0 #if ($dir =3D~ /[A-Z]{1}:(.+)/)
>> =A0 =A0 =A0 #{
>> =A0 =A0 =A0 # =A0 =A0 =A0 $dir =3D $1;
>> =A0 =A0 =A0 #}
>> =A0 =A0 =A0 print "DIR: ".$dir."\n";
>> =A0 =A0 =A0 opendir(DIR, $dir) or die "cannot open directory: $dir - $!";
>> =A0 =A0 =A0 @files =3D readdir(DIR);
>> =A0 =A0 =A0 closedir(DIR);
>> =A0 =A0 =A0 print Dumper \@files;
>> }
>>
>> the script dies at the die with =A0- No such file or directory at
>> importe.pl line 50.
>
> There should be the dir name (containing the newline) on the preceding
> line.
>
> --
> Michael Ludwig
> _______________________________________________
> ActivePerl mailing list
> ActivePerl@listserv.ActiveState.com
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Stupidity on my side - readdir(), chdir() ..
am 29.03.2011 19:16:16 von Brian Raven
> -----Original Message-----
> From: activeperl-bounces@listserv.ActiveState.com [mailto:activeperl-
> bounces@listserv.ActiveState.com] On Behalf Of Ken Slater
> Sent: 28 March 2011 21:06
> To: 'Claus Kick'; activeperl@listserv.ActiveState.com
> Subject: RE: Stupidity on my side - readdir(), chdir() ..
....
> use lexical file handles & three argument form of open
> open (FILE, "<", $list) or die "cannot open file: $^E";
>
> >open (FILE, "<".$list) or die "cannot open file";
A bit of a typo, I think. With a lexical file handle, that should look something like...
open my $fh, "<", $list or die ...
Also, if using "or" which has lower precedence than "||", you don't need the brackets.
Other than that, ++$what_he_said;
HTH
--
Brian Raven
Please consider the environment before printing this e-mail.
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.
Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Re: Stupidity on my side - readdir(), chdir() ..
am 30.03.2011 02:13:04 von fzarabozo
> From: "Brian Raven"
> Sent: Tuesday, March 29, 2011 11:16 AM
>
> Also, if using "or" which has lower precedence than "||", you don't need
> the brackets.
Wow. Everyday you can learn something new just by reading these posts. :-)
I was under the impression that using "||" or "or" was just a matter of
preference. I suppose the same applies for using "&&" or "and".
Cheers,
Francisco
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
RE: Stupidity on my side - readdir(), chdir() ..
am 30.03.2011 11:04:25 von Brian Raven
> -----Original Message-----
> From: Francisco Zarabozo [mailto:fzarabozo@hotmail.com]
> Sent: 30 March 2011 01:13
> To: Brian Raven; activeperl@listserv.ActiveState.com
> Subject: Re: Stupidity on my side - readdir(), chdir() ..
>
> > From: "Brian Raven"
> > Sent: Tuesday, March 29, 2011 11:16 AM
> >
> > Also, if using "or" which has lower precedence than "||", you don't
> need
> > the brackets.
>
>
> Wow. Everyday you can learn something new just by reading these posts.
> :-)
>
> I was under the impression that using "||" or "or" was just a matter of
> preference. I suppose the same applies for using "&&" or "and".
Yes indeedy. See 'perldoc perlop'.
HTH
--
Brian Raven
Please consider the environment before printing this e-mail.
This e-mail may contain confidential and/or privileged information. If you are not the intended recipient or have received this e-mail in error, please advise the sender immediately by reply e-mail and delete this message and any attachments without retaining a copy.
Any unauthorised copying, disclosure or distribution of the material in this e-mail is strictly forbidden.
_______________________________________________
ActivePerl mailing list
ActivePerl@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs