Hi everyone.
I am using PB 2019 R3 2703. We have a problem in our PB application with users being able to attach corrupt files. I want to be able to check whether a file is corrupt before I allow the attachment, but this is not as easy as I thought.
FileOpen succeeds (returns > 0) even when opening a corrupt file, so I can't use that to check. I've tried using ShellExecute, CreateProcess, etc but they also seem to indicate success on opening a corrupted file (I'm probably missing something). I'm playing around with OLE and that seems to be the most promising but even this has it's issues.
So my question is, does anyone out there know of an easy way to use PowerBuilder to determine whether a file is corrupted? If it makes any difference, I only need to check files of type .txt, .pdf, .jpg, .jpeg, .gif, .doc, .docx, .msg, .png, and .html.
Any advice would be greatly appreciated!
Thanks,
Nathan
(my first post to a PowerBuilder forum in many years. It is so good to be back on the PowerBuilder bus!)
Thanks for replying.
I take your point about corrupt txt files, I was already considering excluding them from the proposed check I'm trying to implement.
'You will need to try to open each file type with its corresponding application and see if it can be opened or it throws an exception' - yep that's what I'm trying to do using OLE ConnectToObject and it works well with pdfs, which is the only file type that has caused issues so far, but because the users can attach several different types, I can't really tell my manager I'm only going to check for pdfs and just hope everything else is OK.
The issues I have with ConnectToObject are
- it is always returning -7 for .txt files but as I said, I might exclude them anyway
- I get different results for .doc and .docx files. docx works exaclty as I want, returns -7 for files that can't be opened, 0 for valid files. It does the same for .doc files, eventually, but it pops up the dialog shown in my next response (I don't seem to be able to post a pic to this reply) in the background, which I do not want.
- it returns success for .png files that cannot be opened in drawing apps like paint etc.
For pictures, I've seen in this forum that Imagemagik is frequently used in PowerBuilder, maybe you could try opening or getting information from these files with Imagemagik and catch any errors. A problem would be that sometimes image file corruption cannot be detected by looking at the file itself, and it's only visible when opening the image and seeing visual artifacts. Not sure what kind of files behave like this though.
But, as I suspected, it seems the simple answer to my question 'is there an easy way to use PowerBuilder to determine whether a file is corrupted' is 'No'.
Cheers