Relative and absolute file names

Other discussions about CSPro
Post Reply
AriSilva
Posts: 312
Joined: July 22nd, 2016, 3:55 pm

Relative and absolute file names

Post by AriSilva » April 15th, 2019, 9:41 am

Is there a function to convert relative file names into absolute ones?
Best
Ari
Best
Ari

aaronw
Posts: 245
Joined: June 9th, 2016, 9:38 am
Location: Washington, DC

Re: Relative and absolute file names

Post by aaronw » April 16th, 2019, 5:12 pm

Are you wanting to canonicalize a path? For example convert "C:\abs\..\abc\file.txt" to "C:\abc\file.txt"? Just curious what is your use need for this?

AriSilva
Posts: 312
Joined: July 22nd, 2016, 3:55 pm

Re: Relative and absolute file names

Post by AriSilva » April 22nd, 2019, 12:10 pm

Hi Aaron,
I have a need to display the complete (canocalized) name of the directory where the reports will be stored.
This directory is informd in the program as a relative path, such as
dirname = ..\..\.._50_Reports
and this name is concatenated with the filename to store the specific report.
When I show the complete name of the report (including the directory name) I would like to show it as a its proper name, not the relative name.
I know this is not your priority (mine either), it was just to improve the software-user communication.
Best
Ari
Best
Ari

Gregory Martin
Posts: 1270
Joined: December 5th, 2011, 11:27 pm
Location: Washington, DC

Re: Relative and absolute file names

Post by Gregory Martin » April 23rd, 2019, 10:45 am

You could write a helper function that uses the filename function to get the full name:
function string GetAbsoluteFilename(string relative_filename)
    file temp_file;
    setfile(temp_file, relative_filename);
    GetAbsoluteFilename = filename(temp_file);
    close(temp_file);
end;

// ...

errmsg("%s", GetAbsoluteFilename("..\MyFile.txt"));
That assumes that the file exists.

AriSilva
Posts: 312
Joined: July 22nd, 2016, 3:55 pm

Re: Relative and absolute file names

Post by AriSilva » May 1st, 2019, 9:31 am

Great, Greg.
Thank you, that´s what I needed.
Best
Ari
Best
Ari

Post Reply