RE: Checking to see if file exists.



Somehow my response got mangled a tad. I'm editing the middle part:

.......
and got the same results. (The test failed.)

I've tried:
(-e substr ($file, 0, index($file, ".pgp"))),
(-e $tmpfile), (-e "$tmpfile"),
(-e "$RDYdir\/$tmpfile"),
(-f $tmpfile),
(-e "F0715PAY.TXT") <--- This is the only one that worked.

Here is the true code if that helps:
........

Thanks,
Tim

-----Original Message-----
From: tvadnais [mailto:tvadnais@xxxxxxxxxxxxx]
Sent: Tuesday, July 29, 2008 7:00 PM
To: beginners@xxxxxxxx
Subject: RE: Checking to see if file exists.

Ken,

Thank you for your quick response.

I changed the code from:

if (-e $tmpfile) {

to:

if (-f $tmpfile) {

and got the same results. (The test failed.)
(-e substr ($file, 0, index($file, ".pgp"))),
(-e $tmpfile),
(-e "$tmpfile"),
(-e "$RDYdir\/$tmpfile")
(-f $tmpfile)
(-e "F0715PAY.TXT") <--- This is the only one that worked.

Here is the true code if that helps:
I've tried:

foreach my $file ( @currset )
{
my $tmpstr = substr ($file, 0, index($file, ".pgp"));
print ("trying to delete $tmpstr \n");
## pgp will core dump if there's already an unencrypted file
## with the destination source name... So we have to remove the
## previously decrypted file if it exists.
unlink ($tmpstr);
$tmpstr = "/usr/local/bin/pgp -z 'Chars process for WSHA' $file";
if (system($tmpstr) == 0) {
$notice .= LogMsg (MSG => "system $tmpstr failed: $?", DEBUG =>
$debug);
} else {
my $tmpfile = substr ($file, 0, index($file, ".pgp"));
print cwd();
chomp($tmpfile);
if (-f $tmpfile) {
$notice .= LogMsg (MSG => "Deleteing PGP file from RDY
directory: $file", Echo => $debug);
## Remove the PGP file; it's already been archived.
unlink ($file);
$file = substr ($file, 0, index($file, ".pgp"));
$notice .= LogMsg (MSG => "copying unencrypted to HST directory
as: $file.$datestr", Echo => $debug);
## archive new unencrypted file with today's date
copy ($file, "$HSTdir/$file.$datestr");
$notice .= LogMsg (MSG => "moving unencrypted to HST directory
as: $file", Echo => $debug);
## change file permissions so users can upload file.
chmod 0666, $file;
## move file to archive directory with correct name.
## this file will be deleted upon file upload.
move ($file, "$HSTdir/$file");
} else {
my $msg = LogMsg (MSG => "The call to PGP failed on file:
$file", Echo => $debug);
EmailNotice(LIST => [$file], DEBUG => $debug, MSG => $msg,
WHO => \@alert_notify);
$notice .= "The call to PGP failed on file: $file\n";
}
}
}



-----Original Message-----
From: Kenneth Wolcott [mailto:kennethwolcott@xxxxxxxxx]
Sent: Tuesday, July 29, 2008 5:38 PM
To: beginners@xxxxxxxx
Subject: Re: Checking to see if file exists.

On Tue, Jul 29, 2008 at 2:32 PM, tvadnais <tvadnais@xxxxxxxxxxxxx> wrote:

I am totally confounded by what appears to be a bug in the "does file
exist"
functionality.



Here is the code as it stands now:

my $tmpfile = substr ($file, 0, index($file, ".pgp"));

print cwd(); ## debug code: to make sure we're in the correct
directory

chomp($tmpfile); ## debug code: Added this to see if there was any thing
funny in the file name.

if (-e $tmpfile) {

# if (-e "$tmpfile"){ ##This doesn't work

# if (-e "F0715PAY.TXT") { ## This does work, but it needs to be
a
variable

## Do magic stuff to file.

} else {

## Send error message

}



I stepped through the code with the debugger for the above snippet and
this
is what I got (with a few minor edits for clarity sake)



The following files were found in directory: F0715PAY.TXT.pgp,
J0715PAY.TXT.pgp, C0715PAY.TXT.pgp

main::(rmain.pl2): my $tmpfile = substr ($file, 0, index($file, ".pgp"));

DB<> n

DB<> x $tmpfile

0 'F0715PAY.TXT.pgp'

DB<> /xfer/test/RDY

main::(rmain.pl2): chomp($tmpfile);

DB<> n

main::(rmain.pl2): if (-e $tmpfile) {

DB<> x $tmpfile

0 'F0715PAY.TXT'

DB<> n

main::(rmain.pl2): LogMsg (MSG => "Couldn't find $F0715PAY.TXT", Echo =>
$debug);



In a nutshell:

If I hard code in a file that I know exists, then the conditional (-e
filename) comes back true.

If that same file name is passed to the conditional in variable form, then
(-e $filename) and (-e "$filename") returns false.

I also tried passing in the full path name, with no success.

Yes, I do have permissions to read the file.

The OS is AIX UNIX if that makes any difference.



The "Send error message" includes email notification that the PGP
unencryption didn't work, so it's critical I don't just blindly assume
that
the PGP decryption worked.

The "Do magic stuff" includes zipping and archiving the PGP file of to an
archive directory.



Any thoughts you anyone has would be greatly appreciated.



Tim


See what happens if you don't quote the variable in your if statement and
use -f rather than -e.

Replace this:
if (-e "$tmpfile")

with this:

if (-f $tmpfile)


perldoc -q quoting
Found in /usr/lib/perl5/5.10/pods/perlfaq4.pod
What's wrong with always quoting "$vars"?

The problem is that those double-quotes force
stringification--coercing
numbers and references into strings--even when you don't want them to
be strings. Think of it this way: double-quote expansion is used to
produce new strings. If you already have a string, why do you need
more?

If you get used to writing odd things like these:

print "$var"; # BAD
$new = "$old"; # BAD
somefunc("$var"); # BAD

You'll be in trouble. Those should (in 99.8% of the cases) be the
simpler and more direct:

print $var;
$new = $old;
somefunc($var);

Otherwise, besides slowing you down, you're going to break code when
the thing in the scalar is actually neither a string nor a number,
but
a reference:

func(\@array);
sub func {
my $aref = shift;
my $oref = "$aref"; # WRONG
}

You can also get into subtle problems on those few operations in Perl
that actually do care about the difference between a string and a
number, such as the magical "++" autoincrement operator or the
syscall() function.

Stringification also destroys arrays.

@lines = `command`;
print "@lines"; # WRONG - extra blanks
print @lines; # right


I hope this helps,
Ken Wolcott


--
To unsubscribe, e-mail: beginners-unsubscribe@xxxxxxxx
For additional commands, e-mail: beginners-help@xxxxxxxx
http://learn.perl.org/



.



Relevant Pages

  • RE: Checking to see if file exists.
    ... if (-e $tmpfile) { ... The "Do magic stuff" includes zipping and archiving the PGP file of to an ... If you already have a string, ...
    (perl.beginners)
  • Re: GRAHAM MAYOR - Re: Macro in Normal.dot prevents Word opening
    ... autoopen/autonew/autoexec macros and the content of any add-ins. ... from my most recent attempt at this archiving macro. ... Dim TodaysDate As String, OldVersionPath As String, OldVersionFile ... As String, OldVersionName As String ...
    (microsoft.public.word.vba.general)
  • Re: GRAHAM MAYOR - Re: Macro in Normal.dot prevents Word opening
    ... autoopen/autonew/autoexec macros and the content of any add-ins. ... from my most recent attempt at this archiving macro. ... Dim TodaysDate As String, OldVersionPath As String, OldVersionFile ... As String, OldVersionName As String ...
    (microsoft.public.word.vba.general)
  • Re: GRAHAM MAYOR - Re: Macro in Normal.dot prevents Word opening
    ... other than the nearly-identical AutoClose & manual archiving ... macros, I haven't added to or changed the default Normal.dot template. ... Dim TodaysDate As String, OldVersionPath As String, ... OldVersionFile As String, OldVersionName As String ...
    (microsoft.public.word.vba.general)
  • Re: GRAHAM MAYOR - Re: Macro in Normal.dot prevents Word opening
    ... Word crashes when I try to open it after moving the 3 macros from Normal.old ... most recent attempt at this archiving macro. ... Dim TodaysDate As String, OldVersionPath As String, OldVersionFile As ...
    (microsoft.public.word.vba.general)