Subject | Re: [firebird-support] How to Extract Filename from file path inside a stored procedure? |
---|---|
Author | Mark Rotteveel |
Post date | 2015-09-17T19:30:32Z |
On 17-9-2015 16:19, 'stwizard' stwizard@... [firebird-support] wrote:
EXECUTE BLOCK RETURNS (filename VARCHAR(1024))
AS
DECLARE filepath VARCHAR(1024);
DECLARE searchIndex int;
DECLARE previousIndex int;
BEGIN
filepath = 'K:\Frontline\Documents\Aids\SNKSAid.dat';
previousIndex = 0;
searchIndex = position('\', filepath);
WHILE (searchIndex > 0) DO
BEGIN
previousIndex = searchIndex;
searchIndex = position('\', filepath, previousIndex + 1);
END
filename = substring(filepath from previousIndex + 1);
SUSPEND;
END
For Firebird 1.5, you would need to find equivalent UDFs for position
and substring (and of course you can't use execute block in 1.5, but I
only used it to demonstrate the solution).
Mark
--
Mark Rotteveel
> Firebird v1.5.3 and v2.5.4For Firebird 2.5 it is simpler than for Firebird 1.5.
> I need a way to extract just the file name for a given file path eitherI Firebird 2.5 you can do:
> by code in a stored procedure or by calling a UDF in the stored procedure.
>
> Example, I need to extract “SNKSAid.dat” from
> “K\Frontline\Documents\Aids\SNKSAid.dat”
>
> Any ideas?
EXECUTE BLOCK RETURNS (filename VARCHAR(1024))
AS
DECLARE filepath VARCHAR(1024);
DECLARE searchIndex int;
DECLARE previousIndex int;
BEGIN
filepath = 'K:\Frontline\Documents\Aids\SNKSAid.dat';
previousIndex = 0;
searchIndex = position('\', filepath);
WHILE (searchIndex > 0) DO
BEGIN
previousIndex = searchIndex;
searchIndex = position('\', filepath, previousIndex + 1);
END
filename = substring(filepath from previousIndex + 1);
SUSPEND;
END
For Firebird 1.5, you would need to find equivalent UDFs for position
and substring (and of course you can't use execute block in 1.5, but I
only used it to demonstrate the solution).
Mark
--
Mark Rotteveel