December 04, 2020, 11:11:06 AM

Author Topic: [bb] Get Filename/Folder/Extension by Nebula [ 1+ years ago ]  (Read 664 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
Title : Get Filename/Folder/Extension
Author : Nebula
Posted : 1+ years ago

Description : I was modifying some code and I needed only the filename from a string that contained the entire path. So I coded a function for this and two others just for the heck of it.

filename$ = getfilename$(path_and_file)$
extension$ = getextension$(path_and_file)$
directory$ = getdirectory$(path_and_file)$

filename$ would look like "renamed.bmp"
extension$ would look like "bmp"
directory$ would look like "c:gamespacmanIV"


Code :
Code: BlitzBasic
  1. Function getfilename$(filename$) ; Returns the filename and extension
  2.         lastdir = 1
  3.         For i=1 To Len(filename$)
  4.                 If Mid$(filename$,i,1) = "" Then Lastdir = i
  5.         Next
  6.         If Lastdir > 1 Then Lastdir = Lastdir + 1
  7.         For i=Lastdir To Len(filename$)
  8.                 a$ = a$ + Mid(filename$,i,1)
  9.         Next
  10.         Return a$
  11. End Function
  12.  
  13. Function getextension$(filename$) ; Returns the extension minus the .
  14.         lastdir = 1
  15.         For i=1 To Len(filename$)
  16.                 If Mid$(filename$,i,1) = "." Then Lastdir = i
  17.         Next
  18.         If Lastdir > 1 Then Lastdir = Lastdir + 1
  19.         For i=Lastdir To Len(filename$)
  20.                 a$ = a$ + Mid(filename$,i,1)
  21.         Next
  22.         Return a$
  23. End Function
  24.  
  25. Function getdirectory$(filename$) ; Returns the complete directory including drive
  26.         lastdir = 1
  27.         For i=1 To Len(filename$)
  28.                 If Mid$(filename$,i,1) = "" Then Lastdir = i
  29.         Next
  30.         For i=1 To Lastdir
  31.                 a$ = a$ + Mid(filename$,i,1)
  32.         Next
  33.         Return a$
  34. End Function


Comments : none...

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal