Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Basename and Unzip

Status
Not open for further replies.

Milleniumlegend

IS-IT--Management
Joined
Dec 16, 2003
Messages
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.

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
 
I am using file Basename. I think the problem is with the unless clause. Could you please let me know if I have got something wrong here logically. I see the perl script does work.


 
Try this
Code:
unless ($basename =~ /^(.{4})_(.{2})_([0-9]){8}\.TXT/){



--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top