2012年1月10日 星期二

Perl directory and file list

If directory not exist, create it:

if (not -d $My_DIR) {
  mkdir $My_DIR;
}

Reference: http://stevemorin.blogspot.com/2009/09/perl-test-if-directory-exists.html


Check directory empty example:

$work_dir = "D:\\work";

if (scalar <$work_dir\\*>) {
print "Something is in there\n";
}

Reference: http://forums.devshed.com/perl-programming-6/check-if-directory-empty-or-not-510356.html


Get directory file list example:
$work_dir = "D:\\work";
@files = <$work_dir\\*>;
print @files;

Reference: http://forums.devshed.com/perl-programming-6/using-perl-to-list-files-in-a-directory-344889.html


Get all subdirectory file list example:
use File::Find;
find ( sub { next unless -f; print $File::Find::name,"\n" }, "." );

Reference:
http://www.perlmonks.org/?node_id=535340
http://perldoc.perl.org/File/Find.html

Move files example:

use File::Copy;
$src_file = "D:\\src";
$dst_file = "D:\\dst";
move($src_file, $dst_file);

Reference: http://www.tizag.com/perlT/perlcopyfiles.php

沒有留言:

張貼留言