Win32 API Path Routines

Win32 API Path Routines

Win32 APIでパスを扱うには shlwapi.h に定義されている関数群(Path Routines)を使用する。その際、shlwapi.h をインクルードし、shlwapi.lib をリンクする必要がある。




Path Routinesには、以下のような関数がある。

PathAddBackslashパスの末尾にバックスラッシュ(円マーク)を追加する
PathAddExtensionパスの末尾に拡張子を追加する
PathBuildRootドライブ番号からルートパスを作る
PathCombine2つのパスを結合する
PathFileExistsファイルやディレクトリが存在するかを判定する
PathFindExtensionパスの中から拡張子の位置を探す
PathFindFileNameパスの中からファイル名の位置を探す
PathFindNextComponentパスを解析し、最初のバックスラッシュに続く部分を返す
PathGetArgs与えられたパスから、コマンドライン引数を見つける
PathFindSuffixArray与えられたファイル名が接尾辞リストの中の要素を持っているかどうかを判定する
PathIsLFNFileSpecファイル名がロングフォーマットかどうかを判定する
PathGetDriveNumberAからZの範囲のドライブレターのパスを探し、対応するドライブ番号を返す
PathIsDirectoryパスが有効なディレクトリであるかを判定する
PathIsDirectoryEmpty指定されたパスが空のディレクトリかどうかを判定する
PathIsFileSpecパス文字列かどうかを判定する
PathIsPrefixパスの中に指定された有効な接頭辞の形式が含まれているかどうかを判定する
PathIsRelativeパスが相対パスかどうかを判定する
PathIsRootパスがルートディレクトリかどうかを判定する
PathIsSameRoot2つのパスを比較し、それらが共通のルートコンポーネントを持つかどうかを判定する
PathIsUNC文字列が有効なUniversal Naming Conversion(UNC)かどうかを判定する
PathIsNetworkPathパス文字列がネットワークリソースを表すかどうかを判定する
PathIsUNCServer文字列が有効なサーバーパスだけのUniversal Naming Conversion(UNC)であるかを判定する。
PathIsUNCServerShare文字列が有効なUniversal Naming Conversion(UNC) 共有パスであるかを判定する
PathIsURL文字列が有効なURL形式であるかを判定する
PathRemoveBackslash末尾のバックスラッシュをパスから取り除く
PathSkipRootドライブレターまたはUniversal Naming Conversion(UNC) (server/share)のパス要素を取り除く
PathStripPathパスからディレクトリ部を取り除く
PathStripToRootパスのルート情報以外の部分をすべて取り除く
PathMakeSystemFolderフォルダにシステムフォルダになるために適切な属性を与える
PathUnmakeSystemFolderフォルダーからシステムフォルダーになるための属性を取り除く
PathIsSystemFolderパスがシステムフォルダかどうかを判定する
PathUndecorateパスから装飾を取り除く
PathUnExpandEnvStringsフルパス中の特定のフォルダ名を、関連付けられている環境文字列に置き換える
PathAppend1つのパスをもう一方のパスに追加する
PathCanonicalizeパスを正規化する
PathCompactPath
PathCompactPathEx
PathCommonPrefix2つのパスを比較し、共通の接頭辞があるかどうかを判定する。
PathFindOnPath
PathGetCharTypeパス中の文字の種別を判定する
PathIsContentType
PathIsHTMLFileファイルがHTMLファイルかどうかを調べる
PathMakePrettyパスのすべての文字を小文字に変換する
PathMatchSpecマイクロソフトMS-DOSワイルドカードに一致するタイプを使用している文字列を探す
PathParseIconLocation
PathQuoteSpacesパス中にスペースがあれば、パス全体を二重引用符で囲む
PathRelativePathToファイルまたはフォルダーのパスから相対パスを作成する
PathRemoveArgsパスから引数を取り除く
PathRemoveBlanks先頭と末尾の半角スペースをすべての取り除
PathRemoveExtensionパスから拡張子を取り除く
PathRemoveFileSpecパスの末尾にファイル名やバックスラッシュが存在すれば取り除く
PathRenameExtensionパスの拡張子を変更する
PathSearchAndQualify
PathSetDlgItemPath
PathUnquoteSpacesパスの開始と終端から二重引用符を取り除く

末尾へ






PathAddBackslash

パスの末尾にバックスラッシュ(円マーク)を追加する


定義

LPTSTR PathAddBackslash(
  __inout  LPTSTR lpszPath //パスを表すバッファへのポインタ
);

戻り値:成功すればヌル終端文字列へのアドレスを返す。
失敗すれば、NULLを返す。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[MAX_PATH] = "C:\\Windows";

  printf("Before: %s\n", lpPath);
  
  PathAddBackslash(lpPath);

  printf("After: %s\n", lpPath);

  return 0;
}


実行結果

Before: C:\Windows
After: C:\Windows\



参考:http://msdn.microsoft.com/en-us/library/bb773561


先頭へ 末尾へ





PathAddExtension

パスの末尾に拡張子を追加する


定義

BOOL PathAddExtension(
  __inout   LPTSTR pszPath, //ファイル名が追加されるべきヌル終端文字列バッファへのポインタ
  __in_opt  LPCTSTR pszExtension //追加する拡張子(NULLを指定しても良い)
);

戻り値:拡張子を追加出来ればTRUE、それ以外はFALSE



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[MAX_PATH] = "C:\\test";

  PathAddExtension(lpPath, ".txt");

  puts(lpPath);

  return 0;
}


実行結果

C:\test.txt


参考:http://msdn.microsoft.com/en-us/library/bb773563


先頭へ 末尾へ




PathBuildRoot

ドライブ番号からルートパスを作る


定義

LPTSTR PathBuildRoot(
  __out  LPTSTR szRoot, //構築されたルートパスを受け取るための文字列へのポインタ
  __in   int iDrive //0から25までのドライブ番号
);

戻り値:構築されたルートパスへのポインタ
失敗時(ドライブ番号が0-25以外の場合など)は、szRootにヌル文字列が格納される(Visual Studio 2005, Windows Vistaにて)


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[4];

  for (int i = 0; i <= 25; i++) {
    puts(PathBuildRoot(lpPath, i));
  }

  return 0;
}


