in reply to Bad file descriptor' error with automated FTP script.
There's no guarantee that each line of output from "dir" will have the file name starting at the 41st character. If the file size (which is the fourth column in the dir output) happens to be really large (more than 7 digits, I think), then that column will be wider than on other lines, and the file name will be pushed that many characters further to the right.my @files=$ftp->dir or print FD1 "error in dir list..." ... foreach(@files) { ... $_=substr($_,41); ... }
Use "split()" instead -- the number of columns output by "dir" is always the same:
This let's you have a variable assigned to every piece of information you could possibly use (and some that you will probably never use) -- and you don't need "chomp" here.my @files = $ftp->dir or die "ack -- can't get ftp->dir"; for ( @files ) { my ($mode, $nlnk, $grp, $sz, $mo, $dy, $yrtm, $fname) = split( /\s+ +/, $_, 8 ); ... }
Note the use of the "8" in the split call -- this will make sure that we stop splitting at the 8th "word", so if some jerk puts up a file with spaces in the file name, split() will still treat the whole name as one "word" to be assigned to $fname.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Bad file descriptor' error with automated FTP script.
by Anonymous Monk on May 15, 2004 at 08:06 UTC |