opendir

戻る

列出一个目录下的所有文件

# pro_list.pl use strict; my(%t,$file); opendir(BIN,"cgi-bin\\pro"); open(OUT,">perlpro_list.txt"); print OUT "filename=perpro_list.txt.\n"; while (defined ($file = readdir BIN ) ) { next if $file =~ /^\.\.?$/; print OUT "file==>$file\n"; } close(BIN); 运行结果

input.pl(该程序的要点是使用opendir)

# input.pl use strict; use HTML::Template; my(%t,@fld,$n,$template,@loop); $t{tmpl} = 'input0.htm'; $t{htmfile} = 'index.html'; $template = HTML::Template->new(filename => $t{tmpl}); print "Please input the directory name="; chop($t{dir}=<STDIN>); opendir(DIR,"$t{dir}") or die "Can't opendir $t{dir}: $!"; while ( defined($t{file}=readdir(DIR)) ) { next if $t{file} =~ /^\.\.?$/; # skip . and .. if ( substr($t{file},-3) eq 'csv' ) { $t{NO1} = $t{file}; substr($t{NO1},-4) = ''; substr($t{NO1},0,9) = ''; $t{list}{$t{NO1}} = $t{file}; } } close(DIR); @loop = (); $t{N1} = 0; for $n ( sort {$a<=>$b} keys %{ $t{list} } ) { $t{N1}++; $t{file} = $t{list}{$n}; $t{N3} = '<a href="' . $t{file} . '">' . $t{file} . '</a>'; my %row = ( N1 => $t{N1}, N2 => $n, file => $t{N3} ); push(@loop, \%row); } $template->param(loop => \@loop); $template->param(dir => $t{dir}); open(OUT,">./$t{dir}/$t{htmfile}"); print OUT $template->output; close(OUT); print "The output file is ./$t{dir}/$t{htmfile}\n"; __END__;
戻る