Link to home
Create AccountLog in
Avatar of wqclatre
wqclatre

asked on

Extract Directory Name and file name without extention

How can I extract the name of a directory.

For example:

i have a file in: C:\hubba\hopp\test\nusse.txt
And I just want to know the 'test' name and 'nusse' (without .txt)
Avatar of calinutz
calinutz
Flag of Romania image

How do you obtain the file ? Do you get it from an OpenDialog dialog? Or do you have it in a textbox (Edit)?
Avatar of Mohammed Nasman

function GetFileName(s:string):string;
var
  f,ex : string;
begin
  ex := ExtractFileExt(s);
  f := ExtractFileName(s);
  Result := Copy(f,1,Length(f) - Length(ex));
end;


procedure TForm1.btn1Click(Sender: TObject);
begin
  ShowMessage(GetFileName('C:\hubba\hopp\test\nusse.txt'));
  ShowMessage(ExtractFileDir('C:\hubba\hopp\test\nusse.txt'));
end;
Avatar of wqclatre
wqclatre

ASKER

Depends....

In my program I have a save dialog that Save a file to a directory and some files to a subdirectory. (something like when you save a webpage)

for example say that i save a project from my program and name the project to "MyProj"
and save it in c:\test

a file c:\test\MyProj.xml will be created.
also a directory c:\test\MyProj\ will be created (containing data files or my project.

the MyProj.xml will look like: (more complicated but just so you get an example:

<xml>
<file1>c:\test\myProj\file1.txt</file1>
<file2>c:\test\myProj\file2.txt</file2>
</xml>


I realise that it's not so smart to have c:\test in the file. Just myproj\file1.txt etc would be better (then the files can be moved to other locations on the disc.

So here is one place. The other is in my load fucntion where i parse the xml file. (I still want the old xml files to be able to load)





ShowMessage(ExtractFileDir('C:\hubba\hopp\test\nusse.txt'));
 will give the whole :c:\hubba\hopp\test
not only test
ASKER CERTIFIED SOLUTION
Avatar of Mike Littlewood
Mike Littlewood
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account