わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

パスの末尾の'\'以降を削除する

PathRemoveFileSpec関数は、パスから末尾のファイル名とバックスラッシュを削除します。 末尾のファイル名とバックスラッシュが削除される事によって親フォルダを取得する事ができます。

PathRemoveFileSpecは、ユニコード版(PathRemoveFileSpecW)を使うようにし てください。マルチバイト版は0x5cを含むパスを渡した際に正しい結果を返さない不具合があります。

パスの末尾の'\'以降を削除する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#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






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。