Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving file name without extension

Status
Not open for further replies.

imagenetics

Programmer
Dec 8, 2003
66
PL
ExtractFileName() includes extension of a file. How to read a name of a file pointed via Open dialog and put it into a label without the extension. For example, I select a "C:\file.txt" file and I want to see "file" displayed.
 
but how would you like to see such file as C:\file.txt.txt.txt or C:\file.txt.com.doc.avi.java?

Ion Filipski
1c.bmp
 
I'd like to see exactly as it is displayed in the Explorer with "Display extensions..." options disabled. As I said I'd like to see only the file name without extension, so if Windows says the name is "file.txt.avi.bmp" and extension is "exe", I want to see anything but "exe".
 
you should find the position of last point in the file name string (search from right to left) and return all characters until this position (from left to right). If there is no point, return the whole string.

Ion Filipski
1c.bmp
 
Also, you could use ExtractFileName to get the filename, then use ExtractFileExt to get the file name extension. The do a string search or comparison on the file name for the extension. To get only the file name, take all characters from the beginning of the string to where the two strings match. Sounds harder than it really is.

James P. Cottingham
[sup]
There's no place like 127.0.0.1.
There's no place like 127.0.0.1.
[/sup]
 
The simplest thing I can think of is like this:

while (int Index = Filename.AnsiPos("."))
Filename = Filename.SubString(0, Index-1);

Filename returns without any periods in it.

Chris
 
Hi!

Here is my code:

AnsiString __fastcall WithoutFileExt(AnsiString fn)
{
int dp=fn.Length();
while (dp!=0 && fn[dp]!='.') dp--;

if (dp) return fn.SubString(1,dp-1);

return fn;
}

unbornchikken
 
Here is the easiest way I have found to extract the extension from the filename (assuming a 3 character extension):

newFileName = fileName.SubString(0, fileName.Length()-4);


 
This should do it ;)

Label1->Caption = ChangeFileExt( ExtractFileName(OpenDialog1->FileName) , "");

 
If you has the filename in a char array (char[]) as i often do, i remove it like this:

char Filename[PATH_SIZE];
// Filename is filled
// Now remove extention by searching for LAST occurence of '.'
strrchr(&Filename,'.') = 0; // Replaces last '.' with C-end-of-string

Totte
 
parsing a char array or string is very often necessary to
accomplish many tasks especially when reading from a config file or such. dont be so quick to find the cute little function ready made in the library. You will find that if you spend a little time you can manipulate your own strings.
later you will find the cutesy function but just think of all the neat things you will learn before then. after all
what will you do when there is no ready made functions to read the file or chop the char array the way you require.

I have a task for you.

write a function that takes a windows long file name and returns a dos 8.3 file name. dont forget all the nameing rules in dos.

I'll compare mine to yours and maybe we will make a better mouse trap.

tomcruz.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top