実行結果

A:\, B:\, C:\, D:\, E:\, F:\, G:\, H:\, I:\, J:\, K:\, L:\, M:\, N:\, O:\, P:\, Q:\, R:\, S:\, T:\, U:\, V:\, W:\, X:\, Y:\, Z:\,



参考:http://msdn.microsoft.com/en-us/library/bb773567


先頭へ 末尾へ






PathCombine

2つのパスを結合する


定義

LPTSTR PathCombine(
  __out     LPTSTR lpszDest, //結合されたパス文字列を格納するためのヌル終端文字列へのポインタ
  __in_opt  LPCTSTR lpszDir, //ディレクトリパスを含む最大長がMAX_PATHのヌル終端文字列へのポインタ。NULLを指定しても良い
  __in      LPCTSTR lpszFile //ファイル名を含む最大長がMAX_PATHのヌル終端文字列へのポインタ。NULLを指定しても良い
);

戻り値:成功した場合は、結合された文字列へのポインタを返す。それ以外はNULLを返す


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpDir[] = "C:\\Windows\\system32";
  char lpFile[] = "notepad.exe";
  char lpDest[MAX_PATH] = {'\0'};

  PathCombine(lpDest, lpDir, lpFile);

  puts(lpDest);

  return 0;
}


実行結果

C:\Windows\system32\notepad.exe



参考:http://msdn.microsoft.com/en-us/library/bb773571


先頭へ 末尾へ





PathFileExists

ファイルやディレクトリが存在するかを判定する


定義

BOOL PathFileExists(
  __in  LPCTSTR pszPath //検証されるべきフルパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:パスが存在すればTRUEを返す。それ以外はFALSEを返す。GetLastErrorを呼び出して拡張エラー情報を取得できる。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows";
  if (PathFileExists(lpPath)) {
    printf("%s exists.", lpPath);
  } else {
    printf("%s do not exist.", lpPath);
  }

  return 0;
}


実行結果

C:\Windows exists.



参考:http://msdn.microsoft.com/en-us/library/bb773584


先頭へ 末尾へ





PathFindExtension

パスの中から拡張子を探す


定義

PTSTR PathFindExtension(
  __in  PTSTR pszPath //拡張子を含むパスを格納した最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:拡張子が見つかれば、pszPathの中の拡張子の前の「.」のアドレスを返す。
それ以外は、ヌル文字列のへのアドレスを返す


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows\\system32\\notepad.exe";
  char *lpExtention;

  lpExtention = PathFindExtension(lpPath);

  puts(lpExtention);

  return 0;
}


実行結果

.exe



参考:http://msdn.microsoft.com/en-us/library/bb773587


先頭へ 末尾へ





PathFindFileName

パスの中からファイル名を探す

PTSTR PathFindFileName(
  __in  PTSTR pPath //パスを格納する最大長MAX_PATHのヌル終端文字列
);

戻り値:成功時はファイル名へのポインタを返す。失敗時はpPathの先頭へのポインタを返す。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows\\system32\\notepad.exe";
  char *lpFileName;
  
  lpFileName = PathFindFileName(lpPath);

  printf("File name: %s\n", lpFileName);

  return 0;
}



実行結果

File name: notepad.exe


参考:http://msdn.microsoft.com/en-us/library/bb773589


先頭へ 末尾へ





PathFindNextComponent

パスを解析し、最初のバックスラッシュに続くパスの部分を返す。


定義

PTSTR PathFindNextComponent(
  __in  PTSTR pszPath //解析されるパスが格納されたヌル終端文字列
);

戻り値:切り捨てられたパスを含むヌル終端文字列を返す。
pszPathがパスの最後の部分を指しているときは、ヌル文字列へのポインタを返す。
pszPathがヌル文字列であれば、NULLを返す。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows\\system32\\notepad.exe";
  char *lpNextComponent;

  if ( (lpNextComponent = PathFindNextComponent(lpPath)) == NULL ) {
    return 1;
  }

  do {
      printf("Next component: %s\n", lpNextComponent);
  } while ( (lpNextComponent = PathFindNextComponent(lpNextComponent)) != NULL );

  return 0;
}


実行結果

Next component: Windows\system32\notepad.exe
Next component: system32\notepad.exe
Next component: notepad.exe
Next component:


参考:http://msdn.microsoft.com/en-us/library/bb773591


先頭へ 末尾へ





PathGetArgs

与えられたパスから、コマンドライン引数を見つける


定義

PTSTR PathGetArgs(
  __in  PTSTR pszPath //検索されるべき最大長MAX_PATHのヌル終端文字列
);

戻り値:成功時には、パスの引数部分を含むヌル終端文字列へのポインタを返す。
パスに引数が含まれなければ、入力文字列の終端へのポインタを返す。
pszPathにNULLが指定された場合は、NULLを返す。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "ping -t -a 192.168.1.1";
  char *lpArgs;
  
  lpArgs = PathGetArgs(lpPath);

  printf("Arguments: %s\n", lpArgs);

  return 0;
}


実行結果

Arguments: -t -a 192.168.1.1


参考:http://msdn.microsoft.com/en-us/library/bb773602


先頭へ 末尾へ





PathFindSuffixArray

与えられたファイル名がサフィックス(接尾辞)リストの中の要素を持っているかどうかを判定する

LPCTSTR PathFindSuffixArray(
  __in  LPCTSTR pszPath, //テストされるファイル名が格納された最大長MAX_PATHのヌル終端文字列へのポインタ
  __in  const LPCTSTR *apszSuffix, //iArraySize個の要素を持つ文字列ポインタの配列。それぞれの文字列はサフィックスを含むヌル終端文字列を指す。これらの文字列は可変長である。
  __in  int iArraySize //配列apszSuffixの要素数
);

戻り値:成功時は、マッチしたサフィックスへの文字列へのポインタを返す。
pszPathが指定されたサフィックスで終了していなければNULLを返す。


