PathRemoveFileSpec関数は、パスから末尾のファイル名とバックスラッシュを削除します。 末尾のファイル名とバックスラッシュが削除される事によって親フォルダを取得する事ができます。
PathRemoveFileSpecは、ユニコード版(PathRemoveFileSpecW)を使うようにし てください。マルチバイト版は0x5cを含むパスを渡した際に正しい結果を返さない不具合があります。
#include <tchar.h>
#include <iostream>
#include <windows.h>
#include <shlwapi.h>
#pragma comment (lib, "Shlwapi.lib")
/*
パス末尾の'¥¥'以降を削除する
*/
int _tmain
(
int argc
, _TCHAR* argv[]
)
{
// 標準出力にユニコード出力する
setlocale( LC_ALL, "Japanese" );
TCHAR waBuf[ MAX_PATH ];
// バッファへパスを設定
::wcscpy_s( waBuf, _countof( waBuf ), L"C:¥¥Program Files (x86)¥¥Windows NT¥¥test.txt" );
std::wcout << L"■削除前" << std::endl;
std::wcout << waBuf << std::endl;
std::wcout << std::endl;
// 親フォルダを取得する
::PathRemoveFileSpecW( waBuf );
// 結果を出力
std::wcout << L"■削除後" << std::endl;
std::wcout << waBuf << std::endl;
// 正常終了
return( 0 );
}
■削除前
C:\Program Files (x86)\Windows NT\test.txt
■削除後
C:\Program Files (x86)\Windows NT