一般问题:遍历windows文件夹结构,不使用Find::File并将所有文件夹重命名为一些短值。这是为了解决经典的“Windows 文件路径超过 256 个字符”的问题。
我的问题:我一切正常,除了它会正确处理穿过树的单个路径,但不是任何其他路径,我不明白为什么。
注意:use File:Find 仍然存在,尽管没有使用,而且代码可能不优雅。它重命名当前文件夹,然后遍历子文件夹。
代码:
use strict;
use File::Find;
use File::Copy;
use File::Path;
my $target = "E:\\bl10s\\";
opendir( DIR, $target );
my $newFolderName = 0;
my $file;
while ( $file = readdir(DIR) ) {
# A file test to check that it is a directory
# Use -f to test for a file
next if ( $file eq "." );
next if ( $file eq ".." );
next if ( -f "$target\\$file" );
print "$target/$file" . "\n";
while ( -e $target . $newFolderName ) {
$newFolderName++;
}
print $target. $file . " rename to " . $target . $newFolderName . "\n";
rename( $target . $file, $target . $newFolderName );
}
closedir(DIR);
opendir( DIR, $target );
while ( $file = readdir(DIR) ) {
next if ( $file eq "." );
next if ( $file eq ".." );
if ( -f "$target\\$file" )
{
print "Failed name check on itterator main line \n";
}
my $nextDirectoryPathCalled = $target . $file;
print "Re-iterating on: " . $nextDirectoryPathCalled;
my $someint = &stripper($nextDirectoryPathCalled);
}
closedir(DIR);
# find( \&dir_names, $target );
sub stripper {
print "\nNew level\n";
print "$_[0] . \n";
my $target = $_[0] . "\\";
opendir( DIR, $target );
my $newFolderName = 0;
my $file;
while ( $file = readdir(DIR) ) {
# A file test to check that it is a directory
# Use -f to test for a file
next if ( $file eq "." );
next if ( $file eq ".." );
next if ( -f "$target\\$file" );
print "TARGET: $target FILE: $file" . "\n";
while ( -e $target . $newFolderName ) {
$newFolderName++;
}
print $target
. $file
. " rename to "
. $target
. $newFolderName . "\n";
rename( $target . $file, $target . $newFolderName );
}
closedir(DIR);
opendir( DIR, $target );
while ( $file = readdir(DIR) ) {
next if ( $file eq "." );
next if ( $file eq ".." );
next if ( -f "$target\\$file" );
my $nextDirectoryPathCalled = $target . $file;
print "Re-iterating on: " . $nextDirectoryPathCalled;
&stripper($nextDirectoryPathCalled);
}
closedir(DIR);
}
有任何想法吗?随意批评...
谢谢