備考:この関数は、Case-Sensitiveの比較を行う(アルファベットの大文字・小文字を区別する)。
サフィックスは正確にマッチしなければならない。



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows\\system32\\notepad.exe";
  char *apszSuffix[] = {"zip", "exe", "txt"};
  int nSuffixSize = sizeof(apszSuffix) / sizeof(apszSuffix[0]);
  const char *lpMathedSuffix;

  lpMathedSuffix = PathFindSuffixArray(lpPath, apszSuffix, nSuffixSize);

  if (lpMathedSuffix != NULL) {
    printf("Match: %s\n", lpMathedSuffix);
  } else {
    puts("Suffix does not matched.");
  }

  return 0;
}


実行結果

Match: exe


参考:http://msdn.microsoft.com/en-us/library/bb773598


先頭へ 末尾へ





PathIsLFNFileSpec

ファイル名がロングフォーマットかどうかを判定する。


定義

BOOL PathIsLFNFileSpec(
  __in  LPCTSTR pszName //テストされるファイル名が格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:pszNameが8.3形式で許可される文字数を超えた場合はTRUE、それ以外はFALSEを返す。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpLongPath[] = "C:\\Windows\\system32\\notepad.exe";
  char lpShortPath[] =  "test~1.txt";

  if (PathIsLFNFileSpec(lpLongPath)) {
    printf("%s: Not short path\n", lpLongPath);
  } else {
    printf("%s: Short path (8.3 format)\n", lpLongPath);
  }

  if (PathIsLFNFileSpec(lpShortPath)) {
    printf("%s: Not short path\n", lpShortPath);
  } else {
    printf("%s: Short path (8.3 format)\n", lpShortPath);
  }

  return 0;
}


実行結果

C:\Windows\system32\notepad.exe: Not short path
test~1.txt: Short path (8.3 format)


参考:http://msdn.microsoft.com/en-us/library/bb773635


先頭へ 末尾へ





PathGetDriveNumber

AからZの範囲のドライブレターのパスを探し、対応するドライブ番号を返す。


定義

int PathGetDriveNumber(
  __in  LPCTSTR lpsz //検索されるパスが格納された最大長MAX_PATHのヌル終端文字列
);

戻り値:パスがドライブレターを含んでいれば、AからZに対応した0から25までの整数を返す。
それ以外は、-1 を返す


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *pszPath[] = { "A:\\", "C:\\", "Z:\\" };
  size_t nLength = sizeof(pszPath)/sizeof(pszPath[0]);

  for (size_t i = 0; i < nLength; i++) {
    printf("%s - %d\n", 
      pszPath[i], PathGetDriveNumber(pszPath[i]));
  }

  return 0;
}


実行結果

A:\ - 0
C:\ - 2
Z:\ - 25


参考:http://msdn.microsoft.com/en-us/library/bb773612


先頭へ 末尾へ






PathIsDirectory

パスが有効なディレクトリであるかを判定する


定義

BOOL PathIsDirectory(
  __in  LPCTSTR pszPath //検証すべきパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

パスが有効なディレクトリ(FILE_ATTRIBUTE_DIRECTORYがセットされている)であればTRUE。
それ以外はFALSE。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpDir[] = "C:\\Windows\\system32";
  char lpFile[] = "C:\\Windows\\system32\\notepad.exe";

  if (PathIsDirectory(lpDir)) {
    printf("%s: Directory\n", lpDir);
  } else {
    printf("%s: File\n", lpDir);
  }

  if (PathIsDirectory(lpFile)) {
    printf("%s: Directory\n", lpFile);
  } else {
    printf("%s: File\n", lpFile);
  }

  return 0;
}


実行結果

C:\Windows\system32: Directory
C:\Windows\system32\notepad.exe: File



PathIsDirectoryEmpty

指定されたパスが空のディレクトリかどうかを判定する

BOOL PathIsDirectoryEmpty(
  __in  LPCTSTR pszPath //テストされるパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:pszPathが空のディレクトリであればTRUE、pszPathがディレクトリでないか
最低でも「.」または「..」以外の1つのファイルを含む場合はFALSE。



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpEmptyDir[] = "C:\\EmptyFolder";//実在する空のディレクトリ名
  char lpPath[] = "C:\\Windows\\system32";

  if (PathIsDirectoryEmpty(lpEmptyDir)) {
    printf("%s: Empty\n", lpEmptyDir);
  } else {
    printf("%s: Not empty\n", lpEmptyDir);
  }

  if (PathIsDirectoryEmpty(lpPath)) {
    printf("%s: Empty\n", lpPath);
  } else {
    printf("%s: Not empty\n", lpPath);
  }

  return 0;
}


実行結果

C:\EmptyFolder: Not empty
C:\Windows\system32: Not empty


参考:http://msdn.microsoft.com/en-us/library/bb773623


先頭へ 末尾へ





PathIsFileSpec

パス文字列かどうかを判定する。
パスのパス区切り文字(':' または '\')を見つけ、もしパスの中にパス区切り文字が存在しなければファイル仕様のパスであるとみなされる。

定義

BOOL PathIsFileSpec(
  __in  LPCTSTR lpszPath //テストされるパスが格納される最大長がMAX_PATHのヌル終端文字列へのポインタ
);


戻り値:パス区切り文字がパスに含まれていなければTRUE、パス区切り文字がパスに含まれていればFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath1[] = "C:\\Windows\\system32";
  if (PathIsFileSpec(lpPath1)) {
    printf("There are no path-delimiting character. %s\n", lpPath1);
  } else {
    printf("There are path-delimiting character. : %s\n", lpPath1);
  }

  char lpPath2[] = "123456789";
  if (PathIsFileSpec(lpPath2)) {
    printf("There are no path-delimiting character. %s\n", lpPath2);
  } else {
    printf("There are path-delimiting character. : %s\n", lpPath2);
  }

  return 0;
}


実行結果

There are path-delimiting character. : C:\Windows\system32
There are no path-delimiting character. 123456789


参考:http://msdn.microsoft.com/en-us/library/bb773627


先頭へ 末尾へ





PathIsPrefix

