Gossamer Forum
Home : General : Perl Programming :

test for file exist not working

Quote Reply
test for file exist not working
i want to print file size and number of records in db files. i'm opening each cfg file in data_path and reading the name of the db file from cfg file. excerpt of code below:
Code:
open (CONFILE, "<$db_data_path/$file") or &cgierr ("unable to open: $file.\nReason: $!");

while (<CONFILE>) {
if (/\$html_title/) {
$_=~s/\$html_title//g;
$_=~s/[=';]//g;
$_=~s/\#.*$//g; #3/11/2008 remove comments

$info_name = qq|$_|;
$info_link = qq|<a href="$dbman_script_url?db=$cfgfile&uid=$db_uid">Go to $cfgfile database</a> |;
}
if (/\$db_file_name/) {
$_=~s/\$db_file_name//g;
$_=~s/[=';]//g;
$_=~s/\#.*$//g;
$db_file = $_;
$db_file =~ s/\$db_data_path/$db_data_path/g;
$db_file =~ s/\$db_script_path/$db_script_path/g;
$db_file =~ s/"//g;
$db_file =~ s/ . //g;
$db_file =~ s/ //g;
}
}
close (CONFILE);

# size of the data file

if (-e $db_file) {
open (DBFILE, $db_file) or &cgierr ("unable to open: $db_file.\nReason: $!");
if ($db_use_flock) {
flock(DBFILE, 1) or &cgierr("unable to get exclusive lock on $db_file.\nReason: $!");
}
if ($size = -s $db_file){
blah blah blah
nothing in the if (-e $db_file) is being executed. i printed the value of $db_file and it is correct showing the complete path from the root. i have another sub that will open a file and display content. i sent $db_file to that link and it displays correctly. i can't figure out what's wrong!
Quote Reply
Re: [delicia] test for file exist not working In reply to
more info. this is the line in cfg
Code:
# Full Path and File name of the database file.
$db_file_name = $db_data_path . "/dbman/default.db";
some databases are in $db_script_path instead of $db_data_path
Quote Reply
Re: [delicia] test for file exist not working In reply to
Code:
if (-e $db_file) {

Shouldn't that be $db_file_name , based on your other comment?

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] test for file exist not working In reply to
i don't think so. i think i've set $db_file = $db_file_name:
Code:
if (/\$db_file_name/) {
$_=~s/\$db_file_name//g;
$_=~s/[=';]//g;
$_=~s/\#.*$//g;
$db_file = $_;
$db_file =~ s/\$db_data_path/$db_data_path/g;
$db_file =~ s/\$db_script_path/$db_script_path/g;
$db_file_name is the original file. i want to restore the backup version which is in $backup_dir. i think $_ has the backup_dir in the path. hmmm. maybe i see the problem! i think i may be stripping out too much of the path. will attempt to fix and post results later!
Quote Reply
Re: [delicia] test for file exist not working In reply to
senior moment! i forgot what i was doing. i've already got the restore working so ignore what i said in last message. $db_file contains the complete path and filename that exists. i'm reading the cfg line by line to find the line that has $db_file_name = blah blah. once found i'm stripping out "$db_file_name = ", the quotation marks, and all spaces. i'm substituting the actual data path or script path for the related variable. so the final $db_file is the correct path and filename.

there is a sub that opens and displays the contents of the file and i put in the link to it to be sure it's actually opening the correct file.
here's the link i'm using to test that it's opening the file:
Code:
a href="$db_script_url?db=$db_setup&amp;admin_view_file=1&amp;uid=$db_uid&amp;file_data=$db_file"
note file_data=$db_file. here's excerpt of the sub that displays the file data:
Code:
$view_file_data = $in{file_data};
open (FILEVIEW, $view_file_data) or &cgierr ("unable to open: $view_file_data.\nReason: $!");

i.e. i'm still having the problem!
Quote Reply
Re: [delicia] test for file exist not working In reply to
another sub which is working fine has the following:
Code:
my @ext = ("$db_file_name", "$db_id_file_name", "$auth_log_file", "$auth_pw_file");
foreach $ext (@ext) { # private files
if ($size = -e "$ext") {
if ($size = -z "$ext") {
it finds $db_file_name and reports the size. this sub is using the cfg file, so it knows the value of $db_file_name. the sub that isn't working isn't using the cfg file, but is reading it to find the value. there are lots of cfg files. the working sub is only concerned with one cfg, while the non-working one is reporting values from all the cfg files. hope this makes sense.
Quote Reply
Re: [delicia] test for file exist not working In reply to
Hi,

Sorry, I'm a bit confused with your code ;) This is why using "use strict;" is so important in coding. It points out invalid variables (no existent, typos etc).

All I can say is - that is -s isn't giving a value, its because the file doesn't exist (or is 0 bytes). You need to check the value of the string you are checking, and ideally then confirm it exists by doing something like this in SSH:

Code:
nano /path/to/file.txt

Then see if it actually does exist :) (i.e not a typo)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] test for file exist not working In reply to
ok, it's working for some files but not others, even those in the same directory! it seems chomp helped.
Code:
if (/\$db_file_name/) {
$_=~s/\$db_file_name//g;
chomp $_;
$_=~s/[=';]//g;
$_=~s/\#.*$//g;
$_ =~ s/\$db_data_path/$db_data_path/g;
$_ =~ s/\$db_script_path/$db_script_path/g;
$_ =~ s/"//g;
$_ =~ s/ . //g;
$_ =~ s/ //g;
$_ =~ s/\.\///;
$db_file = $_;
}
it's printing:
Code:
/var/home/delicia/delicia.com/cgi-bin/public/data/common/logins.db not found
/var/home/delicia/delicia.com/cgi-bin/public/data/common/menu.db found
logins.db and menu.db are in the same directory; both exist and are not empty.
here's excerpt of menu.cfg:
Code:
# Full Path and File name of the database file.
$db_file_name = $db_data_path . "/common/menu.db";
# Full path and file name of the counter file.
and logins.cfg:
Code:
# Full Path and File name of the database file.
$db_file_name = $db_data_path . "/common/logins.db";
# Full path and file name of the counter file.
i can't see any difference and can't understand why one works and the other doesn't
Quote Reply
Re: [delicia] test for file exist not working In reply to
Hi,

If "chomp" worked, that means it must have had a newline character at the end for some reason.

For your other issue, I'm afraid I'm not seeing it either. It must be somewhere else.

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!