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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Matt

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

    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(DATFILELIS T,">>/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(DATFILELI ST) || 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


  • Jürgen Exner

    #2
    Re: Question about using readdir to read the files in a directory ...

    Matt wrote:[color=blue]
    > 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(DATFILELIS T,">>/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(DATFILELI ST) || 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 ..[/color]

    Not very surprising. Those are directory entires after all, too, and you
    don't remove/skip them anywhere.
    [color=blue]
    > 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.[/color]

    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$/
    [color=blue]
    > This works, but it also includes the . and ..[/color]

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

    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";
    }
    }


    Comment

    • Matt

      #3
      Re: Question about using readdir to read the files in a directory ...

      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" <jurgenex@hotma il.com> wrote in message
      news:5L9td.754$ zh7.152@trnddc0 2...[color=blue]
      > Matt wrote:[color=green]
      > > 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(DATFILELIS T,">>/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(DATFILELI ST) || 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 ..[/color]
      >
      > Not very surprising. Those are directory entires after all, too, and you
      > don't remove/skip them anywhere.
      >[color=green]
      > > 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.[/color]
      >
      > 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[/color]
      a[color=blue]
      > 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[/color]
      anywhere[color=blue]
      > in the string. If you want to match only the final extension then you must
      > anchor your RE: /\.don$/
      >[color=green]
      > > This works, but it also includes the . and ..[/color]
      >
      > Of course. Just as it will contain any file named any other way. Just try
      > creating some junk files with random names.
      >[color=green]
      > > Any ideas woud be much appreciated.[/color]
      >
      > 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";
      > }
      > }
      >
      >[/color]


      Comment

      Working...