パスの中にpszPrefixによって渡される有効な接頭辞の形式が含まれているかどうかを判定する。
接頭辞は、"C:\\", ".", "..", "..\\". の中の1つである。

BOOL PathIsPrefix(
  __in  IN LPCTSTR pszPrefix, //検索する接頭辞が格納される最大長MAX_PATHのヌル終端文字列へのポインタ
  __in  IN LPCTSTR pszPath //検索されるパスを格納する最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:比較されたパス(pszPrefix)が、パス(pszPath)の完全な接頭辞であればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpCDrive = "C:\\";
  char *lpDirectory1 = "C:\\Windows\\system32";

  if (PathIsPrefix(lpCDrive, lpDirectory1)) {
    printf("%s is prefix of %s\n", lpCDrive, lpDirectory1);
  } else {
    printf("%s is not prefix of %s\n", lpCDrive, lpDirectory1);
  }

  char *lpDirectory2 = "Z:\\test";
  if (PathIsPrefix(lpCDrive, lpDirectory2)) {
    printf("%s is prefix of %s\n", lpCDrive, lpDirectory2);
  } else {
    printf("%s is not prefix of %s\n", lpCDrive, lpDirectory2);
  }

  return 0;
}


実行結果

C:\ is prefix of C:\Windows\system32
C:\ is not prefix of Z:\test



参考:http://msdn.microsoft.com/en-us/library/bb773650


先頭へ 末尾へ





PathIsRelative

パスが相対パスかどうかを判定する


定義

BOOL PathIsRelative(
  __in  LPCTSTR lpszPath //テストされるパスを格納する最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:相対パスであればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpRelative = "..\\..\\";
  char *lpAbsolute = "C:\\";
  if (PathIsRelative(lpRelative)) {
    printf("%s is relative path.\n", lpRelative);
  } else {
    printf("%s is absolute path.\n", lpRelative);
  }

  if (PathIsRelative(lpAbsolute)) {
    printf("%s is relative path.\n", lpAbsolute);
  } else {
    printf("%s is absolute path.\n", lpAbsolute);
  }

  return 0;
}


実行結果

..\..\ is relative path.
C:\ is absolute path.


参考:http://msdn.microsoft.com/en-us/library/bb773660


先頭へ 末尾へ





PathIsRoot

パスがルートディレクトリかどうかを判定する

BOOL PathIsRoot(
  __in  LPCTSTR pPath //検証されるパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:指定されたパスがルートディレクトリであればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpRoot = "C:\\";
  char *lpSystem32 = "C:\\Windows\\system32";

  if (PathIsRoot(lpRoot)) {
    printf("%s is root.\n", lpRoot);
  } else {
    printf("%s is not root.\n", lpRoot);
  }

  if (PathIsRoot(lpSystem32)) {
    printf("%s is root.\n", lpSystem32);
  } else {
    printf("%s is not root.\n", lpSystem32);
  }

  return 0;
}


実行結果

C:\ is root.
C:\Windows\system32 is not root.


参考:http://msdn.microsoft.com/en-us/library/bb773674


先頭へ 末尾へ





PathIsSameRoot

2つのパスを比較し、それらが共通のルートコンポーネントを持つかどうかを判定する

定義

BOOL PathIsSameRoot(
  __in  LPCTSTR pszPath1, //比較される1番目のパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
  __in  LPCTSTR pszPath2 //比較される2番目のパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:両方の文字列が同じルートコンポーネントを持てばTRUE, それ以外はFALSE。
もしpszPath1がサーバーと共有パスのみを持てばFALSEを返す。

使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpPath1 = "C:\\Windows";
  char *lpPath2 = "C:\\Program Files";

  if (PathIsSameRoot(lpPath1, lpPath2)) {
    printf("\"%s\" and \"%s\" are same root.\n", lpPath1, lpPath2);
  } else {
      printf("\"%s\" and \"%s\" are not same root.\n", lpPath1, lpPath2);
  }

  return 0;
}

実行結果

"C:\Windows" and "C:\Program Files" are same root.


参考:http://msdn.microsoft.com/en-us/library/bb773687


先頭へ 末尾へ





PathIsUNC

文字列が有効なUniversal Naming Conversion(UNC)かどうかを判定する。


定義

BOOL PathIsUNC(
  __in  LPCTSTR pszPath //検証されるパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:有効なUNCパスであればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpPath = "\\\\192.168.1.2\\Share";

  if (PathIsUNC(lpPath)) {
    printf("\"%s\" is UNC.\n", lpPath);
  } else {
    printf("\"%s\" is not UNC.\n", lpPath);
  }

  return 0;
}


実行結果

"\\192.168.1.2\Share" is UNC.


参考:http://msdn.microsoft.com/en-us/library/bb773712


先頭へ 末尾へ





PathIsNetworkPath

パス文字列がネットワークリソースを表すかどうかを判定する。


定義

BOOL PathIsNetworkPath(
  __in  LPCTSTR pszPath //パスが格納されている最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:文字列がネットワークリソースを表していればTRUE、それ以外はFALSE

備考:PathIsNetworkPath関数は次の2つの形式のパスをネットワークパスと解釈する
1.2つのバックスラッシュ文字で始まるパスはUniversal Naming Conversion(UNC)と解釈される。
2.コロン(:)に続く文字列で始まるパスは、マウントされたネットワークドライブとして解釈される。
しかし、PathIsNetoworkPath関数は、Microsoft MS-DOSのSUBSTコマンドまたは、DefineDosDevice関数によりドライブレターにマップされたネットワークドライブを認識できない。

ノート:この関数は、指定されたネットワークリソースの存在するか、現在アクセス可能か、ユーザーが十分なアクセスパーミッションを持っているかを検証しない。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpPath = "\\\\192.168.1.2\\Share";

  if (PathIsNetworkPath(lpPath)) {
    printf("\"%s\" is network path.\n", lpPath);
  } else {
    printf("\"%s\" is not network path.\n", lpPath);
  }

  return 0;
}


実行結果

"\\192.168.1.2\Share" is network path.


参考:http://msdn.microsoft.com/en-us/library/bb773640


先頭へ 末尾へ






