Filename function

Discussions about CSEntry
Forum rules
New release: CSPro 8.0
Post Reply
Boni
Posts: 75
Joined: February 25th, 2020, 5:53 am

Filename function

Post by Boni »

I use the filename () function to get and write in a text file the file names in a given path. For example, for hhd01, hhd02,...,hhd(n) files contained in the "HHData" folder, I get:
C:\MySurvey\HHdat\hhd01.csdb
C:\MySurvey\HHdat\hhd02.csdb
....

On Android devise I cet :
/storage/emulated/0/csentry/MySurvey/HHdata/hhd01.csdb
.....

I want to use this file for another program using the "FileRead" function, and as you noticate with me, it would be complicated to use the same function at a time on PC as well as on Android devise.
So, I would like to know how to get only the file name (hhd01, hhd02, ...) instead of having the path included as described above.
Yours advises please ??? :roll:
Sincerely yours !
josh
Posts: 2399
Joined: May 5th, 2014, 12:49 pm
Location: Washington DC

Re: Filename function

Post by josh »

There is no built in function to extract just the name. You can use loop through the string from the end going backwards to find the last / and then take the substring from the last / to the end of the string.
Boni
Posts: 75
Joined: February 25th, 2020, 5:53 am

Re: Filename function

Post by Boni »

It's really interesting. I tried something like this, with two counters "i" and "j", one that takes the lengh of the whole string, and the other that counts from a special character ";" or "space" in evolving by 1 or -1. But it didn't work, I didn't I did not succeed to do it.I had to do it manually by counting the positions on the string, and as soon as something changes, it's screwed up. :x
Dear Josh !!! Can I have an example please? If it's not too much
Thanks
Sincerely yours !
Gregory Martin
Posts: 1777
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Filename function

Post by Gregory Martin »

Here is a function that you can use:
function string GetFilename(string full_path)

    // calculate where the directory slash is
   
do numeric counter = length(full_path) while counter > 1 by -1

       
if full_path[counter:1] in '/','\' then
            exit
full_path[counter + 1];
       
endif;

   
enddo;

    // return the full path if no slash was found
   
exit full_path;

end;
Boni
Posts: 75
Joined: February 25th, 2020, 5:53 am

Re: Filename function

Post by Boni »

Thank you Dear Gregory !!! I'll try it
Sincerely yours !
Post Reply