Getting file extension from path
-
This is hopefully a basic question but it's been a VERY long time since I used C++ and I just need to know how I can get a file extension from a path and compare to constant extension. I have an input: LPCWSTR pwszPath and a constant: WCHAR MY_EXT[4] = L".me"; I need to know if pwszPath ends with MY_EXT but I can't think how to do it.. Any help would be great!
-
This is hopefully a basic question but it's been a VERY long time since I used C++ and I just need to know how I can get a file extension from a path and compare to constant extension. I have an input: LPCWSTR pwszPath and a constant: WCHAR MY_EXT[4] = L".me"; I need to know if pwszPath ends with MY_EXT but I can't think how to do it.. Any help would be great!
__DanC__ wrote:
I need to know if pwszPath ends with MY_EXT but I can't think how to do it..
Use
PathFindExtension
. Matching extension will somewhat look like this...// Assumption: there is no '.' in extension
bool MatchExtension( LPCTSTR lpctszMyPath, LPCTSTR lpctszChkExt )
{
ASSERT( lpctszMyPath && lpctszChkExt );
LPCTSTR lpctszExt = PathFindExtension( lpctszMyPath );
// Or if you don't wish to link to shlwapi for this one function use
// LPCTSTR lpctszExt = _tcsrchr( lpctszMyPath, _T( '.' ));
return ( lpctszExt && _tcscmpi( lpctszExt + 1, lpctszChkExt ) == 0 );
}Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
modified on Wednesday, July 2, 2008 5:06 AM
-
This is hopefully a basic question but it's been a VERY long time since I used C++ and I just need to know how I can get a file extension from a path and compare to constant extension. I have an input: LPCWSTR pwszPath and a constant: WCHAR MY_EXT[4] = L".me"; I need to know if pwszPath ends with MY_EXT but I can't think how to do it.. Any help would be great!
What about PathFindExtension [^]? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
__DanC__ wrote:
I need to know if pwszPath ends with MY_EXT but I can't think how to do it..
Use
PathFindExtension
. Matching extension will somewhat look like this...// Assumption: there is no '.' in extension
bool MatchExtension( LPCTSTR lpctszMyPath, LPCTSTR lpctszChkExt )
{
ASSERT( lpctszMyPath && lpctszChkExt );
LPCTSTR lpctszExt = PathFindExtension( lpctszMyPath );
// Or if you don't wish to link to shlwapi for this one function use
// LPCTSTR lpctszExt = _tcsrchr( lpctszMyPath, _T( '.' ));
return ( lpctszExt && _tcscmpi( lpctszExt + 1, lpctszChkExt ) == 0 );
}Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
modified on Wednesday, July 2, 2008 5:06 AM
-
__DanC__ wrote:
That was exactly what I was looking for! Thanks!
I've modified my reply a bit. :)
Nibu babu thomas Microsoft MVP for VC++ Code must be written to be read, not by the compiler, but by another human being. Programming Blog: http://nibuthomas.wordpress.com
-
What about PathFindExtension [^]? :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]too late buddy :)
nave [OpenedFileFinder]
-
too late buddy :)
nave [OpenedFileFinder]
Indeed: bits flow very slowly from Italy this morning :zzz:. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
Indeed: bits flow very slowly from Italy this morning :zzz:. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]:laugh: :laugh:
nave [OpenedFileFinder]
-
Indeed: bits flow very slowly from Italy this morning :zzz:. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Quote: bits flow very slowly from Italy this morning Bits? or Thoughts? :P
Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.
-
Quote: bits flow very slowly from Italy this morning Bits? or Thoughts? :P
Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.
I don't know (maybe bits of thougths? :-D). BTW apparently the same happens from Germany...;P (this thread is a bit dated)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I don't know (maybe bits of thougths? :-D). BTW apparently the same happens from Germany...;P (this thread is a bit dated)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]Yeah, I am having quite things to do and less time to do them, I am a bit away from keyboard
Regards. -------- M.D.V. ;) If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about? Help me to understand what I'm saying, and I'll explain it better to you “The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson Rating helpfull answers is nice, but saying thanks can be even nicer.