Solved

splitting a filename into its components

Posted on 1999-01-10
11
246 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
Comment Utility
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 20

Expert Comment

by:Madshi
Comment Utility
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
Comment Utility
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
Comment Utility
Wow, thank you for this compliment. It's always nice to hear something like that...  :-)))
0
 

Author Comment

by:mathes
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

728 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now