天天看點

C++常用的檔案操作路徑函數

shlwapi.dll中的實用API函數釋出

在windows   system目錄下有這個動态連結庫

 BOOL PathFileExists(LPCTSTR lpszPath)

功能:檢查檔案/路徑是否存在

LPTSTR PathFindFileName(LPCTSTR pPath)

功能:獲得路徑中的檔案名

例如: PathFindFileName( “c:\\Program Files\\File.txt” ) = “File.txt”。

如果路徑中不包含檔案名,則傳回值是最下面的目錄名。

LPTSTR PathFindExtension(LPCTSTR pPath)

功能:擷取路徑或檔案名中的檔案擴充名

例如:PathFindExtension( “File.txt” ) = “.txt”

LPTSTR PathFindNextComponent(LPCTSTR pszPath)

例如:PathFindNextComponent( “c:\\Program Files\\Directory” ) = “Program Files\\

   Directory”

   PathFindNextComponent( “c:\\Program Files” ) = “Program Files”

BOOL PathFindOnPath( IN OUT LPTSTR   pszFile , IN LPCTSTR * ppszOtherDirs)

功能:在指定的目錄中尋找檔案。

參數: 

pszFile  要尋找的檔案名,確定 pszFile 有足夠的大小來容納 MAX_PATH 個字元。如果能找到指定的檔案,該參數将傳回檔案的全路徑。

ppszOtherDirs   該參數是首先要尋找的目錄清單,它可以為NULL,如果為NULL,函數将在系統目錄,目前目錄和由PATH設定的目錄中查找。

LPTSTR PathGetArgs(LPCTSTR pszPath)

功能:從路徑中分析參數

例如:LPTSTR pszParam = PathGetArgs( “notepad.exe c:\\temp.txt” )

         pszParam 傳回 “c:\\temp.txt”

int PathGetDriveNumber(LPCTSTR lpsz)

功能:從路徑中分析盤符

傳回值:成功傳回 0 –25 ,代表(A – Z),失敗傳回 –1

BOOL PathIsContentType(LPCTSTR pszPath,LPCTSTR pszContentType)

功能:檢查檔案是否為指定的ContentType

例如:PathIsContentType( “hello.txt” , “text/plain” ) 傳回TRUE

        PathIsContentType( “hello.txt” , “image/gif” ) 傳回FALSE

BOOL PathIsDirectory(LPCTSTR pszPath)

功能:檢查路徑是否為有效目錄。注意路徑不能包含檔案名

BOOL PathIsFileSpec(LPCTSTR lpszPath)

功能:檢查路徑中是否帶有 ‘:’ 和 ‘\’ 分隔符

BOOL PathIsHTMLFile(LPCTSTR pszFile)

功能:檢查是否為HTML檔案

BOOL PathIsPrefix(IN LPCTSTR  pszPrefix,IN LPCTSTR  pszPath)

功能:檢查路徑是否包含指定字首,字首如:”盤符:\\” 、“..\\” 、”.\\”

PathIsPrefix ( “c:\\” , “c:\\hello.txt” ) 傳回       TRUE

BOOL PathIsRelative(LPCTSTR lpszPath)

功能:檢查路徑是否是相對路徑

BOOL PathIsRoot(LPCTSTR pPath)

功能:檢查路徑是否是根目錄

BOOL PathIsSameRoot(LPCTSTR pszPath1,LPCTSTR pszPath2);

功能:比較兩個路徑是否在同一盤符

BOOL PathIsUNC(LPCTSTR pszPath );

功能:判斷路徑是否是UNC格式

BOOL PathIsURL(IN LPCTSTR pszPath );

功能:判斷路徑是否是URL

BOOL PathMatchSpec( LPCTSTR pszFileParam,LPCTSTR pszSpec)

功能:用帶有通配符(*号和?号)的字元串去比較另一個字元串

例如:PathMatchSpec( “http://news.sina.com.cn” , “*sina.com*” ) 傳回TRUE

         PathMatchSpec( “c:\\hello.txt” , “*.txt” ) 傳回TRUE

void PathRemoveArgs(LPTSTR pszPath);

功能:删除路徑中帶有的參數

LPTSTR PathRemoveBackslash(LPTSTR lpszPath);

功能:删除路徑結尾多餘的’\’符号

void PathRemoveBlanks(LPTSTR lpszString);

功能:删除字元串頭尾的空格

void PathRemoveExtension( LPTSTR pszPath);

功能:删除路徑後面的檔案擴充名

BOOL PathRemoveFileSpec( LPTSTR pszPath);

功能:删除路徑後面的檔案名和’\’符号。該函數可以分析出一個檔案的路徑。

例:szPath = “c:\\windows\\system32\\nt.dll” ;

調用PathRemoveFileSpec( szPath ) 後,szPath = “c:\\windows\\system32” 

BOOL PathRenameExtension( IN OUT LPTSTR pszPath,LPCTSTR pszExt);

功能:替換路徑後面的檔案擴充名,如果pszPath後面不帶擴充名,則新的擴充名會被添加上去。

pszPath  pszPath至少需要MAX_PATH個字元的空間

pszExt    新的擴充名,必需包含 . 号

void PathStripPath(LPTSTR pszPath );

功能:從路徑中分析出檔案名

例: PathStripPath( “c:\\test\\hello.txt” ) => hello.txt

BOOL PathStripToRoot( LPTSTR szRoot );

功能:從路徑中分析出盤符

void PathUnquoteSpaces( LPTSTR lpsz );

功能:從帶引号的路徑中取出路徑

例:szPath = “\”c:\\Program Files\”” 

       PathUnquoteSpaces( szPath )  => szPath = “c:\\Program Files”

附另一路徑相關函數,由ImageHlp提供

BOOL SearchTreeForFile(

  IN LPSTR RootPath,       

  IN LPSTR InputPathName,  

  OUT LPSTR OutputPathBuffer  

);

功能:從指定目錄樹中尋找檔案

ExtractFilePath()

  功能:傳回完整檔案名中的路徑

  函數原型如下:

  AnsiString __fastcall ExtractFilePath;

  ExtractFilePath和相近函數:

  ExtractFileDrive :傳回完整檔案名中的驅動器,如"C:" 

  ExtractFilePath:傳回完整檔案名中的路徑,最後帶“/”,如"C:\test\" 

  ExtractFileDir:傳回完整檔案名中的路徑,最後不帶“/” ,如"C:\test" 

  ExtractFileName:傳回完整檔案名中的檔案名稱 (帶擴充名),如"mytest.doc" 

  ExtractFileExt 傳回完整檔案名中的檔案擴充名(帶.),如".doc" 

  extractfiledir //這個沒有最後的 \ 

  extractfilepath //這個最後有 \

ExpandFileName()

#include   "shlwapi.h"   

  #   pragma   comment(lib,"shlwapi.lib") 

繼續閱讀