使用Perl與文件::查找模塊,可以實現這樣的事情:
use File::Find;
my $dirname = "../test/";
finddepth(sub {
return if /^\.{1,2}$/; # ignore '.' and '..'
return unless -d $File::Find::name; # check if file is directory
if (s/\ /_/g) { # replace spaces in filename with underscores
my $new_name = $File::Find::dir.'/'.$_; # new filename with path
if (rename($File::Find::name => $new_name)) {
printf "Directory '%s' has been renamed to '%s'\n",
$File::Find::name,
$new_name;
} else {
printf "Can't rename directory '%s' to '%s'. Error[%d]: %s\n",
$File::Find::name,
$new_name,
$!, $!;
}
}
}, $dirname);
前:
% tree test
test
├── test 1
├── test 2
└── test 3
└── test 3 4
後:
% tree test
test
├── test_1
├── test_2
└── test_3
└── test_3_4
當你有 「產品圖片」和「product_images」目錄已經存在,那麼會發生什麼? – tadmc 2012-01-19 02:12:41