Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking file selected is the correct type

Status
Not open for further replies.

keepingbusy

Programmer
Apr 9, 2000
1,470
GB

Hi

I am using the following to allow users to select a CSV file:
Code:
gcCSV=GETFILE('CSV', 'Open and import .CSV:','Browse', 1, 'Open and import')
IF RIGHT(gcCSV,3)<>"csv" OR RIGHT(gcCSV,3)<>"CSV"
  =MESSAGEBOX("File selected is NOT a valid CSV file"+ ;
    SPACE(10),0+64+0,"System Message")
   RETURN
ENDI
I included the IF RIGHT.. statement to ensure a CSV file was selected. However, if you select a csv file or any other file you still get the same message.

Any suggestions please?

Thank you
Lee
 
Lee,

I'm afraid your logic is wrong:

RIGHT(gcCSV,3)<>"csv" OR RIGHT(gcCSV,3)<>"CSV"

will always be true. Think about it. RIGHT(gcCSV,3) must be unequal to one or other of the strings.

The following would work:

NOT (RIGHT(gcCSV,3)="csv" OR RIGHT(gcCSV,3)="CSV"))

Going a couple of steps better, why not use JUSTEXT() instead of RIGHT(). That would handle the case where the extension has more than three letters, or where there is no extension at all. You could also combine it with UPPER(), in case the extension is neither all lower case or all caps.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Hello Lee:

try this:
use the UPPER() clause and you could test with
cf = getfile()
? IIF(ALLTRIM(UPPER(JUSTEXT(cf))) <> "CSV",.f.,.t.)
 

Mike and imaginecorp

Many thanks.
Code:
IF NOT (RIGHT(gcCSV,3)="csv" OR RIGHT(gcCSV,3)="CSV")
My only question here is though, what if the file is something like myfile.CSV?

Would this still stop the file from being accepted as the command is saying "If the last three characters of the variable = CSV then its not a valid file?

I might be missing something here?

imaginecorp: Have to admit I hadn't heard of the command JUSTEXT()

Lee
 

Lee,

JUSTEXT() returns the three-letter extension from a complete path. You can also check out JUSTFNAME(), JUSTPATH() and JUSTSTEM().

You don't have to check for both, "CSV" and "csv". What if you have file with an extension "cSv"? Would you want it to be accepted? You should probably use UPPER() or LOWER() function to check for all of those cases at once. So use

either
IF UPPER(RIGHT(gcCSV,3))#"CSV"
or
IF UPPER(JUSTEXT(gcCSV))#"CSV"

I didn't understand what's your concern, though (about myfile.CSV)?
 
Lee,

My only question here is though, what if the file is something like myfile.CSV?

But isn't that exactly the case you are testing for?

I suggest you go back and read what I said about JUSTEXT() and UPPER(). It should answer all your concerns.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 

Stella
You don't have to check for both, "CSV" and "csv".
That makes it easier then.
I didn't understand what's your concern, though (about myfile.CSV)?
My concern was that some files have extensions in both upper and lower case (e.g. CSV and csv)

I take your point about cSv but you have answered that as above.

Mike
But isn't that exactly the case you are testing for?
Yes, but I didn't quite understand if you needed to look for both upper and lower case extentions which Stella has now answered.

Thank you all

Lee
 
Hello Lee:

I am not sure when Justext() etc were introduced, but it will definately work in 9.

You do not need Right() etc. try the following and it will work for you.
Code:
gcCSV=Getfile('dbf', 'Open and import .CSV:','Browse', 1, 'Open and import')
csvfile = Iif(Upper(Justext(gcCSV)) # "CSV",.F.,.T.)
If !csvfile
	=Messagebox("File selected is NOT a valid CSV file"+ ;
		SPACE(10),0+64+0,"System Message")
	Return
Endif
 

I am not sure when Justext() etc were introduced, but it will definately work in 9.

At least in VFP6 they were already there. May be even earlier - not sure.


 

Imaginecorp

Just noticed:
Code:
gcCSV=Getfile('[b]dbf[/b]', 'Open and import .CSV:','Browse', 1, 'Open and import')
Changed to:
Code:
gcCSV=Getfile('[b]csv[/b]', 'Open and import .CSV:','Browse', 1, 'Open and import')
For anyone viewing this thread and requiring a similar sollution.

Thanks again guys
Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top