Highlighted

How do I know if this variable is a file handle?

Jul 27, 2012

Copy to clipboard

Copied

G'day

(This has also been posted on StackOverflow)

Say I have this code:

function doFileStuff(){
   
var file = "";
   
try {
        file
= fileOpen(filePath);
       
// do stuff with file
   
}
   
finally {
        fileClose
(file);
   
}
}

If the fileOpen() process fails, the fileClose() call will error. What I need to do is this sort of thing (pseudocode):

if (isFile(file)){
    fileClose
(file);
}

I know I can test if file is an empty string still, and this works for me here, but it's not testing what I should be testing: whether file is a file handle. I can check the object's Java class, but this again sounds a bit hacky to me, and there should be a CFML way of doing it.

There should be something like just isFile(), shouldn't there? I can't find anything like this in the docs.

Any thoughts / tips? I have gone into more depth in my investigations on my blog. it's too wordy for here.

Cheers for any help.

--

Adam

Views

734

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How do I know if this variable is a file handle?

Jul 27, 2012

Copy to clipboard

Copied

G'day

(This has also been posted on StackOverflow)

Say I have this code:

function doFileStuff(){
   
var file = "";
   
try {
        file
= fileOpen(filePath);
       
// do stuff with file
   
}
   
finally {
        fileClose
(file);
   
}
}

If the fileOpen() process fails, the fileClose() call will error. What I need to do is this sort of thing (pseudocode):

if (isFile(file)){
    fileClose
(file);
}

I know I can test if file is an empty string still, and this works for me here, but it's not testing what I should be testing: whether file is a file handle. I can check the object's Java class, but this again sounds a bit hacky to me, and there should be a CFML way of doing it.

There should be something like just isFile(), shouldn't there? I can't find anything like this in the docs.

Any thoughts / tips? I have gone into more depth in my investigations on my blog. it's too wordy for here.

Cheers for any help.

--

Adam

Views

735

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Jul 27, 2012

Copy to clipboard

Copied

Since "file = fileOpen(filePath)", can't you just remove the "file = ''" and check to see "if file" in the finally part?

^_^

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Jul 27, 2012

Copy to clipboard

Copied

That would just defer the issue.. fileOpen() doesn't return a boolean, so I can't go:

if (fileOpen(filePath)){

     fileClose(file);

}

fileOpen() returns a file object; or nothing if it fails.  The whole thing is to identify whether it's a file.  That's the question.

As per my original, it's dead easy to work around, provided one leverages known side effects of the situation (original variable state; that if it's a file it exposes some public properties; that one can doa  getClass() on it via Java, etc), but one shouldn't have to work around something as fundamental as this.  So I was wondering if I had missed something.

Seemingly not (based on feedback I've had from various quarters).

--

Adam

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Resources
ColdFusion User Guide
CFML Reference Guide
Develop CFML Applications
Add a group