Milleniumlegend
IS-IT--Management
- Dec 16, 2003
- 135
I am having problems with unzipping a file and moving them. My program runs a command line utility that unzips the file but the problem with that is the filename changes.
Is there a way to get a filename without the .zip or .gz and then store the basename and then utilise the script to move the file to another location.
The following is the code.
Assume all of the variables do exist. I think there is something wrong with the script. I am expecting a filename the above pattern such as XXXX_YY_20060512.TXT.gz and then I need to strip the .gz off and then match the pattern for the basename. They are then transferred to another location. But for some reason the files get rejected in the unless clause.
Any pointers.
Many Thanks
Is there a way to get a filename without the .zip or .gz and then store the basename and then utilise the script to move the file to another location.
The following is the code.
Code:
@ARGV == 1 || @ARGV == 2
or abort "expected 1 or 2 arguments";
$filename = shift;
-f $filename
or abort "$filename does not exist";
($basename = $filename) =~ s/.*\\//;
$basename = basename("$filename", ".gz");
print "************BASENAME = $basename***********\n";
if ($filename eq "$basename.gz") {
(system @UNZIP, "$filename")==0 or abort "unzip of $basename.gz failed";
}
unless ($basename =~ /^(.{4})_(.{2})_\.([0-9]){8}\.TXT/){
if ((system @MOVE, $basename, sprintf "%s\\%s-%02d-%02d-%02d_%02d-%02d-%04d", $UNHANDLED, $basename, $HOUR, $MIN, $SEC, $DAY, $MON, $YEAR) == 0)
{
abort "bad filename `$basename', moved to unhandled directory";
}
else
{
abort "failed to move unhandled file `$basename'!";
}
}
Assume all of the variables do exist. I think there is something wrong with the script. I am expecting a filename the above pattern such as XXXX_YY_20060512.TXT.gz and then I need to strip the .gz off and then match the pattern for the basename. They are then transferred to another location. But for some reason the files get rejected in the unless clause.
Any pointers.
Many Thanks