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
ChLaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jimyXCommented:
There are many options actually.
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:
procedure TForm1.Button1Click(Sender: TObject);
var
  s:string;
begin
  s:= 'D:\Images\Raccoons';
  if s[length(s)] = '\' then
    delete(s,length(s),1);
  showmessage(ExtractFileDir(s+'.ext'));
end;

Open in new window

0
jimyXCommented:
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;

Open in new window

0
jimyXCommented:
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;

Open in new window

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

BobcsiCommented:
This is simple:
function UpOneLevel(Path: string): string;
begin
  result := ExtractFilePath(ExcludeTrailingPathDelimiter(path));
end;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
If you are using the FileSystemObject, you can use the ParentFolder property.
http://msdn.microsoft.com/en-us/library/dt64ftxb(v=vs.85).aspx
0
ChLaAuthor Commented:
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(ExcludeTrailingPathDelimiter(ShellListView1.RootFolder.PathName)));
0
ChLaAuthor Commented:
Thank You
0
BobcsiCommented:
Your welcome!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.