How to find if a path contains a valid file name

Post your Gambas programming questions here.
Post Reply
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

How to find if a path contains a valid file name

Post by bill-lancaster »

I need to know if sVar is just a directory or if it contains a file name
This is best I can do:-

Code: Select all

Dim sVar as string
sVar = User.Home &/ "00.mp3"
Print IsDir(File.Dir(sVar) &/ File.Name(sVar))
Is there a better way?
User avatar
cogier
Site Admin
Posts: 1118
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: How to find if a path contains a valid file name

Post by cogier »

You can reduce the code to: -
Print IsDir(User.Home &/ "00.mp3")
User avatar
Serban
Posts: 39
Joined: Saturday 28th March 2020 8:17am
Location: Alexandria
Contact:

Re: How to find if a path contains a valid file name

Post by Serban »

That's easy: :)
Dim strFilePath As String
'--- It's either you get the path building it in a procedure, OR, just build the string, according to a specific situation
'--- strFilePath= Application.Path & "/App.conf" '--- it's just an example, can be more ellaborate than that.
If Exist(strFilePath) Then
  '--- DoWhatIsRequired()
Else
  '--- BranchForErrors()
End If
The other approach, would be using the Dir(), then IsDir(), but it depends very much on the specifics of the App.
The only thing necessary for the triumph of evil is for good men to do nothing.”― Edmund Burke;
It's easy to die for an idea. It is way harder to LIVE for your idea! (Me)
User avatar
Serban
Posts: 39
Joined: Saturday 28th March 2020 8:17am
Location: Alexandria
Contact:

Re: How to find if a path contains a valid file name

Post by Serban »

bill-lancaster wrote: Sunday 29th March 2020 9:37am I need to know if sVar is just a directory or if it contains a file name
Are you SURE YOU need to know?
Why would you need?
In my view, THE COMPUTER needs to know. Your approach is from the human perspective. It has nothing to do with what the computer needs to know.
Try the sample code I put before this comment.
Also, try to get the computer's perspective, since me, or you, or anyone else as users, we need buttons and labels and the like, while the computer, needs a different type of data: figures. Numbers.
The only thing necessary for the triumph of evil is for good men to do nothing.”― Edmund Burke;
It's easy to die for an idea. It is way harder to LIVE for your idea! (Me)
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: How to find if a path contains a valid file name

Post by bill-lancaster »

Thanks for the replies and thank you cogier, IsDir() does the job. File.Dir() will truncate the path string if there is no '/' at the end.
Thanks again
User avatar
Cedron
Posts: 156
Joined: Thursday 21st February 2019 5:02pm
Location: The Mitten State
Contact:

Re: How to find if a path contains a valid file name

Post by Cedron »

.... and carry a big stick!
bill-lancaster
Posts: 190
Joined: Tuesday 26th September 2017 3:17pm
Location: NW England

Re: How to find if a path contains a valid file name

Post by bill-lancaster »

Thanks Cedron
Post Reply