Page 1 of 1

Filename function

Posted: June 25th, 2020, 1:10 pm
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:

Re: Filename function

Posted: June 25th, 2020, 1:35 pm
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.

Re: Filename function

Posted: June 25th, 2020, 3:25 pm
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

Re: Filename function

Posted: June 25th, 2020, 4:47 pm
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;

Re: Filename function

Posted: June 25th, 2020, 5:13 pm
by Boni
Thank you Dear Gregory !!! I'll try it