Liberty BASIC Community Forum
« Function to get path, filename etc. »

Welcome Guest. Please Login or Register.
Jan 23rd, 2018, 7:25pm


Rules|Home|Help|Search|Recent Posts|Notification


« Previous Topic | Next Topic »
Pages: 1  Notify Send Topic Print
 thread  Author  Topic: Function to get path, filename etc.  (Read 209 times)
cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 630
xx Function to get path, filename etc.
« Thread started on: Jul 6th, 2016, 5:32pm »

I'm trying to improve this Function
Code:
print  "Drive",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 0 )
print  "Filename",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 1 )
print  "Path",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 2 )
print  "Extension",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 3 )


End


Function DriveFilenamePathOrExtension$( i$, i )
    q$=chr$(34)
    cmdList$= "upto$( afterlast$( upto$( afterlast$("
    a$ =  word$(cmdList$,i+1);q$;i$;q$;",";q$; word$(": \ "; afterlast$( i$ ,"\" );" ." , i+1);q$;  ")"
    DriveFilenamePathOrExtension$=eval$(a$)
End Function



Function aDriveFilenamePathOrExtension$( i$, i )
    a$ = "upto$(˝afterlast$(˝upto$(˝afterlast$("
    b$ = ":˝\˝";eval$("afterlast$( ";chr$(34);i$;chr$(34);",";chr$(34);"\";chr$(34);")");"˝."
    aDriveFilenamePathOrExtension$ = eval$( word$(a$,i+1,"˝") ;chr$(34); i$;chr$(34) ; ",";chr$(34);word$(b$,i+1,"˝");chr$(34);")" )
End Function
 

I think I should remove the 'Or' from the Function's name.
User IP Logged

cundo
Chris Iverson
Administrator
ImageImageImageImageImage


member is offline

Avatar

20% Cooler


Homepage PM

Gender: Male
Posts: 2294
xx Re: Function to get path, filename etc.
« Reply #1 on: Jul 6th, 2016, 11:06pm »

I would personally create four wrapper functions, GetDrive(), GetFilename(), etc, that just pass the proper call to your function. Makes it easier to understand the code that's calling the function.
User IP Logged

"Do you believe in destiny?" - Pyrrha Nikos, RWBY
"With what wish will your Soul Gem shine?" - Kyubey, Puella Magi Madoka Magica
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2834
xx Re: Function to get path, filename etc.
« Reply #2 on: Jul 7th, 2016, 08:14am »

If you're trying to be clever you're doing a good job. ;-)

Probably creating separate functions for each part of the filename is better because it will be easier to ensure correctness for each case. But your code seems to work pretty well.

Here is an example with more cases. Notice that the path that has a network resource instead of a drive letter doesn't work.

-Carl

Code:
print  "Drive",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 0 )
print  "Drive",DriveFilenamePathOrExtension$( "\\networkdrive\bas\tools\file.bas", 0 )
print
print  "Filename",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 1 )
print  "Filename",DriveFilenamePathOrExtension$( "C:\bas\tools\", 1 )
print
print  "Path",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 2 )
print  "Path",DriveFilenamePathOrExtension$( "\bas\tools\file.bas", 2 )
print  "Path",DriveFilenamePathOrExtension$( "C:\bas\tools\", 2 )
print
print  "Extension",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 3 )
print  "Extension",DriveFilenamePathOrExtension$( "C:\bas\tools\file", 3 )
End

Function DriveFilenamePathOrExtension$( i$, i )
    q$=chr$(34)
    cmdList$= "upto$( afterlast$( upto$( afterlast$("
    a$ =  word$(cmdList$,i+1);q$;i$;q$;",";q$; word$(": \ "; afterlast$( i$ ,"\" );" ." , i+1);q$;  ")"
    DriveFilenamePathOrExtension$=eval$(a$)
End Function 
« Last Edit: Jul 7th, 2016, 08:14am by CarlGundel » User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
cundo
Guru
ImageImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 630
xx Re: Function to get path, filename etc.
« Reply #3 on: Jul 14th, 2016, 10:46am »

I don't know much about networks, does this little cheat works?
Code:
print  "Drive",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 0 )
print  "Drive",DriveFilenamePathOrExtension$( "\\networkdrive\bas\tools\file.bas", 0 )
print
print  "Filename",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 1 )
print  "Filename",DriveFilenamePathOrExtension$( "C:\bas\tools\", 1 )
print
print  "Path",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 2 )
print  "Path",DriveFilenamePathOrExtension$( "\bas\tools\file.bas", 2 )
print  "Path",DriveFilenamePathOrExtension$( "C:\bas\tools\", 2 )
print
print  "Extension",DriveFilenamePathOrExtension$( "C:\bas\tools\file.bas", 3 )
print  "Extension",DriveFilenamePathOrExtension$( "C:\bas\tools\file", 3 )
End

Function DriveFilenamePathOrExtension$( i$, i )
    cmdList$= "upto$( afterlast$( upto$( afterlast$("
    q$=chr$(34)

    if instr(i$,":")=0 then
    i$ = replstr$(i$, "\\","")
         net$="\"
        else
        net$=":"
    end if

    a$ =  word$(cmdList$,i+1);q$;i$;q$;",";q$; word$( net$;" \ "; afterlast$( i$ ,"\" );" ." , i+1);q$;  ")"
    DriveFilenamePathOrExtension$=eval$(a$)
End Function
 

Are there any ":" when using network paths?
User IP Logged

cundo
CarlGundel
Administrator
ImageImageImageImageImage


member is offline

Avatar

author of Liberty BASIC


Homepage PM


Posts: 2834
xx Re: Function to get path, filename etc.
« Reply #4 on: Jul 16th, 2016, 4:13pm »

The network drive really should still have the \\ in front of it.

-Carl
User IP Logged

author of Liberty BASIC
http://www.libertybasic.com
Pages: 1  Notify Send Topic Print
« Previous Topic | Next Topic »

Rules|Home|Help|Search|Recent Posts|Notification

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls