Re: Newbie Question...

From: Paul Lalli (ittyspam_at_yahoo.com)
Date: 04/12/04


Date: Mon, 12 Apr 2004 12:31:05 -0400

On Mon, 12 Apr 2004, EddieJ wrote:

> I'm writing my first perl script and I'm running in to
> difficulties as I imagined. What I'm trying to do is
> basically scan all the directories and subdirectories
> of a drive for powerpoint files, write the full path
> and name of the files to an ordinary text file, and
> then remove the drive name and colon (eg C:) from the
> path and send the output to another text file.
>
> The script I have so far is...

#!perl.exe
use warnings;
use strict;

>
> print "Which drive do you want to search? ";
> chomp($drive = <STDIN>);
> chdir("$drive") || die "Cannot access requested drive";
> $outputfile1 = ("%HOMEDRIVE%\\ppfile1.txt");
> $outputfile2 = ("%HOMEDRIVE%\\ppfile2.txt");
> $outputfile3 = ("%HOMEDRIVE%\\ppfile3.txt");
>
> if (-e $outputfile1) {
> unlink ($outputfile1);
> }
>
> if (-e $outputfile2) {
> unlink ($outputfile2);
> }
>
> if (-e $outputfile3) {
> unlink ($outputfile3);
> }

Why are you bothering with this? If the files exist when you try to open
them for writing, they are automatically clobbered.

> system ("dir/s/b *.pp* >$outputfile1") && die "Cannot write to file1 $!";
> system ("dir/s/b *.xyz >$outputfile2");

Good checking the return value of the first system(). Bad not checking
the return value of the second system().

> open FILE1, ">$outputfile1" || die "Cannot open file1 $!";
> open FILE2, ">$outputfile2" || die "Cannot open file2 $!";
> while (defined <FILE1>) {
> # print FILE1 $_;
> print FILE2 s/^a-zA-Z://;
> }

What? What exactly are you trying to do here? You're opening two files
for writing, and then trying to read from one of them, and exit the loop
when you can't read any more. This doesn't make sense. You can't read
from FILE1 because you opened FILE1 for writing.

I don't understand what data you're expecting to be in $_. I also don't
understand why you're trying to open these files at all when you already
redirected the output of the system() calls to go to those files.

> close (FILE1) || die "Cannot close file1 $!";
> close (FILE2) || die "Cannot close file2 $!";
>
> But when I run it, the second output file is empty and perl
> complains that it cannot close the first outputfile
>
> -----------------------------
> Which drive do you want to search? j:
> File Not Found
> Cannot close file1 Bad file descriptor at powerp.pl line 27.
> -------------------------------
>
> The "File Not Found" is from the
> "system ("dir/s/b *.xyz >$outputfile2");"
> which doesnt bother me since I'm just trying to create an empty
> file so that I can open it later to receive the output (without
> the C:).
>
> From what I can see though, the open commands aren't working
> despite the use of the "die" commands..
>
>
> So...I'm sure this is very obvious to anyone with experience
> of Perl, but (a) what am I doing wrong with the script??? and
> (b) is there a more efficient way of doing what I'm trying to
> do??. Can you do a recursive directory search in perl without
> using the system dir/b/s command, and should I store the output
> in an array rather than the first output file....

To answer b) "Yes, use File::Find". This is a module that will
recursively scan any directory you give to it. For more info,
perldoc File::Find

#!perl.exe
use strict;
use warnings;
use File::Find;

my $dir = "C:";
my ($out1, $out2);
open $out1, "> output1.txt" or die "Cannot open output1.txt: $!";
open $out2, "> output2.txt" or die "Cannot open output2.txt: $!";

sub wanted {
        return unless /\.ppt$/i; #skip non-powerpoint files
        print $out1 "$File::Find::name\n"; #print full path to one file
        $File::Find::name =~ s/^[a-z]://i; #strip C:
        print $out2 "$File::Find::name\n"; #print modified path to other
}

find (\&wanted, $dir); #recursively scan all directories under C:
__END__

Hope this helps
Paul Lalli



Relevant Pages

  • Re: Some Advice.
    ... Practice, practice. ... really need that much math to learn to program. ... > for an algorithm but writing a program is more like writing a good paper ... No comment, I don't know Perl. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: (1)[0] ok but not 1[0]
    ... FK> I think I'll be writing for some weeks, ... FK> "Programming Perl" and http://perldoc.perl.org/. ... there are dozens of perl tutorials on the net ...
    (comp.lang.perl.misc)
  • Re: Some Advice.
    ... >>am learning that naming is far more important in C than in Perl. ... >>Writing a program and learning is very fustrating. ... Perl was really bad for that in complex data structures. ... I hope that my code can be seen and I get some advice why ...
    (alt.comp.lang.learn.c-cpp)
  • [UNIX] Sudo Perl Local Privileges Escalation
    ... The following security advisory is sent to the securiteam mailing list, and can be found at the SecuriTeam web site: http://www.securiteam.com ... Sudo Perl Local Privileges Escalation ... Writing FTP/Makefile.PL ...
    (Securiteam)
  • Re: How good is PERL at searching ASCII files?
    ... Can PERL do th ... or should I just spend 3 or 4 hours writing ... segregate the three param calls from the two param calls. ... The key elements being the string CONVERT, ...
    (comp.lang.perl.misc)