Question about using readdir to read the files in a directory ...

Discussion in 'Perl' started by Matt, Dec 7, 2004.

  1. Matt

    Matt Guest

    Hello,

    I am writing a script that opens needs to get a listing of files in a
    directory, print that listing to a file and use that file as a quasi ftp
    control file. My problem is that when I print the filenames found via the
    readdir I also get the . and .. (current directory and parent directory)
    written in the ftp control file which causes my script to stop since you
    can't ftp . nor ..

    Here's my logic ....


    opendir(DON,"/don") || die "Can't opendir /don: $!";
    open(DATFILELIST,">>/data/datafiles.out") || die "Can't opendir
    /data/datafiles.out: $!";
    print DATFILELIST "cd /incoming\n";
    print DATFILELIST "lcd /data/utech/dat\n";
    while ($donefile = readdir(DON)) {
    $datafile = $donefile;
    $datafile =~ s/\.don/\.dat/;
    print DATFILELIST "get $datafile\n";
    }
    print DATFILELIST "bye\n";
    close(DATFILELIST) || die "Can't close file /data/datafiles.out $!";
    closedir(DON) || "Can't closedir /data/utech/don: $!";

    The DATFILELIST gets the right files, but it also gets the . and .. which
    causes the ftp to fail.

    I wonder if my real problem is with my pattern matching. I want to match
    all occurences of a file that ends in .don and rename the extension to .dat
    then write that output to the DATFILELIST file. This works, but it also
    includes the . and ..

    Any ideas woud be much appreciated.


    Thanks,


    Matt
    Matt, Dec 7, 2004
    #1
    1. Advertising

  2. Matt wrote:
    > Hello,
    >
    > I am writing a script that opens needs to get a listing of files in a
    > directory, print that listing to a file and use that file as a quasi
    > ftp control file. My problem is that when I print the filenames
    > found via the readdir I also get the . and .. (current directory and
    > parent directory) written in the ftp control file which causes my
    > script to stop since you can't ftp . nor ..
    >
    > Here's my logic ....
    >
    >
    > opendir(DON,"/don") || die "Can't opendir /don: $!";
    > open(DATFILELIST,">>/data/datafiles.out") || die "Can't opendir
    > /data/datafiles.out: $!";
    > print DATFILELIST "cd /incoming\n";
    > print DATFILELIST "lcd /data/utech/dat\n";
    > while ($donefile = readdir(DON)) {
    > $datafile = $donefile;
    > $datafile =~ s/\.don/\.dat/;
    > print DATFILELIST "get $datafile\n";
    > }
    > print DATFILELIST "bye\n";
    > close(DATFILELIST) || die "Can't close file /data/datafiles.out $!";
    > closedir(DON) || "Can't closedir /data/utech/don: $!";
    >
    > The DATFILELIST gets the right files, but it also gets the . and ..


    Not very surprising. Those are directory entires after all, too, and you
    don't remove/skip them anywhere.

    > which causes the ftp to fail.
    >
    > I wonder if my real problem is with my pattern matching. I want to
    > match all occurences of a file that ends in .don and rename the
    > extension to .dat then write that output to the DATFILELIST file.


    Actually the pattern match is only the first step in the substitute
    operator.
    You got a minor issue in the replacement string. The replacement string is a
    string, not an RE. Therefore there is no reason to escape the dot.
    And you got a maybe significant issue in the RE. Your RE will match anywhere
    in the string. If you want to match only the final extension then you must
    anchor your RE: /\.don$/

    > This works, but it also includes the . and ..


    Of course. Just as it will contain any file named any other way. Just try
    creating some junk files with random names.

    > Any ideas woud be much appreciated.


    If you want to print only specific files, then you may want to use a
    condition, e.g. (untested):

    if ($datafile =~ s/\.don/\.dat/) {
    print DATFILELIST "get $datafile\n";
    }

    On the other hand the whole sections begs the question why don't you just
    use $_ (agaIn, untested):
    while (readdir(DON)) {
    if (s/\.don/\.dat/) {
    print DATFILELIST "get $_\n";
    }
    }
    Jürgen Exner, Dec 7, 2004
    #2
    1. Advertising

  3. Matt

    Matt Guest

    Jurgen,

    Thank you for the response. I did try the expression boundaries earlier but
    they didn't seem to help so I kept playing around. I will definitely put
    them back since I know it is better coding.

    I will also work to implement the condition statement.

    Thank you for your assistance.


    Matt
    "Jürgen Exner" <> wrote in message
    news:5L9td.754$zh7.152@trnddc02...
    > Matt wrote:
    > > Hello,
    > >
    > > I am writing a script that opens needs to get a listing of files in a
    > > directory, print that listing to a file and use that file as a quasi
    > > ftp control file. My problem is that when I print the filenames
    > > found via the readdir I also get the . and .. (current directory and
    > > parent directory) written in the ftp control file which causes my
    > > script to stop since you can't ftp . nor ..
    > >
    > > Here's my logic ....
    > >
    > >
    > > opendir(DON,"/don") || die "Can't opendir /don: $!";
    > > open(DATFILELIST,">>/data/datafiles.out") || die "Can't opendir
    > > /data/datafiles.out: $!";
    > > print DATFILELIST "cd /incoming\n";
    > > print DATFILELIST "lcd /data/utech/dat\n";
    > > while ($donefile = readdir(DON)) {
    > > $datafile = $donefile;
    > > $datafile =~ s/\.don/\.dat/;
    > > print DATFILELIST "get $datafile\n";
    > > }
    > > print DATFILELIST "bye\n";
    > > close(DATFILELIST) || die "Can't close file /data/datafiles.out $!";
    > > closedir(DON) || "Can't closedir /data/utech/don: $!";
    > >
    > > The DATFILELIST gets the right files, but it also gets the . and ..

    >
    > Not very surprising. Those are directory entires after all, too, and you
    > don't remove/skip them anywhere.
    >
    > > which causes the ftp to fail.
    > >
    > > I wonder if my real problem is with my pattern matching. I want to
    > > match all occurences of a file that ends in .don and rename the
    > > extension to .dat then write that output to the DATFILELIST file.

    >
    > Actually the pattern match is only the first step in the substitute
    > operator.
    > You got a minor issue in the replacement string. The replacement string is

    a
    > string, not an RE. Therefore there is no reason to escape the dot.
    > And you got a maybe significant issue in the RE. Your RE will match

    anywhere
    > in the string. If you want to match only the final extension then you must
    > anchor your RE: /\.don$/
    >
    > > This works, but it also includes the . and ..

    >
    > Of course. Just as it will contain any file named any other way. Just try
    > creating some junk files with random names.
    >
    > > Any ideas woud be much appreciated.

    >
    > If you want to print only specific files, then you may want to use a
    > condition, e.g. (untested):
    >
    > if ($datafile =~ s/\.don/\.dat/) {
    > print DATFILELIST "get $datafile\n";
    > }
    >
    > On the other hand the whole sections begs the question why don't you just
    > use $_ (agaIn, untested):
    > while (readdir(DON)) {
    > if (s/\.don/\.dat/) {
    > print DATFILELIST "get $_\n";
    > }
    > }
    >
    >
    Matt, Dec 7, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramon
    Replies:
    5
    Views:
    10,780
    John C. Bollinger
    Jan 3, 2005
  2. electric sheep

    readdir() and S_ISDIR(stat.st_mode) question (OT ?)

    electric sheep, Mar 4, 2004, in forum: C Programming
    Replies:
    2
    Views:
    11,510
    Villy Kruse
    Mar 4, 2004
  3. John Stoffel
    Replies:
    0
    Views:
    151
    John Stoffel
    Feb 6, 2009
  4. Davy
    Replies:
    2
    Views:
    209
  5. Charles Packer

    In what order does readdir() see a directory?

    Charles Packer, Aug 10, 2007, in forum: Perl Misc
    Replies:
    1
    Views:
    158
    Peter Makholm
    Aug 10, 2007
Loading...

Share This Page