How do I get the filename?

Previous topic - Next topic

Alex_R

Hi!!

With this code:
ok = OPENFILE(channel%, file$, mode%)

I get the full path to my file. But, how do I get just the name of my file?

MrTAToad

You would have to work it out yourself unfortunately - the name is precedent by either a \ or / (or perhaps nothing at all).

Kitty Hello

"get"? Did you mean FILEREUEST$? I don't understand the question.

Slydog

You can use this function to retrieve the file name portion from a full path:
Code (glbasic) Select

FUNCTION File_GetName$: path$, slash$="/"
LOCAL slash%
slash = REVINSTR(path$, slash$)
IF slash <=0 THEN RETURN path$
RETURN MID$(path$, slash+1)
ENDFUNCTION


It's usage:
Code (glbasic) Select
DEBUG(File_GetName$("//Web/WorldWide"))
DEBUG(File_GetName$("Internet"))
DEBUG(File_GetName$("../Graphics/pics/"))
DEBUG(File_GetName$("C:\\GLBasic\\Rocks.com", "\\"))

Note the need for the double '\\' when using that slash.

Outputs:
Code (glbasic) Select
WorldWide
Internet

Rocks.com
My current project (WIP) :: TwistedMaze <<  [Updated: 2015-11-25]

Alex_R

Yes Kitty. If I use file$=FILEREQUEST$(TRUE, "Text|*.txt|All|*.*")  I have the same problem.
file$ returns the full path and I just need the name of that file and extension.

Wow Slydog!!. A function that checks where is the last "/"  to get the file name. Great!!!

Many thanks!!

Hemlos

Data stripping is important obviously.
Recently, i needed to know which files were bmp and png.
So here is how i checked for file extensions..

GLOBAL splits$[]
splitnum=SPLITSTR( M.List$, splits$[], "." ) //split everything with dots to check file ext

FOR j=0 TO splitnum-1
  IF (splits$[j] = "bmp") OR (splits$[j] = "BMP") THEN IsImg=TRUE
  IF (splits$[j] = "png") OR (splits$[j] = "PNG") THEN IsImg=TRUE
NEXT

IF IsImg=TRUE then ...blahblahblah
               
Bing ChatGpt is pretty smart :O

Kitty Hello

that would return true for "bmp.png"
Better: IF LCASE$(RIGHT$(filename$, 4)) = ".png" ...

Hemlos

#7
Quote from: Kitty Hello on 2011-Jan-10
that would return true for "bmp.png"
Better: IF LCASE$(RIGHT$(filename$, 4)) = ".png" ...

Cool, Thanks, fixing it now!

Code (glbasic) Select
SELECT LCASE$(RIGHT$(M.List$[i], 4))
CASE ".bmp"; IsImg=TRUE
CASE ".BMP"; IsImg=TRUE
CASE ".png"; IsImg=TRUE
CASE ".PNG"; IsImg=TRUE
DEFAULT; IsImg=FALSE
ENDSELECT
Bing ChatGpt is pretty smart :O

Moebius

Why do you need to check for uppercase extensions when using LCASE$() ?  :S
Endless Loop: n., see Loop, Endless.
Loop, Endless: n., see Endless Loop.
- Random Shack Data Processing Dictionary

Hemlos

#9
Dont you hate redundancies?  :bed:

CASE ".bmp" OR ".png"; IsImg = TRUE

Thanks :)
Bing ChatGpt is pretty smart :O

Hemlos

Woops that made the IDE go crazy with errors.
CGStr' to `bool' is ambiguous
you can say that again.

CASE ".bmp"; IsImg = TRUE
CASE ".png"; IsImg = TRUE

this works
Bing ChatGpt is pretty smart :O