PDA

View Full Version : Blimey, trouble with creating files


bazz
02-21-2006, 09:00 PM
Feels like a simple error but I don't get it.

This code creates a Dir and is then supposed to open it and create files in it.
It fails at the point where it is meant to create the files. I am wondering if the script is running all the way through, which happens to be befor the server has opened the DIR? if this is possible, how tdo I make the script pause?

I tried the full Filepath as well as this shortened one but neither seems to work.


my $Filepath ="/domains/574/2317/html/cgi-bin/$clientsReservationsDir/Rooms_Booked/"; # location of where to open New Directory for $nextYear

opendir(my $dir,$Filepath) or die "You have been unsuccessful. The Directory has not been opened.\n";
chdir "Rooms_Booked";
mkdir "$nextYear";
print "next year = $nextYear\n";
opendir(my $dir,"$Filepath$nextYear/") or die "Uh Oh! $Filepath$nextYear/ You have been unsuccessful. The Directory has not been opened. To repeat the process, remember to refresh the page\n";

foreach my $Month (sort values %months) {
#print "month =$Month<br />\n"; #successful

foreach $Date (sort values %dates) {

if ($Month == '09' || $Month == '04' || $Month == '06' || $Month == '11' ) {
if ($Date > 30 ){
next;
}
}
if ($Month == 02 && $Date > '29' ) {
next;
}
if ($Month == 02 && $yearDivided =~ m/\./ && $Date > '28' ) {
next;
}

$YMD = join ("-", ($nextYear, $Month, $Date));

foreach ( qw( SingleRoomNumbers DoubleRoomNumbers TwinRoomNumbers FamilyRoomNumbers SuitesRoomNumbers ) ){
$type = $1 if $_ =~ /^(.*?)RoomNumbers/;
@rooms = split ',', $accommodations{$_};
foreach $room (@rooms) {
print join ('_', ($type, $room, $YMD )), $/;
my $filenames = join ('_', ($type, $room, $YMD)), $/;

my $dat_file ="$filenames";
print "df=$dat_file<br />\n"; #prints OK
open DAT, "< $dat_file" || die "couldnt open file\n";# doesn't even output the error here.
}
}
}
}




Almost embarrassed to ask for help with this one :o

Bazz

FishMonger
02-21-2006, 09:42 PM
Have you tried putting error handling on the chdir and mkdir commands?

chdir "Rooms_Booked" || die "can't chg dir $!";
mkdir "$nextYear" || die "can't create dir $!";

bazz
02-22-2006, 02:31 PM
Ah you focussed my mind FishMonger. I had omitted to chdir to $nextYear.

So now, I can create the many filenames as I need but, with one bug.

FIXED Yay!!. I'll leave in this post in case it may be of use to others.

SOLUTION: I chopped the line before splitting it.

In the flatfile, the line for DoubleRoomNumbers reads like this:

DoubleRoomNumbers:01,03,05,06,08,09

When I output the $room in the code below, For the last number (09), It adds a space after it. Then when the file is created on the server it is called 'Double_09 ', whereas the others are Single_02_yyy-mm-dd. The space seems to confuse the server as when I try to delete them, it says that they don't exist. All the ones properly formed can be deleted as normal.

How can I get rid of the space? chop fixes the space but removes the last digit from each of the other room Numbers.

I am thinking of some sort of conditional but that seems to be a bit if a bandaid.


foreach ( qw( SingleRoomNumbers DoubleRoomNumbers TwinRoomNumbers FamilyRoomNumbers ) ){
$type = $1 if $_ =~ /^(.*?)RoomNumbers/;
@rooms = split ',', $accommodations{$_};
foreach $room (@rooms) {
#chomp $room;
#chop $room;
print "rooms = $room.<br />\n"; # this dot clarifies that there is an unwanted space after the last digit of the last room number.
#print join ('_', ($type, $room, $YMD )), $/;
my $filenames = join ('_', ($type, $room, $YMD)),$/;

my $dat_file ="$filenames";
print "df=$dat_file<br />\n";
#open DAT, "<$dat_file" || die "couldn't open file\n";

#print DAT "GuestName:\n";
#print DAT "BookingReference:\n";
#print DAT "CheckOutDate:\n";
#print DAT "CheckInDate:\n";


#close DAT;



Bazz

bazz
02-22-2006, 02:45 PM
Thanks

FishMonger
02-22-2006, 03:21 PM
There's nothing in this section of the code that would introduce that space. I think the best way to fix the space issue is to look at the section where you're writting that data to the file and make the correction at that point.

I see that your foreach loop is using the example I showed in your prior question. We can clean it up a little.

foreach $type ( qw( Single Double Twin Family ) ){
@rooms = split ',', $accommodations{"${type}RoomNumbers"};