- 论坛徽章:
- 0
|
本帖最后由 zhlong8 于 2012-06-16 20:55 编辑
出现的问题是,目录下有多个文件时存在部分文件仅修改了部分就终止了,是不是不能在同一目录下操作。
sub uncompress {
my $file = shift;
#print "1111111111111\n";
my $new_file = $file;
$new_file =~ s/\.Z$/\.dat/;
$new_file =~ s/\.gz$/\.dat/;
# Uncompressing the file using the system 'zcat' command
my $rc = system( "zcat '${file}' > '${new_file}'" );
#print "inDirname:$inDirname\n";
add_value($inDirname,$new_file);#此处调用子程序
#print "111111111\n";
# Checking the return code from the system function
if( 0 == $rc ) {
system("rm '${file}' ");
}else{
print "ERROR uncompressing .$file.";
unlink $new_file;
return 0;
}
return $new_file;
}
sub add_value {#写文件号函数
my $filepath = shift; #接收路径
my $file_name = shift;#接收文件名
#print "$filepath\n";
my $file_new =basename($file_name);
#print "file_new=$file_new";
opendir (DIR, $filepath) or die "can't open the directory!";
my @files =grep !/^\./, grep /$file_new/, readdir DIR;
#print join('!!',@files);
close DIR;
foreach my $file (@files) {
#print $file;
if ( $file =~/SGSN.*CELLUSR.*\.dat$/ ) {
print "$file\n";
my $w_file=$file."_tmp";
#print $w_file;
my @field=split('_',$file);
my $len=@field;
my $str="";
if($len<4){
$str="20120999001";
}else{
$str=substr($field[3],0,11);
}
open (RFILE,"<$filepath/$file")||die"$!\n"; ;
open (WFILE,">$filepath/$w_file")||die"$!\n";;
while(my $pline=<RFILE>){
next if length(trim($pline))==0;
my $newline=trim($pline).'@#$'.trim($str);
#print "newline:$newline\n";
print WFILE $newline;
print WFILE "\n";
}
close RFILE;
close WFILE;
#文件重命名
rename("$filepath/$w_file","$filepath/$file");
}
}
}
|
|