Solved

splitting a filename into its components

Posted on 1999-01-10
11
254 Views
Last Modified: 2010-04-06
Hi experts,

suppose we have a file:

filename:='c:\programs\windows\my file.txt';

how can I split this filename into its components?

drive:='c:';
directory:='\programs\windows\';
name:='my file';
extension:='.txt';


With kind regards

Mathes
0
Comment
Question by:mathes
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
11 Comments
 
LVL 1

Accepted Solution

by:
martin_g earned 10 total points
ID: 1355697
var
FileName,drive,directory,name,ext:string;
begin
FileName:='c:\programs\windows\my file.txt';
drive:= ExtractFileDrive(FileName);
{need directory w/o drive}
directory:= ExtractFileDir(FileName);
Delete(directory,1,2);
ext:=ExtractFileExt(FileName);
{now for the name w/o the extension}
name:=ExtractFileName(FileName);
if Pos(ext,name)>0 then
  begin
  Delete(name,Pos(ext,name),Length(name));
  end;

end;
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355698
Hi Mathes, looks as if you would have posted this question twice...  :-)

Martin,
(1) Your drive looks like this "c:\" and your directory like this "\programs\windows\". So there's a backslash too much.
(2) Your directory Delete(...,1,2) doesn't work with UNC paths
(3) The length in your last Delete is wrong. If the extension is longer than the filename (e.g. "a.txt") your name will look like this "a.xt"

Mathes, perhaps you should use my solution in the other question...  :-)))

Regards, Madshi.
0
 

Author Comment

by:mathes
ID: 1355699
Dear Madshi,

I am afraid I don't understand you. I think your function for example will never return an extension. Are there functions abvailablke in Delphi like Fsplit and fexpand, as in Borland Pascal?

With kind regards

Mathes
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 20

Expert Comment

by:Madshi
ID: 1355700
Hi Mathes,

please try my solution. It works perfectly. I tested it...  :-)
I don't know fsplit and fexpand. But the Delphi functions Martin and I used, are not bad, are they?

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:martin_g
ID: 1355701
Hi from Mathes from Martin and also to you Madshi.  Madshi, you may be right about the UNC aspect but I wasn't sure if that's  something Mathes might want to consider.

Mathes, I tried out the code I gave you using an edit box to enter file names and labels for each of the variables (drive, directory,etc) and the results always gave the same format as what you needed.  The drive was c: (no backslash), directory alsways started with a backslash, name had no extension and extension ( if there was one) always returned with the period.  BTW the extension could be any length from 1 to whatever characters.

Try it, you'll like it :)

Regards,
Martin

BTW - Thanks for your answers Madshi, you've helped me a lot!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355702
Oooops.

Martin, I was wrong with the double backslash and also with the name. I've my own ExtractFileDrive function that adds always a backslash to the drive, so I got confused by that.
So the only problem with your code is perhaps the UNC stuff. But how about changing the line
    Delete(directory,1,2);
to  Delete(directory,1,length(drive));
Then it should be working with UNC paths, too. Right?

Mathes, I think you should accept Martin's answer, because he was some minutes faster with his answer than I was (on the doubled question you've posted)...   :-)

Regards, Madshi.

P.S: Martin, did I answer some of your questions? I can't remember it...  :-)
0
 
LVL 1

Expert Comment

by:martin_g
ID: 1355703
Madshi,
No you didn't answer some of my questions, but questions others had that I myself had, you answered!  I 've always appreciated your clear responses.  thanks!
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355704
Wow, thank you for this compliment. It's always nice to hear something like that...  :-)))
0
 

Author Comment

by:mathes
ID: 1355705
Hi experts,

thank you for your help. All your hints and thoughts are very valuable for me.
I tested the code and found out that it works perfectly, just as I desire it.
The source even can handle long filenames and filenames containing blanks
and is not limited to DOS 8.3 filenames.
BTW: What does the abbreviation "UNC" stand for? I am not familiar with this abbreviation.

With kind regards

Mathes
0
 
LVL 20

Expert Comment

by:Madshi
ID: 1355706
Hmmm. I'm not sure... Perhaps "Universal Network Convention" or "Universal Naming Convention" ?
However, it means paths like this: "\\server-name\share-name\dir\file.ext". Where "\\server-name\share-name" is the volume name, "\dir\" is the directory...

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:martin_g
ID: 1355707
Madshi is correct.  It stands for Universal Naming Convention.
There's a pretty good site that describes it at:
http://webopedia.internet.com/Operating_Systems/DOS/UNC.html
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question