Hi,
I am using Dosfilename lookup to return a file. However this is returning the path and filename.
Is there another command to just select and return the filename or do I need to strip out the path of the returned value ?
C10
Thanks
Hi,
I am using Dosfilename lookup to return a file. However this is returning the path and filename.
Is there another command to just select and return the filename or do I need to strip out the path of the returned value ?
C10
Thanks
Easy to separate path & filename after lookup. See help for fnsplit.
The Name is after the last Backslash
BS USHORT
BS=INSTRING('\',PathPlusName,-1,SIZE(PathPlusName)) !-1 Reverse step, must start at the end i.e. size
NameOnly=SUB(PathPlusName,BS+1,999)
Much easier to use PathSplit thatâs found in CWUTIL
PROGRAM
MAP
include('CWUTIL.INC'),once
END
theFullPathAndFileName cstring(261)
theFileName cstring(81)
CODE
if filedialog('Select a File',theFullPathAndFileName,'All Files|*.*')
PathSplit(theFullPathAndFileName,,,theFileName)
message('FileName ' & theFileName)
end
Itâs not documented anywhere but PathSplit returns an unsigned that shows you what parts of a full file path where present using these equatesâŚ
extension equate(2)
file equate(4)
wildcards equate(5)
folder equate(8)
drive equate(16)
Looking in CwUtil.INC PathSplit()
is simply renamed from the RTL C Library _fnsplit
. You would need to use all CSTRINGâs to call it. Iâm surprised SV did not create a wrapper function that took Clarion STRINGâs.
MODULE('Core')
PathSplit(CONST *CSTRING path, <*CSTRING drive>, <*CSTRING dir>, <*CSTRING file>, <*CSTRING ext>), SIGNED, PROC, RAW, NAME('_fnsplit')
PathMerge(*CSTRING path, <*CSTRING drive>, <*CSTRING dir>, <*CSTRING file>, <*CSTRING ext>), SIGNED, PROC, RAW, NAME('_fnmerge')
END
You could just declare these C functions in your program, but use different names. I would create a wrapper that took STRINGs ⌠well I would use my posted 2 lines of code and be done.
Edit 11/11/2022:
In the Legacy2ABC project I ran into code calling Split. It had declared Name CSTRING(9)
for 8.3 names. That failed with a LFN to return .EXT. Since its RAW it does not know the sizes of the CSTRINGâs so overran the 9 bytes and wrote into the Extension.
FNInfoType GROUP,TYPE
Path CSTRING(File:MaxFilePath)
Drive CSTRING(3)
Directory CSTRING(File:MaxFilePath)
Name CSTRING(FILE:MaxFileName)
Extension CSTRING(32)
END
FileExtension PROCEDURE(STRING FullName)
FN LIKE(FNInfoType),AUTO
CODE
FN.Path = CLIP(FullName)
FNSplit(FN.Path,FN.Drive,FN.Directory,FN.Name,FN.Extension)
RETURN UPPER(SUB(FN.Extension,2,LEN(FN.Extension)-1))
Here is a class I wrote that wraps FnSplit, allowing you to use STRING vs. CSTRING
CwUnit/ctFileHelper.clw at master ¡ MarkGoldberg/CwUnit (github.com)
StringTheory also has a FileNameOnly method, as well as ExtensionOnly and PathOnly.
Using StringTheory;
filename = str.FileNameOnly(PathPlusName)
https://www.capesoft.com/docs/StringTheory3/StringTheory.htm#FileNameOnly
(thereâs an option to have it with, or without, the extension.)
I try not to do too much 3rd party promotion here, but honestly if you are writing code, and youâre not using StringTheory then you are just wasting time each and every day.
Cheers
Bruce
I canât recommend StringTheory (Capesoft) enough. It does all this stuff and so much more!
Mark
longpath() & âfileA.tpsâ works well
Welcome May_Woo. That would indeed work if you were constructing the name. But in this case Sim has the full name and wants to split out just the filename part (ie removing the path part.) So the opposite of your code.
Incidentally itâs not a great idea to use LongPath() in your code to specify file locations, because this is relative to the âcurrent directoryâ. Since that can be changed (like on the windows shortcut) this can lead to strange bugs. The current directory may also change when using FILEDIALOG, so you get the effect where âdata goes missingâ after a file lookup is done.
Lastly, LongPath defaults to the Exe folder, and thatâs a really bad place to put your data these days. So, all in all, the use of LongPath is a signal that there is something ânot quite rightâ in the way you are doing things.
cheers
Bruce
Bruce thanks for this. I was actually using longpath to get the directory / file and I was actually getting some strange issues when trying to set the default path.
I personally use StringTheory but this is an elegant, non 3rd party, solution.