Is when I can import a file.So, I want know how to detect the files we could import.
It depends, if you don’t have PCFILES you can’t open an XLSX file. Your criteria is still too broad.
Vague question = vague answer.
How would you detect that a .txt file is "wrong"? Once you know that, you can create code out of this algorithm. Note that you will have to read the file, and determine its state by the contents you read.
One thing we use a lot is a file header (the first line in the file) and a file footer (the last line in the file).
In the header number of rows are specified, also file creation date and name.
By reading the header we get information about the file which we then use to make an initial check if it's ok.
If so we stop.
If ok, we read the file and if we reach the footer we check the number of rows again.