问题
I want to list all the files that run under Task Scheduler by accessing all the files in the "C:\windows\system32\Tasks" Directory.
The program should recursively keep opening subfolders in the directory and list all the files. I currently use Windows OS.
I tried using the COM library but it doesn't display the tasks in the subfolders. I have 60 tasks but it displays only 12. So, I'm trying to iterate through the Tasks folder instead.
#include <stdio.h>
#include <dirent.h>
int main(void)
{
DIR *dir;
struct dirent *de;
if ((dir = opendir("C:\\Windows\\System32\\Tasks")) != NULL);
{
printf("The startup Programs are:\n");
while ((de = readdir(dir)) != NULL)
{
printf("%s\n", de->d_name);
}
closedir(dir);
}
getchar();
}
I expected the output to display all the files inside the current folder and the subfolders. However, the output only displays the name of the first folder and exits.
回答1:
As there is apparently no full, simple, example of recursively listing directories under windows, here is one:
#include <windows.h>
#include <stdio.h>
void listdirs(char *dir, char *mask)
{
char fspec[1024], fname[1024];
WIN32_FIND_DATA dta;
HANDLE hDta;
sprintf(fspec,"%s/%s",dir, mask);
if ((hDta= FindFirstFile (fspec, &dta)) != INVALID_HANDLE_VALUE) {
do {
if ( !(dta.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) )
{
printf ("%s/%s\n", dir, dta.cFileName);
}
else
{
if (strcmp(dta.cFileName,".")!=0 && strcmp(dta.cFileName,"..")!=0 )
{
sprintf (fname, "%s\\%s", dir, dta.cFileName);
listdirs(fname, mask);
}
}
} while (FindNextFile (hDta, &dta));
FindClose(hDta);
}
}
int main (int argc, char *argv[])
{
listdirs(argv[1],argv[2]); // Usage: progname c:\MyDir *.*
return 0;
}
来源:https://stackoverflow.com/questions/54363548/list-of-all-files-inside-the-folder-and-its-subfolders-in-windows