PathIsUNCServer

文字列が有効なサーバーパスだけのUniversal Naming Conversion(UNC)であるかを判定する。


定義

BOOL PathIsUNCServer(
  __in  LPCTSTR pszPath //検証されるパスが格納されている最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:文字列がサーバーのみのUNCパス(共有名ではない)であればTRUE、
それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpPath1 = "\\\\192.168.1.2";
  char *lpPath2 = "\\\\192.168.1.2\\Share";

  if (PathIsUNCServer(lpPath1)) {
    printf("\"%s\" is UNC server.\n", lpPath1);
  } else {
    printf("\"%s\" is not UNC server.\n", lpPath1);
  }

  if (PathIsUNCServer(lpPath2)) {
    printf("\"%s\" is UNC server.\n", lpPath2);
  } else {
    printf("\"%s\" is not UNC server.\n", lpPath2);
  }

  return 0;
}


実行結果

"\\192.168.1.2" is UNC server.
"\\192.168.1.2\Share" is not UNC server.


参考:http://msdn.microsoft.com/en-us/library/bb773722


先頭へ 末尾へ





PathIsUNCServerShare

文字列が有効なUniversal Naming Conversion(UNC) 共有パスであるかを判定する(例:\\server\share)


定義

BOOL PathIsUNCServerShare(
  __in  LPCTSTR pszPath //検証されるパスを格納した最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:文字列が「\\server\share」の形式であればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpPath1 = "\\\\192.168.1.2";
  char *lpPath2 = "\\\\192.168.1.2\\Share";

  if (PathIsUNCServerShare(lpPath1)) {
    printf("\"%s\" is UNC server share.\n", lpPath1);
  } else {
    printf("\"%s\" is not UNC server share.\n", lpPath1);
  }

  if (PathIsUNCServerShare(lpPath2)) {
    printf("\"%s\" is UNC server share.\n", lpPath2);
  } else {
    printf("\"%s\" is not UNC server share.\n", lpPath2);
  }

  return 0;
}


実行結果

"\\192.168.1.2" is not UNC server share.
"\\192.168.1.2\Share" is UNC server share.


参考:http://msdn.microsoft.com/en-us/library/bb773723


先頭へ 末尾へ





PathIsURL

文字列が有効なURL形式であるかを判定する。


定義

BOOL PathIsURL(
  __in  LPCTSTR pszPath //検証されるパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:pszPathが有効なURL形式であればTRUE、それ以外はFALSE

備考:この関数は、パスが存在しているサイトを指しているかを検証しない。
単に有効なURL形式であることを判定する。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

void PrintPathIsURL(const char *lpUrl)
{
  if (PathIsURL(lpUrl)) {
    printf("\"%s\" is URL.\n", lpUrl);
  } else {
    printf("\"%s\" is not URL.\n", lpUrl);
  }
}

int main()
{
  char *lpUrl1 = "/";
  char *lpUrl2 = "http://www.hatena.ne.jp/";
  char *lpUrl3 = "C:\\Windows";
  char *lpUrl4 = "aaa";

  PrintPathIsURL(lpUrl1);
  PrintPathIsURL(lpUrl2);
  PrintPathIsURL(lpUrl3);
  PrintPathIsURL(lpUrl4);

  return 0;
}


実行結果

"/" is not URL.
"http://www.hatena.ne.jp/" is URL.
"C:\Windows" is not URL.
"aaa" is not URL.


参考:http://msdn.microsoft.com/en-us/library/bb773724


先頭へ 末尾へ





PathRemoveBackslash

末尾のバックスラッシュをパスから取り除く


定義

LPTSTR PathRemoveBackslash(
  __inout  LPTSTR lpszPath //バックスラッシュを取り除くパスが格納されている最大長MAX_PATHのヌル終端文字列へのポインタ
);

戻り値:バックスラッシュに置換されたヌル文字へのポインタを返す。
または末尾がバックスラッシュでなければ末尾の文字のアドレスを返す。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows\\";
  char *p = PathRemoveBackslash(lpPath);
  puts(lpPath);
  return 0;
}


実行結果

C:\Windows


参考:http://msdn.microsoft.com/en-us/library/bb773743


先頭へ 末尾へ





PathSkipRoot

ドライブレターまたはUniversal Naming Conversion(UNC) (server/share)のパス要素を取り除く


定義

PTSTR PathSkipRoot(
  __in  PTSTR pszPath //解析されるパスが格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);

ルート(ドライブレターまたはUNC server/share)に続くサブパスの開始位置のアドレスを返す


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *lpPath = "C:\\Windows\\System32";
  char *lpSkipped;

  lpSkipped = PathSkipRoot(lpPath);

  printf("lpPath: %s\n", lpPath);
  printf("lpSkipped: %s\n", lpSkipped);

  return 0;
}


実行結果

lpPath: C:\Windows\System32
lpSkipped: Windows\System32


参考:http://msdn.microsoft.com/en-us/library/bb773754


先頭へ 末尾へ





PathStripPath

パスからディレクトリ部を取り除く


定義

void PathStripPath(
  __inout  LPTSTR pszPath //パスとファイル名が格納された最大長MAX_PATHのヌル終端文字列へのポインタ
);


この関数は成功すれば、pszPathはパス部が取り除かれファイル名のみの文字列となる。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows\\System32\\notepad.exe";
  
  printf("Before: %s\n", lpPath);

  PathStripPath(lpPath);
  
  printf("After: %s\n", lpPath);

  return 0;
}


実行結果

Before: C:\Windows\System32\notepad.exe
After: notepad.exe


参考:http://msdn.microsoft.com/en-us/library/bb773756


先頭へ 末尾へ





PathStripToRoot

パスのルート情報以外の部分をすべて取り除く


定義

BOOL PathStripToRoot(
  __inout  LPTSTR szRoot //変換されるべきパスを含んだ最大長MAX_PATHのヌル終端文字列へのポインタ
);

関数が成功すればszRootはルート情報のみを含む文字列に変換される



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char pszPath[] = "C:\\Windows\\System32";

  printf("Before: %s\n", pszPath);

  PathStripToRoot(pszPath);

  printf("After: %s\n", pszPath);

  return 0;
}


