Dos filename lookup how to get File Name without Path

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)
2 Likes

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)

2 Likes

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.

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)

1 Like

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

1 Like