1. 程式人生 > >vc 遍歷目錄下的檔案與總數

vc 遍歷目錄下的檔案與總數

使用::FindFirstFile和::FindNextFile方法
#include "StdAfx.h"
#include <windows.h>
#include <stdio.h>
#include <string.h>
#define LEN 1024
int FileCount = 0;
// 深度優先遞迴遍歷目錄中所有的檔案
BOOL  DirectoryList(LPCSTR Path)
{
    WIN32_FIND_DATA FindData;
    HANDLE hError;

    char FilePathName[LEN];
    // 構造路徑
char FullPathName[LEN]; strcpy(FilePathName, Path); strcat(FilePathName, "\\*.*"); hError = FindFirstFile(FilePathName, &FindData); if (hError == INVALID_HANDLE_VALUE) { printf("搜尋失敗!"); return 0; } while(::FindNextFile(hError, &FindData)) { // 過慮.和..
if (strcmp(FindData.cFileName, ".") == 0 || strcmp(FindData.cFileName, "..") == 0 ) { continue; } // 構造完整路徑 wsprintf(FullPathName, "%s\\%s", Path,FindData.cFileName); FileCount++; // 輸出本級的檔案 printf("%s\n", FullPathName); if
(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { DirectoryList(FullPathName); } } return 0; } void main() { DirectoryList("F:\\123"); printf("共%d個檔案\n",FileCount); } 二、利用CFileFind類較簡潔的實現該功能 void CModelDlg::FindBmpFile(CString strFoldername) { CString m_cstrFileList=""; CFileFind tempFind; BOOL bFound; //判斷是否成功找到檔案 bFound=tempFind.FindFile(strFoldername + "\\*.*"); /修改" "內內容給限定查詢檔案型別 CString strTmp; //如果找到的是資料夾 存放資料夾路徑 while(bFound) //遍歷所有檔案 { bFound=tempFind.FindNextFile(); //第一次執行FindNextFile是選擇到第一個檔案,以後執行為選擇 //到下一個檔案 if(!tempFind.IsDots()) continue; //如果找到的是返回上層的目錄 則結束本次查詢 if(tempFind.IsDirectory()) //找到的是資料夾,則遍歷該資料夾下的檔案 { strTmp=""; strTmp=tempFind.GetFilePath(); FindFile(strTmp); } else { strTmp=tempFind.GetFileName(); //儲存檔名,包括字尾名 // 在此處新增對找到檔案的處理 } } tempFind.Close(); return; } 三、使用IO.H中的_findfirst和_findnext方法 在IO.H、WCHAR.H中提供了_finddata_t, _wfinddata_t, _wfinddatai64_t 結構,通過_findfirst可以得到滿足條件的第一個檔案的控制代碼,如下: long _findfirst( char *filespec, struct _finddata_t *fileinfo ),然後你可以使用_findnext函式得到用_findfirst的控制代碼後的檔案指標,如此就可以遍歷所有滿足條件的檔案。其中_finddata_t 結構包括了檔案的相關資訊:檔名,建立日前等屬性,你可以從你的機器中的IO.H檔案中查詢相應的定義。當然不要忘了,使用_findclose 函式關閉相應控制代碼 例如:下面程式實現把資料夾中的檔名字顯示在視窗的標題欄中。 CString pathWild ="你的目錄//*.jpg" ; struct _finddata_t c_file; long hFile; if( (hFile = _findfirst( LPCTSTR(pathWild), &c_file )) == -1L ) { ::AfxMessageBox("No image files in current directory!/n" ) ; } else {一、使用::FindFirstFile和::FindNextFile方法 find(char * lpPath) {   char szFind[MAX_PATH];   WIN32_FIND_DATA FindFileData;   strcpy(szFind,lpPath);   strcat(szFind,"*.*");   HANDLE hFind=::FindFirstFile(szFind,&FindFileData);   if(INVALID_HANDLE_VALUE == hFind)  return;   while(TRUE)   {     if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)     {       if(FindFileData.cFileName[0]!='.')       {         strcpy(szFile,lpPath);         strcat(szFile,"");         strcat(szFile,FindFileData.cFileName);         find(szFile);       }     }     else     {       cout << FindFileData.cFileName;     }     if(!FindNextFile(hFind,&FindFileData))  break;   }   FindClose(hFind); } 二、利用CFileFind類較簡潔的實現該功能 void CModelDlg::FindBmpFile(CString strFoldername) { CString m_cstrFileList=""; CFileFind tempFind; BOOL bFound; //判斷是否成功找到檔案 bFound=tempFind.FindFile(strFoldername + "\\*.*"); /修改" "內內容給限定查詢檔案型別 CString strTmp; //如果找到的是資料夾 存放資料夾路徑 while(bFound) //遍歷所有檔案 { bFound=tempFind.FindNextFile(); //第一次執行FindNextFile是選擇到第一個檔案,以後執行為選擇 //到下一個檔案 if(!tempFind.IsDots()) continue; //如果找到的是返回上層的目錄 則結束本次查詢 if(tempFind.IsDirectory()) //找到的是資料夾,則遍歷該資料夾下的檔案 { strTmp=""; strTmp=tempFind.GetFilePath(); FindFile(strTmp); } else { strTmp=tempFind.GetFileName(); //儲存檔名,包括字尾名 // 在此處新增對找到檔案的處理 } } tempFind.Close(); return; } 三、使用IO.H中的_findfirst和_findnext方法 在IO.H、WCHAR.H中提供了_finddata_t, _wfinddata_t, _wfinddatai64_t 結構,通過_findfirst可以得到滿足條件的第一個檔案的控制代碼,如下: long _findfirst( char *filespec, struct _finddata_t *fileinfo ),然後你可以使用_findnext函式得到用_findfirst的控制代碼後的檔案指標,如此就可以遍歷所有滿足條件的檔案。其中_finddata_t 結構包括了檔案的相關資訊:檔名,建立日前等屬性,你可以從你的機器中的IO.H檔案中查詢相應的定義。當然不要忘了,使用_findclose 函式關閉相應控制代碼 例如:下面程式實現把資料夾中的檔名字顯示在視窗的標題欄中。 CString pathWild ="你的目錄//*.jpg" ; struct _finddata_t c_file; long hFile; if( (hFile = _findfirst( LPCTSTR(pathWild), &c_file )) == -1L ) { ::AfxMessageBox("No image files in current directory!/n" ) ; } else { do { AfxGetMainWnd()->SetWindowText(c_file.name); } while (_findnext(hFile, &c_file) == 0); } _findclose(hFile); 對了,別忘了在你的工程中包括標頭檔案IO.H do { AfxGetMainWnd()->SetWindowText(c_file.name); } while (_findnext(hFile, &c_file) == 0); } _findclose(hFile); 對了,別忘了在你的工程中包括標頭檔案IO.H

相關推薦

vc 目錄檔案總數

使用::FindFirstFile和::FindNextFile方法 #include "StdAfx.h" #include <windows.h> #include <stdio.h> #include <string.h>

python目錄的所有檔案目錄詳細介紹

目錄結構如下圖: test---a------d------g--------g.txt test---a------d------a.txt test---a------e --------b --------c --------1.txt --------2.tx

os.walk 目錄目錄檔案

python中os.walk是一個簡單易用的檔案、目錄遍歷器,可以幫助我們高效的處理檔案、目錄方面的事情。 1.載入 要使用os.walk,首先要載入該函式 可以使用以下兩種方法 import os from os im

目錄的所有檔案

using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; name

Excel VBA 目錄所有檔案

         Application.ScreenUpdating = False         Dim MyDir As String         MyDir = ThisWorkbo

目錄所有各級子目錄檔案的全路徑

public System.Collections.Specialized.StringCollection GetAllFiles(string rootdir){ System.Collections.Specialized.StringCollection result

shell 目錄字尾名為.html的檔案,並替換檔案中內容

1、shell查詢 .html檔案,例: find /usr/local/guotom/webapps/ZingMH/nodeJs/tpls/views/ -type f -name '*.html' 2、替換方法 sed -i "s/邀請/呵呵/g"【將邀請替換為呵呵】

目錄所有資料夾和檔案-------遞迴刪除空目錄

遍歷資料夾下所有檔案有兩種方法備註p為列印函式相當於print_r(),var_dump()的友好輸出第一種://遍歷資料夾下所有檔案和目錄//opendir():將目錄下的檔案已資源的形式儲存 //r

python 目錄所有檔案

#!/usr/bin/python # -*- coding: utf-8 -*- import os def gci(filepath): #遍歷filepath下所有檔案,包括子目錄 files = os.listdir(filepath) for fi in

C/C++目錄檔案或指定檔案

每次遇到這樣的問題總會折騰很久,到網上搜,或者查資料,弄了很多次,但就是沒記住,這次寫程式又遇到了,乾脆就把它都弄清楚了,然後順便在這裡記錄一下,以後再遇到就不用到處去找了。         用 C/C++ 遍歷目錄檔案主要有兩種方式,分別對應在 Windows VS

Python遞迴目錄所有檔案查詢指定檔案

之前看到網上有人說『os.path.isdir()判斷必須寫絕對路徑』,當時心想Python不是有迭代上下文嗎,為什麼不行?遂作本文驗證之 程式碼部分 考慮用一個path變數指代當前遍歷元素的絕對路徑(正確做法) def search(ro

is_dir(),opendir(),readdir()簡單目錄檔案

function listdoc($dir){ if(is_dir($dir)){//判斷是否是目錄 if($hd = opendir($dir)){//是目錄,則開啟 while($fi

perl 遞迴地目錄檔案

#!/usr/bin/perl -w use strict; use File::Spec; local $\ ="\n";#當前模組的每行輸出加入換行符 my %options;

MFC 目錄指定型別的檔案並複製

void Recurse(CString strDir,CString strExt) { //在指定目錄下查詢指定副檔名的檔案 CFileFind finder; CString strCurrDir; strCurrDir = strDir +

C/C++目錄的所有文件(Windows/Linux篇,超詳細)

檢查 msd 字符 size tro 也會 結構 () alt 前面的一篇文章我們講了用Windows API遍歷一個目錄下的所有文件,這次我們講用一種Windows/Linux通用的方法遍歷一個目錄下的所有文件。 Windows/Linux的IDE都會提供一個頭文件—

[轉載]Python遞歸目錄所有文件

cnblogs 需要 os.walk ext 包含 mage ring wal exe #自定義函數: import ospath="D:\\Temp_del\\a"def gci (path):"""this is a statement"""parents = os.l

使用python目錄瀏覽檔案屬性

設計一個python模組ShowFileProperties.py來檢視path目錄下所有檔案的屬性。 通過給定的目錄路徑檢視檔案的名稱大小,建立時間,最後修改時間。 1.遍歷path指定的目錄,獲取每個子目錄的路徑, 2.遍歷子目錄下的所有檔案,並返回檔案的屬性列表 3.

C++11:for_each_file目錄處理檔案

經常我們需要對某個目錄下的所有檔案進行處理,這裡我們需要列出目錄下的檔案,並找出符合要求的檔案,然後才開始真正的處理工作。大部分情況下,這個流程都差不多,只是檔案處理的內容不同,可不可以做一個類似#include<algorithm>中的for_ea

C++ 目錄所有檔案並判斷是否為目錄

1. 思路   使用FindFirstFile、FindNextFile函式,來遍歷目錄。結果儲存在WIN32_FIND_DATA結構體中。將查詢結果的dwFileAttributes和FILE_ATTRIBUTE_DIRECTORY取與操作,判斷是否為目錄。

os.walk()os.path.join()目錄檔案

os.walk() os.walk() 方法用於通過在目錄樹中游走輸出在目錄中的檔名,向上或者向下。 os.walk(top[, topdown=True[, onerror=None[, followlinks=False]]]) top 為要遍歷的目錄: top