実行結果

Before: C:\Windows\System32
After: C:\


参考:http://msdn.microsoft.com/en-us/library/bb773757


先頭へ 末尾へ





PathUndecorate

パスから装飾を取り除く


定義

void PathUndecorate(
  __inout  LPTSTR pszPath //パスが格納されている最大長MAX_PATHのヌル終端文字列へのポインタ
);

この関数が成功して戻ったときに、pszPathは装飾がなくなった文字列を指している。

備考:装飾とは、ブラケットで囲まれた1つかそれ以上の数字から構成され、
ベースネームの直後とファイル名の拡張子の間に挿入される。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath1[] = "C:\\Path\\File[12]";//Decorated Path
  PathUndecorate(lpPath1);
  puts(lpPath1);

  char lpPath2[] = "C:\\Path\\File[5].txt";//Decorated Path
  PathUndecorate(lpPath2);
  puts(lpPath2);

  char lpPath3[] = "C:\\Path\\File.txt";//Undecorated Path
  PathUndecorate(lpPath3);
  puts(lpPath3);

  return 0;
}


実行結果

C:\Path\File
C:\Path\File.txt
C:\Path\File.txt


参考:http://msdn.microsoft.com/en-us/library/bb773759


先頭へ 末尾へ





PathUnExpandEnvStrings

フルパス中の特定のフォルダ名を、関連付けられている環境文字列に置き換える。


定義

BOOL PathUnExpandEnvStrings(
  __in   LPCTSTR pszPath,
  __out  LPTSTR pszBuf,
  __in   UINT cchBuf
);



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char *pszPath;
  char lpBuff[MAX_PATH] = {'\0'};

  pszPath = "C:\\Windows\\system32";
  PathUnExpandEnvStrings(pszPath, lpBuff, MAX_PATH);
  puts(lpBuff);

  pszPath = "C:\\Program Files\\Microsoft.NET";
  PathUnExpandEnvStrings(pszPath, lpBuff, MAX_PATH);
  puts(lpBuff);

  pszPath = "C:\\";
  PathUnExpandEnvStrings(pszPath, lpBuff, MAX_PATH);
  puts(lpBuff);

  return 0;
}


実行結果

%SystemRoot%\system32
%ProgramFiles%\Microsoft.NET
%SystemDrive%\


参考:http://msdn.microsoft.com/en-us/library/bb773760


先頭へ 末尾へ





PathAppend

1つのパスをもう一方のパスに追加する


定義

BOOL PathAppend(
  __inout  LPTSTR pszPath, //pszModeに格納されたパスが追加されるヌル終端文字列へのポインタ
  __in     LPCTSTR pszMore //pszPathに追加されるパス
);


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[MAX_PATH] = "C:\\Windows";

  if (PathAppend(lpPath, "system32")) {
    puts(lpPath);
  } else {
    puts("PathAppend failed");
  }

  return 0;
}


実行結果

C:\Windows\system32


参考:http://msdn.microsoft.com/en-us/library/bb773565


先頭へ 末尾へ





PathCanonicalize

パスを正規化する。
パスに挿入された特種な文字列に対応するファイルパスの要素を削除する(相対パス形式が含まれている部分を絶対パスに変換する)


定義

BOOL PathCanonicalize(
  __out  LPTSTR lpszDst, //正規化されたパスを受け取るためのヌル終端文字列へのポインタ
  __in   LPCTSTR lpszSrc //正規化されるべきパスを格納したヌル終端文字列へのポインタ
);

戻り値:結果が計算され、lpszDstの内容が有効であればTRUE、それ以外はFALSE



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[MAX_PATH];

  PathCanonicalize(lpPath, "..\\..\\system32");
  puts(lpPath);

  PathCanonicalize(lpPath, "C:\\Windows\\system32\\..\\drivers");
  puts(lpPath);

  return 0;
}



実行結果

system32
C:\Windows\drivers


参考:http://msdn.microsoft.com/en-us/library/bb773569


先頭へ 末尾へ






PathCommonPrefix

2つのパスを比較し、共通の接頭辞があるかどうかを判定する。
接頭辞の形式は、"C:\\", ".", "..", "..\\".


定義

int PathCommonPrefix(
  __in       LPCTSTR pszFile1, //第一のパスが格納されたヌル終端文字列へのポインタ
  __in       LPCTSTR pszFile2, //第二のパスが格納されたヌル終端文字列へのポインタ
  __out_opt  LPTSTR pszPath //共通の接頭辞を受け取るためのバッファへのポインタ
);

戻り値:パス中の共通の接頭辞の数を返す。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath1[] = "C:\\Windows\\system32\\notepad.exe";
  char lpPath2[] = "C:\\Windows\\system32";
  char lpCommonPrefix[MAX_PATH];
  int nCommonPrefixCharcters;

  nCommonPrefixCharcters = 
      PathCommonPrefix(lpPath1, lpPath2, lpCommonPrefix);

  printf("CommonPrefixCharcters = %d\n", nCommonPrefixCharcters);
  printf("CommonPrefix = %s\n", lpCommonPrefix);

  return 0;
}



実行結果

CommonPrefixCharcters = 19
CommonPrefix = C:\Windows\system32


参考:http://msdn.microsoft.com/en-us/library/bb773574


先頭へ 末尾へ






PathGetCharType

パス中の文字の種別を判定する


定義

UINT PathGetCharType(
  __in  TUCHAR ch //タイプの調査対象の文字
);

戻り値:文字のタイプ


戻り値は以下のような値が定義されている

GCT_INVALID文字はパスとして有効でない
GCT_LFNCHAR文字はロングファイル名で有効
GCT_SEPARATOR文字はセパレータとして有効
GCT_SHORTCHAR文字は8.3形式のファイル名として有効
GCT_WILD文字はワイルドカード文字



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

