VC下如何遍历文件夹及嵌套文件夹

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC下如何遍历文件夹及嵌套文件夹相关的知识,希望对你有一定的参考价值。

在VC中如何遍历出文件下的所有文件,以及嵌套文件夹下的文件

参考技术A BOOL EmuFilesToDes(const char * path)

CString cs(path);
WIN32_FIND_DATA data;
cs += "\\*.*";
HANDLE handle = FindFirstFile((LPCSTR)cs,&data);
BOOL ret = (BOOL)handle;
while(ret)

if(data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)

if(data.cFileName[0] != '.') //IsDirectory

cs = path;
cs +="\\";
cs += data.cFileName;
EmuFilesToDes((LPCSTR)cs);


else //is a file


cs = path;
cs +="\\";
cs += data.cFileName;

OutputDebugstring(cs);


ret = FindNextFile(handle,&data);


FindClose(handle);
return bSuccess;

使用Dir,遍历文件夹下所有子文件夹及文件

-------------------------------------------获取某文件夹下所有文件和子目录下的文件-------------------------------------------
Sub getAllFile()

Cells.ClearContents

Call getFileNm(ChooseFolder(), 0, 0)

MsgBox "处理完成!"

End Sub

-------------------------------------------获取目标文夹路径-------------------------------------------
Public Function ChooseFolder() As String
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog(msoFileDialogFolderPicker)
    With dlgOpen
        If .Show = -1 Then
                ChooseFolder = .SelectedItems(1)
        End If
    End With
    Set dlgOpen = Nothing
End Function

-------------------------------------------获取文件夹下所有文件和文件夹名称
‘row worksheet里的打印行
‘col worksheet里的打印列(根据层次显示)
------------------------------------------- Public Sub getFileNm(ByVal subFolderPath As String, _ ByRef row As Integer, _ ByVal col As Integer) Dim fileName As String Dim subFolderVisited As String col = col + 1 subFolderPath = subFolderPath & "\" fileName = Dir(subFolderPath, vbDirectory) Do While fileName <> "" If fileName <> "." And fileName <> ".." Then If (GetAttr(subFolderPath & fileName)) = vbDirectory Then 文件夹的场合 row = row + 1 Worksheets(1).Cells(row, col) = fileName Call getFileNm(subFolderPath & fileName, row, col) Else 文件的场合 row = row + 1 Worksheets(1).Cells(row, col) = fileName End If End If 获取执行递归前的下一个文件 subFolderVisited = Dir(subFolderPath, vbDirectory) Do While subFolderVisited <> fileName subFolderVisited = Dir Loop fileName = Dir Loop End Sub

 

以上是关于VC下如何遍历文件夹及嵌套文件夹的主要内容,如果未能解决你的问题,请参考以下文章

bat命令,如何只遍历当前文件夹及下一层子文件夹

Android如何遍历特定目录下所有文件

VC下文件的使用

Python中如何遍历指定目录下的所有文件?

如何使用perl遍历一个目录下的所有文件

C#,给定一个目录,遍历该目录下的所有文件、文件夹