## FilePathInfo()

Author: Jare
Edited: 11.3.2021 20:33
Category: Tiedostot

### Description

Erittelee annetusta polusta hakemiston, tiedoston kokonimen, tiedoston päätteen ja tiedoston nimen ilman päätettä. Toisella parametrilla valitaan, mikä näistä arvoista palautetaan, vaiko kaikki, jolloin arvot ketjutetaan yhteen |-merkillä. Nimessä on sana File, mutta tätä voi käyttää kansioillekin. Tiedostojärjestelmää ei lueta lainkaan, joten funktio ei piittaa tuon taivaallista, onko kyseessä tiedosto vain kansio, tai onko sitä edes oikeasti olemassa.

### Code

Select all
  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 // // Extracts details from a file/folder path (does not read filesystem in any way). The first parameter defines a file/folder path and the second parameter defines what to return: // - "dirname": Return the parent directory of a file or folder. // - "basename": Return a file's complete name (or a folder's name), but without the parent directory. // - "extension": Return a file's extension (or a folder's "extension", if the folder happens to have a dot in its name). The returned value does not contain a dot, and if the file/folder name happens to contain multiple dots, only the last dot is significant when determining the extension, i.e. for "myfile.inc.cb", the extension is ".cb", not ".inc.cb". // - "filename": Return a file's name without an extension. // - "all": Return a string containing all of the above separated by |, e.g. "C:\path\to\somewhere|myfile.txt|txt|myfile". // // Note: "/" will be replaced with "\" and trailing "\" will be removed! // Note: This function does not understand folder names . and .. ( . refers to "current directory" and .. to "parent directory"). Maybe they will be supported later? // Function FilePathInfo(path$, what$) // Support Force Variable Declaration Dim i, character$, collect$ Dim dirname$, basename$, extension$, filename$ // Returnable variables // Order the path To behave under tight rules! path = Replace(path, "/", "\") // Convert slashes To backslashes. If Right(path,1) = "\" Then path = Left(path,Len(path)-1) // Remove trailing backslash. // Do the hard work collect = "extension" For i = Len(path) To 1 Step -1 character\$ = Mid(path, i,1) If "." = character And "extension" = collect Then // End collecting a file extension And start collecting a filename basename = "." + basename collect = "filename" ElseIf "\" = character Then // End collecting a filename (Or file extension) And start To collect dirname. If "extension" = collect Then // Actually this path contains NO extension, so move the collected "extension" To filename. filename = extension extension = "" basename = filename // Should be already, because everything collected To extension is also collected To basename, but do this "just in Case" And "just For clarification". EndIf // Take a shortcut And don't iterate the String completely, because the rest belongs To dirname anyway. dirname = Left(path, i-1) // -1 leaves out the trailing backslash. Exit Else Select collect Case "filename" filename = character + filename basename = character + basename Case "extension" extension = character + extension basename = character + basename EndSelect EndIf If i=1 Then // We did Not encounter a backslash Select collect Case "extension" // We have collected a file extension, but actually it's a basename And filename. There is no extension, As we did Not encounter a Dot. basename = extension filename = extension extension = "" EndSelect EndIf Next i // Now just figure out what To Return Select what Case "dirname" : Return dirname Case "basename" : Return basename Case "extension" : Return extension Case "filename" : Return filename Case "all" : Return dirname + "|" + basename + "|" + extension + "|" + filename Default: MakeError "FilePathInfo('"+path+"', '"+what+"'): It's unclear what you want from me!" // Got no clue. EndSelect EndFunction