Traversing Windows Files

Time:2019-10-9

File traversal

abstract
Windows file traversal and process traversal are very similar, if you do not understand the process traversal recommendation to see my previous two articles traversal system process and traversal system process module! Traveling through files you need to knowWIN32_FIND_DATAThis structure andGetCurrentDirectory FindFirstFile FindNextFile FindCloseThese APIs

thinking
I will not elaborate on the idea, the code can be understood at a glance, mainly to obtain the path of the folder, and then usestrcatEqual function stitching file names you want to traverse, and then useFindFirstFileandFindNextFileYou can traverse the file. Be careful*.* *.dll h???.*Equal formatted characters are similar to regular expressions and are used to find specified files.dwFileAttributesKeyword is attribute information of file

Complete code

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

int main(int args, char *argv[])
{
    HANDLE hFile;
    char path[MAX_PATH];
    char filepath[MAX_PATH];
    WIN32_FIND_DATA fileData;
    GetCurrentDirectory(MAX_PATH, path);
    strcpy(filepath, path);
    strcat(filepath, "\*.*");
    hFile = FindFirstFile(filepath, &fileData);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        do
        {
            printf("%s: ", fileData.cFileName);
            if (fileData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE)
            {
                printf("<CommonFile> ");
            }
            if (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
            {
                printf("<Directory> ");
            }
            if (fileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
            {
                printf("<Hidden> ");
            }
            if (fileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
            {
                printf("<ReadOnly> ");
            }
            if (fileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM)
            {
                printf("<SystemUse> ");
            }
            if (fileData.dwFileAttributes & FILE_ATTRIBUTE_TEMPORARY)
            {
                printf("<Temporary> ");
            }
            printf("\n");
        } while (FindNextFile(hFile, &fileData));
    }
    FindClose(hFile);
    system("pause");
    return 0;
}

Compile commandgcc file1.c -o file1

Operational screenshots

Traversing Windows Files

END

Recommended Today

Oracle (PLSQL) introduction 1

Oracle installation, user authorization, table operation, data type, DDL table, DML data. Next: Oracle introduction 2 Learning video: https://www.bilibili.com/video/BV1tJ411r7EC?p=15 Baidu cloud installation package is attached to the installation tutorial: https://blog.csdn.net/qq_ 40774525/article/details/79919057 Oracle Baidu cloud installation package: https://pan.baidu.com/s/1qVWwCUR37j2JxEecYbe5HQ Extraction code: 5abz After the Oracle service above is installed, open the client of SQL developer and the […]