void PrintCharType(char ch)
{
  switch (PathGetCharType(ch)) {
    case GCT_INVALID:
      printf("%c: The character is not valid in a path.\n", ch);
      break;
    case GCT_LFNCHAR:
      printf("%c: The character is valid in a long file name.\n", ch);
      break;

    case GCT_SEPARATOR:
      printf("%c: The character is a path separator.\n", ch);
      break;

    case GCT_SHORTCHAR:
      printf("%c: The character is valid in a short (8.3) file name.\n", ch);
      break;

    case GCT_WILD:
      printf("%c: The character is a wildcard character.\n", ch);
      break;

    default:
      printf("%c: Other.\n", ch);
      break;
  }
}

int main()
{
  PrintCharType('\\');
  PrintCharType('*');
  PrintCharType('?');
  PrintCharType('<');
  PrintCharType(',');
  PrintCharType('a');

  return 0;
}


実行結果

\: The character is a path separator.
*: The character is a wildcard character.
?: The character is a wildcard character.
<: The character is not valid in a path.
,: The character is valid in a long file name.
a: Other.


参考:http://msdn.microsoft.com/en-us/library/bb773608


先頭へ 末尾へ






PathIsHTMLFile

ファイルがHTMLファイルかどうかを調べる。ファイルの拡張子で登録されているコンテントタイプに基づき決定される。


定義

BOOL PathIsHTMLFile(
  __in  LPCTSTR pszFile //テストされるファイル名
);

戻り値:ファイルがHTMLファイルであれば、非ゼロ(TRUE)。それ以外はゼロ。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

void PrintPathIsHTMLFile(const char *lpszPath)
{
  if (PathIsHTMLFile(lpszPath)) {
    printf("\"%s\" is html file.\n", lpszPath);
  } else {
    printf("\"%s\" is not html file.\n", lpszPath);
  }
}

int main()
{
  char *lpPath;
  
  PrintPathIsHTMLFile("a.html");
  PrintPathIsHTMLFile("a.htm");
  PrintPathIsHTMLFile("a.aspx");
  PrintPathIsHTMLFile("a.xml");
  PrintPathIsHTMLFile("a.php");
  PrintPathIsHTMLFile("a.pl");

  return 0;
}


実行結果

"a.html" is html file.
"a.htm" is html file.
"a.aspx" is not html file.
"a.xml" is not html file.
"a.php" is not html file.
"a.pl" is not html file.


参考:http://msdn.microsoft.com/en-us/library/bb773631


先頭へ 末尾へ





PathMakePretty

一貫性のある外観を与えるために、パスのすべての文字を小文字に変換する。


BOOL PathMakePretty(
  __inout  LPTSTR lpPath //変換されるべきパスが格納されたヌル終端文字列へのポインタ
);

戻り値:パスが小文字に変換されればTRUE、、それ以外はFALSE


使用例

int main()
{
  char lpPath[] = "C:\\WINDOWS\\SYSTEM32\\NOTEPAD.EXE";
  PathMakePretty(lpPath);
  puts(lpPath);
  return 0;
}


実行結果

C:\windows\system32\notepad.exe


参考:http://msdn.microsoft.com/en-us/library/bb773725


先頭へ 末尾へ






PathMatchSpec

マイクロソフトMS-DOSワイルドカードに一致するタイプを使用している文字列を探す


定義

BOOL PathMatchSpec(
  __in  LPCSTR pszFile, //検索される文字列へのポインタ
  __in  LPCSTR pszSpec //検索文字列
);

戻り値: 文字列がマッチすればTRUE、それ以外はFALSE。


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

void PrintPathMatchSpec(const char *lpszPath, 
                        const char *lpszSpec)
{
  if (PathMatchSpec(lpszPath, lpszSpec)) {
    printf("Match: Path:%s, Spec:%s\n", 
      lpszPath, lpszSpec);
  } else {
    printf("Unmatch: Path:%s, Spec:%s\n", 
      lpszPath, lpszSpec);
  }
}

int main()
{
  PrintPathMatchSpec("C:\\Windows\\system32\\notepad.exe", "*system32*");
  PrintPathMatchSpec("C:\\Windows\\system32\\notepad.exe", "*.exe");
  PrintPathMatchSpec("C:\\Windows\\system32\\notepad.exe", "*.ex?");
  PrintPathMatchSpec("C:\\Windows\\system32\\notepad.exe", "?:\*.exe");
  PrintPathMatchSpec("C:\\Windows\\system32\\notepad.exe", "*.txt");

  return 0;
}


実行結果

Match: Path:C:\Windows\system32\notepad.exe, Spec:*system32*
Match: Path:C:\Windows\system32\notepad.exe, Spec:*.exe
Match: Path:C:\Windows\system32\notepad.exe, Spec:*.ex?
Match: Path:C:\Windows\system32\notepad.exe, Spec:?:*.exe
Unmatch: Path:C:\Windows\system32\notepad.exe, Spec:*.txt


参考:http://msdn.microsoft.com/en-us/library/bb773727


先頭へ 末尾へ






PathQuoteSpaces

パス中にスペースがあれば、パス全体を二重引用符で囲む



定義

BOOL PathQuoteSpaces(
  __inout  LPTSTR lpsz //パスが格納された文字列へのポインタ
);

戻り値:パスにスペースが含まれていればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath1[MAX_PATH] = "C:\\Program Files\\Windows Defender";
  char lpPath2[MAX_PATH] = "C:\\Windows\\system32";
  
  PathQuoteSpaces(lpPath1);
  puts(lpPath1);

  PathQuoteSpaces(lpPath2);
  puts(lpPath2);

  return 0;
}


実行結果

"C:\Program Files\Windows Defender"
C:\Windows\system32


参考:http://msdn.microsoft.com/en-us/library/bb773739


先頭へ 末尾へ





PathRelativePathTo

ファイルまたはフォルダーのパスから相対パスを作成する


定義

BOOL PathRelativePathTo(
  __out  LPTSTR pszPath, //相対パスを受け取る文字列へのポインタ
  __in   LPCTSTR pszFrom, //相対パスの基点
  __in   DWORD dwAttrFrom, //pszFromのファイル属性
  __in   LPCTSTR pszTo, //相対パスの終端
  __in   DWORD dwAttrTo //pszToのファイル属性
);

