Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting file name from url path

0 views
Skip to first unread message

jklioe

unread,
Dec 31, 2007, 2:02:09 AM12/31/07
to
Hi All,

I have a url path(CString) how do i get the file name from it in
CString ?? I dont want to use _tsplitpath ..


Zapanaz

unread,
Dec 31, 2007, 2:46:00 AM12/31/07
to

On Sun, 30 Dec 2007 23:02:09 -0800 (PST), jklioe <ranu...@gmail.com>
wrote:

>Hi All,
>
>I have a url path(CString) how do i get the file name from it in
>CString ?? I dont want to use _tsplitpath ..
>

personally I always convert strings to c++ strings ...

string dir = pathName;
dir.erase(dir.find_last_of("\\", dir.length()-1) + 1, dir.length());

But that entails the extra step of converting CString to a c++ string
... if I recall correctly, this doesn't work

CString pathname = "foo\file";
string dir = pathname;

But this does

CString pathname = "foo\file";
string dir;
dir = pathname;

--
Joe Cosby
http://joecosby.com/
People ask me if I've ever been called a Nazi. I answer that no one has
ever had dreams of being tied down and sexually ravished by someone dressed
as a liberal.
- P.J. O'Rourke

:: Currently listening to Someone Else's Dream, 1996, by Laurie Anderson, from "The Ugly One with the Jewels and other stories"

jklioe

unread,
Dec 31, 2007, 3:31:17 AM12/31/07
to
Cant v not do this by using MFC ? Without converting CString to
string ?
Does MFC provide this feature ?


On Dec 31, 12:46 pm, Zapanaz <http://joecosby.com/code/mail.pl> wrote:
> On Sun, 30 Dec 2007 23:02:09 -0800 (PST), jklioe <ranu2...@gmail.com>


> wrote:
>
> >Hi All,
>
> >I have a url path(CString) how do i get the file name from it in
> >CString ?? I dont want to use _tsplitpath ..
>
> personally I always convert strings to c++ strings ...
>
> string dir = pathName;
> dir.erase(dir.find_last_of("\\", dir.length()-1) + 1, dir.length());
>
> But that entails the extra step of converting CString to a c++ string
> ... if I recall correctly, this doesn't work
>
> CString pathname = "foo\file";
> string dir  = pathname;
>
> But this does
>
> CString pathname = "foo\file";
> string dir;
> dir = pathname;
>
> --

> Joe Cosbyhttp://joecosby.com/

Norbert Unterberg

unread,
Dec 31, 2007, 5:15:51 AM12/31/07
to
jklioe schrieb:

> Hi All,
>
> I have a url path(CString) how do i get the file name from it in
> CString ?? I dont want to use _tsplitpath ..

Actually, it is not that easy.

You need to:
1. Decode the URL (it can be escaped like containing %20 and so on)
2. Convert from UTF8 to UTF16 (URIs can contain UTF-8 characters)
3. Strip all unneccessary path components. Web URL愀 can contain parameters like
?session=blahblah at the end

You could then use something like

int pos = url.ReverseFind(_T('/'));
CString filepart;
if (pos != -1)
filepart = url.Mid(pos+1);

Norbert

Message has been deleted

jklioe

unread,
Dec 31, 2007, 5:49:49 AM12/31/07
to
Thanks for reply .. Well I was able to get the File Name as
follow .Correct me if I am wrong


CFileFind finder;
if(!finder.FindFile(FilePath))
return FALSE;

finder.FindNextFile();
CString FileName=finder.GetFileName();
FileName.Truncate(FileName.GetLength()-4);

I still did not understand y FindNextFile is necessary before calling
GetFileName(In fact before calling many of the attribute member
functions of CFileFind ) ?? Can any one clear my doubt


On Dec 31, 3:15 pm, Norbert Unterberg <nunterb...@newsgroups.nospam>
wrote:


> jklioe schrieb:
>
> > Hi All,
>
> > I have a url path(CString) how do i get the file name from it in
> > CString ?? I dont want to use _tsplitpath ..
>
> Actually, it is not that easy.
>
> You need to:
> 1. Decode the URL (it can be escaped like containing %20 and so on)
> 2. Convert from UTF8 to UTF16 (URIs can contain UTF-8 characters)

> 3. Strip all unneccessary path components. Web URL´s can contain parameters like

Serge Wautier

unread,
Dec 31, 2007, 6:08:50 AM12/31/07
to
AfxParseURL()

HTH,

Serge.
http://www.apptranslator.com - Localization tool for your MFC applications


"jklioe" <ranu...@gmail.com> wrote in message
news:adad6626-f318-4f11...@u10g2000prn.googlegroups.com...

jklioe

unread,
Dec 31, 2007, 6:17:38 AM12/31/07
to
Thanks but the url is something like C:\\temp\xx.txt and i need only
"xx"

On Dec 31, 4:08 pm, "Serge Wautier" <se...@wautier.nospam.net> wrote:
> AfxParseURL()
>
> HTH,
>
> Serge.http://www.apptranslator.com- Localization tool for your MFC applications
>
> "jklioe" <ranu2...@gmail.com> wrote in message


>
> news:adad6626-f318-4f11...@u10g2000prn.googlegroups.com...
>
>
>
> > Hi All,
>
> > I have a url path(CString) how do i get the file name from it in

> > CString ?? I dont want to use _tsplitpath ..- Hide quoted text -
>
> - Show quoted text -

David Wilkinson

unread,
Dec 31, 2007, 6:43:46 AM12/31/07
to
jklioe wrote:
> Thanks but the url is something like C:\\temp\xx.txt and i need only
> "xx"

jklioe:

C:\temp\xx.txt is a path, not an url.

