Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

splitting a filename into its components

Posted on 1999-01-10
11
Medium Priority
?
257 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

719 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