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

Install and configure Redis under Ubuntu16.04

Statement: the content of this article is reproduced to [Ubuntu16.04 installation and configuration of Redis] I. preconditions You need to connect to the Internet and then execute the sudo apt-get update package Execute the installation command sudo apt-get install redis-server After execution, as shown below, we enter y to confirm the installation and use the […]