How do I print the file list of a directory into another file in Perl? -
i tried
system("ls > file"); in perl script when open file, empty file while directory has list of file.
my $dirpath = "./"; $filepath = "./file"; opendir(dir, $dirpath) or die("cannot open directory: $!"); open(out, ">$filepath"); foreach( sort readdir(dir) ){ next if $_ =~ /^\.{1,2}$/; # ignore "." , ".." print(out "$_\n"); } close(out); closedir(dir); note if have output file in directory listing, listed since have open before reading directory.
Comments
Post a Comment