ChLa
asked on
is there a filepath shortening procedure ?
Is there an existing procedure that will shorten a filepath in a string by one level ? I would use it to implement an "Up one Level" button for a ShellFileView
For Example, D:\Images\Raccoons would become D:\Images
For Example, D:\Images\Raccoons would become D:\Images
A better way is to use copy function to copy the entire path up to the last "\" in the provided path:
procedure TForm1.Button1Click(Sender: TObject);
var
Str:string;
begin
Str := 'D:\Images\Raccoons\';
if Str[length(Str)] = '\' then
delete(Str,length(Str),1); // to ensure the last slash in the path is for the parent directory
showmessage(Copy(Str,1,LastDelimiter('\',Str)));
end;
I have read in About.com another way also you may see useful which using "ExpandFileName" and provide the parent directory indicator "\.." so it returns the path of the parent directory:
procedure TForm1.Button1Click(Sender: TObject);
var
Str:string;
begin
Str:= 'D:\Images\Raccoons\';
Showmessage(ExpandFileName(Str + '\..'));
end;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If you are using the FileSystemObject, you can use the ParentFolder property.
http://msdn.microsoft.com/en-us/library/dt64ftxb(v=vs.85).aspx
http://msdn.microsoft.com/en-us/library/dt64ftxb(v=vs.85).aspx
ASKER
Thank you for your help and quick response. I ended up using the solution from Bobcsi. I am sure others would have worked also and I learn from the information even if I didn't use it this time.
I used that solution because it looked the shortest and so I tried it first and it worked.
I used it to implement an up button for a ShellFileView. Here is that code:
ShellListView1.Root := (ExtractFilePath(ExcludeTr ailingPath Delimiter( ShellListV iew1.RootF older.Path Name)));
I used that solution because it looked the shortest and so I tried it first and it worked.
I used it to implement an up button for a ShellFileView. Here is that code:
ShellListView1.Root := (ExtractFilePath(ExcludeTr
ASKER
Thank You
Your welcome!
There is a function that reads the folder path from the current filename "ExtractFileDir", so what I did is added a dummy extension to the folderpath and used that function so is stripes the filename and returns the parent directory:
Open in new window