Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

splitting a filename into its components

Posted on 1999-01-10
11
Medium Priority
?
258 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 40 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

971 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