Re: Getting a filename and directory path from a string in RPG?



Korto wrote:

I have a string that looks like this:
"c:\alpha\beta\files\estimate.jpg" and I'd like to separate this string
into a path ("c:\alpha\beta\files") and a filename ("estimate.jpg").

Now, without using an array, is there any conceivable way to accomplish
this? I'd like to create a module where all I have to do is pass it
the string and the module will return the directory path and the
filename in separate variables.

I don't think CHECKR will help in this case. CHECKR '\' will return the
position of the last character that is not '\', which will (usually?)
just be the last character in your string. Well, you could build a
string of all the characters except '\', and CHECKR with that, but

I think something like this would work (untested).

P splitPath b export
D splitPath pi
D string 65535a const varying
D path 65535a varying
D filename 65535a varying
/free
path = '';
for i = %len(string) downto 1;
if %subst(string : i : 1) = '\';
path = %subst(string : 1 : i - 1);
filename = %subst(string : i + 1);
leave;
endif;
endfor;

if path = '';
// \ was not found, so the entire path is just the filename
filename = string;
endif;
/end-free
.



Relevant Pages

  • Re: A critique of cgi.escape
    ... backward compatability. ... Character encoding and character escaping are separate jobs with ... separate requirements that are better off handled by separate code. ... The resulting string might be a byte string or it might still be unicode: ...
    (comp.lang.python)
  • RE: Split non delimited data into multiple cells
    ... I used a column as a counter, and then used the mid statement in separate ... It separates each character into its own cell. ... charcter of the string in its own cell. ... I am importing an ascii file and can separate the data manually during ...
    (microsoft.public.excel.worksheet.functions)
  • Re: Still in while loop hell, Now with refined question!
    ... Besides all the advice on how to get a line in to your program, you have a separate problem of how to do the loop, and how to stop it. ... public String retrieveLine() ... Now you need to have an addmethod in NodeWorker. ... This is where you use that one part of your original program where you manipulate head and prev. ...
    (comp.lang.java.programmer)
  • Re: Extract data from String
    ... I need to split this data out into separate columns. ... a random length depending on the data in it. ... string as it is, the following one shows how it should be split up into ... The same functions can be run from within VBA using the RUN ...
    (microsoft.public.excel.programming)
  • Re: Custom Function
    ... That would certainly solve the need for the custom function ... if the values were in separate fields... ... > Regarding>The Val function returns the first set of consecutive ... > numbers are first in the string, ...
    (microsoft.public.access.reports)