--
David Wilkinson
Visual C++ MVP

Joseph M. Newcomer

unread,
Dec 31, 2007, 1:25:22 PM12/31/07
to
Actullay, it isn't that simple, either...don't forget about the ?parmeters part, which has
to be removed.

For example, in writing a recipe about shallots I wanted to include something about
Tennyson's poem "The Lady of Shallot". Note also that a filename may not be present,
because the default is index.htm (and a number of other possibilities based on some rules
about how to search for the default Web page)


http://www.google.com/search?hl=en&q=lady+shallot


So the code below, for

www.flounder.com

woud deliver an empty string, but for

http://www.flounder.com

would deliver a "file name" of www.flounder.com, which is not actually a file name at all.
joe

On Mon, 31 Dec 2007 11:15:51 +0100, Norbert Unterberg <nunte...@newsgroups.nospam>
wrote:

Joseph M. Newcomer [MVP]
email: newc...@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

Joseph M. Newcomer

unread,
Dec 31, 2007, 1:28:15 PM12/31/07
to
This applies to files on the current machine, not files on remote machines. You have to
call FindNextFile because this C++ iterator class was designed to be consistent with other
MFC iterators, in that you create a "position" object and then you find the "next"
position while retrieving the "current" object. The ::FindFirstFile/::FindNextFile APIs
work differently, so they went through some effort to simulate MFC iterator behavior here,
but it is now completely consistent with other MFC iterators (e.g., CList, CArray, etc.)
joe

Joseph M. Newcomer

unread,
Dec 31, 2007, 1:31:07 PM12/31/07
to
Which then goes back to the question of why not use _tsplitpath? That would be the
correct way to handle a file name!

TCHAR file[MAX_PATH];
TCHAR ext[MAX_PATH];
_tsplitpath(input_string, NULL, NULL, file, ext);

TCHAR filename[MAX_PATH];
_tmakepath(filename, NULL, NULL, file, ext);

done! There is no reason to NOT use _tsplitpath in this context.

Note that in VS2005 and beyond, you should really use _tsplitpath_s and _tmakepath_s to
make sure there are never any buffer overruns.
joe

On Mon, 31 Dec 2007 06:43:46 -0500, David Wilkinson <no-r...@effisols.com> wrote:

>jklioe wrote:
>> Thanks but the url is something like C:\\temp\xx.txt and i need only
>> "xx"
>
>jklioe:
>
>C:\temp\xx.txt is a path, not an url.

jklioe

unread,
Dec 31, 2007, 11:56:52 PM12/31/07
to
Thanks was very informative ..
I was not knowing abt _tsplitpath_s

I ll change my code ...

On Dec 31 2007, 11:31 pm, Joseph M. Newcomer <newco...@flounder.com>
wrote:


> Which then goes back to the question of why not use _tsplitpath?  That would be the
> correct way to handle a file name!
>
> TCHAR file[MAX_PATH];
> TCHAR ext[MAX_PATH];
> _tsplitpath(input_string, NULL, NULL, file, ext);
>
> TCHAR filename[MAX_PATH];
> _tmakepath(filename, NULL, NULL, file, ext);
>
> done!  There is no reason to NOT use _tsplitpath in this context.
>
> Note that in VS2005 and beyond, you should really use _tsplitpath_s and _tmakepath_s to
> make sure there are never any buffer overruns.
>                                 joe
>

> On Mon, 31 Dec 2007 06:43:46 -0500, David Wilkinson <no-re...@effisols.com> wrote:
> >jklioe wrote:
> >> Thanks but the url is something like C:\\temp\xx.txt and i need only
> >> "xx"
>
> >jklioe:
>
> >C:\temp\xx.txt is a path, not an url.
>
> Joseph M. Newcomer [MVP]

> email: newco...@flounder.com

David Ching

unread,
Jan 2, 2008, 9:15:45 AM1/2/08
to
"jklioe" <ranu...@gmail.com> wrote in message
news:adad6626-f318-4f11...@u10g2000prn.googlegroups.com...

> Hi All,
>
> I have a url path(CString) how do i get the file name from it in
> CString ?? I dont want to use _tsplitpath ..
>

PathFindFileName()

-- David

Joseph M. Newcomer

unread,
Jan 2, 2008, 12:08:28 PM1/2/08
to
As with many posts, the OP did not say WHY _tsplitpath was a Bad Thing. It appears (given
my answer about _tsplitpath_s and the reply) that the concern was buffer overrun,
PathFindFileName has the same problem (but there was no way you could know that because
the rationale was not stated!)
joe

Joseph M. Newcomer [MVP]
email: newc...@flounder.com

David Ching

unread,
Jan 2, 2008, 1:28:46 PM1/2/08
to
"Joseph M. Newcomer" <newc...@flounder.com> wrote in message
news:j4hnn3d2ltd491u4r...@4ax.com...

> As with many posts, the OP did not say WHY _tsplitpath was a Bad Thing.
> It appears (given
> my answer about _tsplitpath_s and the reply) that the concern was buffer
> overrun,
> PathFindFileName has the same problem (but there was no way you could know
> that because
> the rationale was not stated!)

PathFindFileName() expects a MAX_PATH output buffer, so I imagine it takes
care to truncate the output to fit MAX_PATH.

Thanks,
David


Joseph M. Newcomer

unread,
Jan 2, 2008, 2:57:19 PM1/2/08
to
We do hope so. We might make the same hypothesis about _tsplitpath. So I went and read
the _tsplitpath code, and indeed, it limits the size of the buffers so there is no buffer
overrun. Note, however, that neither of these actually states that they truncate the
buffer to the limit they specify.
joe

0 new messages