Solved

splitting a filename into its components

Posted on 1999-01-10
11
251 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
  • 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

831 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