可以使用use File::Copy ; 中间的move() 方法,或者使用 rename(); 参考代码如下: #! perl use Switch; # use File::Copy; my $dir = "F:/www/tmp/20000/"; opendir DIR,$dir or die $!; while(my $file = readdir(DIR)){ # print $file."/n"; if($file =~ /(/d+)/.html/){ print $1."moved!------------------------->/n"; mkdirlevel($1,$dir,".html"); } } sub mkdirlevel($$$){ my $num = shift; #这里是数字 my $length = length($num); my $dir = shift; #文件建立的目录 my $sufix = shift; my $fileFolder = $num.""; my $firstLevel = ""; my $secondLevel = ""; my $thirdLevel = ""; switch($length){ case 1{ if(!-d $dir.$num){ #-d表示它为目录时 mkdir $dir.$num; } rename $dir.$num.$sufix,$dir.$num."/".$num.$sufix; } case 2{ if(!-d $dir.$num){ #-d表示它为目录时 mkdir $dir.$num; } rename $dir.$num.$sufix,$dir.$num."/".$num.$sufix; } case 3{ $firstLevel = substr($fileFolder,0,1)."/"; $secondLevel = substr($fileFolder,1,2)."/"; if(!-d $dir.$firstLevel){ #-d表示它为目录时,当没有该层目录时 mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; }elsif(!-d $dir.$firstLevel.$secondLevel){ #-d表示它为第二层目录时 mkdir $dir.$firstLevel.$secondLevel; } rename $dir.$num.$sufix,$dir.$firstLevel.$secondLevel.$num.$sufix; } case 4{ $firstLevel = substr($fileFolder,0,2)."/"; $secondLevel = substr($fileFolder,2,2)."/"; if(!-d $dir.$firstLevel){ #-d表示它为目录时,当没有该层目录时 mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; }elsif(!-d $dir.$firstLevel.$secondLevel){ #-d表示它为第二层目录时 mkdir $dir.$firstLevel.$secondLevel; } rename $dir.$num.$sufix,$dir.$firstLevel.$secondLevel.$num.$sufix; } case 5{ $firstLevel = substr($fileFolder,0,1)."/"; $secondLevel = substr($fileFolder,1,2)."/"; $thirdLevel = substr($fileFolder,3,2)."/"; if(!-d $dir.$firstLevel){ #-d表示它为目录时,当没有该层目录时 mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; } if(!-d $dir.$firstLevel.$secondLevel){ mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; } if(!-d $dir.$firstLevel.$secondLevel.$thirdLevel){ mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; mkdir $dir.$firstLevel.$secondLevel.$thirdLevel; } # print $dir.$firstLevel.$secondLevel.$thirdLevel.$num.$sufix."/n"; # print $dir.$num.$sufix."/n"; rename $dir.$num.$sufix,$dir.$firstLevel.$secondLevel.$thirdLevel.$num.$sufix; } case 6{ $firstLevel = substr($fileFolder,0,2)."/"; $secondLevel = substr($fileFolder,2,2)."/"; $thirdLevel = substr($fileFolder,4,2)."/"; if(!-d $dir.$firstLevel){ #-d表示它为目录时,当没有该层目录时 mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; } if(!-d $dir.$firstLevel.$secondLevel){ mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; } if(!-d $dir.$firstLevel.$secondLevel.$thirdLevel){ mkdir $dir.$firstLevel; mkdir $dir.$firstLevel.$secondLevel; mkdir $dir.$firstLevel.$secondLevel.$thirdLevel; } rename $dir.$num.$sufix,$dir.$firstLevel.$secondLevel.$thirdLevel.$num.$sufix; } } } 可以方便的移动文件。