戻り値:成功すればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[MAX_PATH];
  char *lpFrom = "C:\\Windows\\system32\\drivers";
  DWORD dwAttrFrom = FILE_ATTRIBUTE_DIRECTORY;
  DWORD dwAttrTo = FILE_ATTRIBUTE_DIRECTORY;
  char *lpTo;

  printf("lpFrom: %s\n\n", lpFrom);

  lpTo = "C:\\";
  PathRelativePathTo(lpPath, lpFrom, dwAttrFrom, lpTo, dwAttrTo);
  printf("lpTo=%s; [%s]\n", lpTo, lpPath);

  lpTo = "C:\\Windows";
  PathRelativePathTo(lpPath, lpFrom, dwAttrFrom, lpTo, dwAttrTo);
  printf("lpTo=%s; [%s]\n", lpTo, lpPath);

  lpTo = "C:\\Windows\\System32";
  PathRelativePathTo(lpPath, lpFrom, dwAttrFrom, lpTo, dwAttrTo);
  printf("lpTo=%s; [%s]\n", lpTo, lpPath);

  lpTo = "C:\\WINDOWS\\System32\\IME\\shared\\res";
  PathRelativePathTo(lpPath, lpFrom, dwAttrFrom, lpTo, dwAttrTo);
  printf("lpTo=%s; [%s]\n", lpTo, lpPath);

  lpTo = "C:\\Program Files\\Microsoft.NET";
  PathRelativePathTo(lpPath, lpFrom, dwAttrFrom, lpTo, dwAttrTo);
  printf("lpTo=%s; [%s]\n", lpTo, lpPath);

  return 0;
}


実行結果

lpFrom: C:\Windows\system32\drivers

lpTo=C:\; [..\..\..]
lpTo=C:\Windows; [..\..]
lpTo=C:\Windows\System32; [..]
lpTo=C:\WINDOWS\System32\IME\shared\res; [..\IME\shared\res]
lpTo=C:\Program Files\Microsoft.NET; [..\..\..\Program Files\Microsoft.NET]



参考:http://msdn.microsoft.com/en-us/library/bb773740


先頭へ 末尾へ





PathRemoveArgs

コマンドライン入力から引数を取り除く


定義

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpArgument[] = "rm -fr /";
  PathRemoveArgs(lpArgument);
  puts(lpArgument);

  return 0;
}


実行結果

rm


参考:http://msdn.microsoft.com/en-us/library/bb773742


先頭へ 末尾へ





PathRemoveBlanks

先頭と末尾の半角スペースをすべての取り除く


定義

void PathRemoveBlanks(
  __inout  LPTSTR lpszString //先頭と末尾の半角スペースを取り除く対象の文字列
);


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[MAX_PATH] = "    C:\\Windows\\system32    ";
  
  printf("Before:[%s]\n", lpPath);
  
  PathRemoveBlanks(lpPath);
  
  printf("After:[%s]\n", lpPath);
  
  return 0;
}


実行結果

Before:[    C:\Windows\system32    ]
After:[C:\Windows\system32]


参考:http://msdn.microsoft.com/en-us/library/bb773745


先頭へ 末尾へ





PathRemoveExtension

パスに拡張子が存在すれば、パスから拡張子を取り除く


定義

void PathRemoveExtension(
  __inout  LPTSTR pszPath //拡張子を取り除く対象の文字列
);



使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "C:\\Windows\\system32\\notepad.exe";

  printf("Before:%s\n", lpPath);

  PathRemoveExtension(lpPath);

  printf("After:%s\n", lpPath);

  return 0;
}


実行結果

Before:C:\Windows\system32\notepad.exe
After:C:\Windows\system32\notepad



参考:http://msdn.microsoft.com/en-us/library/bb773746


先頭へ 末尾へ





PathRemoveFileSpec

パスの末尾にファイル名やバックスラッシュが存在すれば取り除く。


定義

BOOL PathRemoveFileSpec(
  __inout  LPTSTR pszPath //パス末尾のファイル名、バックスラッシュを取り除く対象の文字列
);

戻り値:ファイル名やバックスラッシュが取り除かれればTRUE、それ以外はFALSE


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[MAX_PATH] = "C:\\Windows\\system32\\notepad.exe";
  
  printf("Before:%s\n", lpPath);
  
  PathRemoveFileSpec(lpPath);
  
  printf("After:%s\n", lpPath);
  
  return 0;
}


実行例

Before:C:\Windows\system32\notepad.exe
After:C:\Windows\system32


参考:http://msdn.microsoft.com/en-us/library/bb773748


先頭へ 末尾へ




PathRenameExtension


パスの拡張子を変更する



定義

BOOL PathRenameExtension(
  __inout  LPTSTR pszPath, //拡張子を置き換えるためのパス
  __in     LPCTSTR pszExt //新たな拡張子
);

戻り値:TRUEであれば成功、それ以外はFALSE


使用例

#include <windows.h>
#include <stdio.h>

#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char szPath[MAX_PATH] = "C:\\test.txt";

  printf("Before: %s\n", szPath);

  PathRenameExtension(szPath, ".log");

  printf("After: %s\n", szPath);

  return 0;
}


実行結果

Before: C:\test.txt
After: C:\test.log


参考:http://msdn.microsoft.com/en-us/library/bb773749


先頭へ 末尾へ




PathUnquoteSpaces

パスの開始と終端から二重引用符を取り除く


定義

void PathUnquoteSpaces(
  __inout  LPTSTR lpsz //引用符を取り除く対象の文字列
);


使用例

#include <stdio.h>
#include <windows.h>
#include <shlwapi.h>
#pragma comment(lib, "shlwapi.lib")

int main()
{
  char lpPath[] = "\"C:\\Program Files\\Windows Defender\"";
  
  printf("Before:%s\n", lpPath);

  PathUnquoteSpaces(lpPath);
 
  printf("After:%s\n", lpPath);

  return 0;
}


実行結果

Before:"C:\Program Files\Windows Defender"
After:C:\Program Files\Windows Defender


参考:http://msdn.microsoft.com/en-us/library/bb773763


先頭へ




.