i whant to program that can find all files in c:\ folder , the idea is to using linked list to store the folders and subfolders name , than using a fonction that will get the files in those folders
i have made a floder d with some subfolders in c:\ for test , when i run the program i get some symboles in floders name , any idea to how fixt it?
Target file is C:\d4☺oj♦
Target file is C:\d\k\
Target file is C:\d\pol♦
Target file is C:\d\volo\
the code is:
#include <windows.h> #include <tchar.h> #include <stdio.h> #include <string - Pastebin.com
Code:
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <string.h>
WIN32_FIND_DATA FindFileData;
HANDLE hFind;
void files(HANDLE hFind,TCHAR r[]);
void onlyfiles(HANDLE hFind,TCHAR r[]);
TCHAR pd[]="C:\\d\\";
TCHAR z[]="*.*";
TCHAR r[];
TCHAR data[];
struct node{
struct node *link;
char folder[];
};
struct node *start = NULL;//initialiser au null
struct node *newnode;
void insertatend(struct node *start,char *data){
struct node *p,*temp;
temp=(struct node *)malloc(sizeof(struct node));
strcpy(temp->folder,data);
p=start;
while(p->link!=NULL)
p=p->link;
p->link=temp;
temp->link=NULL;
}
void displaylist(struct node *start){
struct node *p;
if(start==NULL)
{
printf("list is empty\n");
return;
}
p=start->link;
while(p!=NULL)
{
_tprintf (TEXT("Target file is %s\n"), p->folder);
//strcpy(r,p->folder);
//strcat(r, "\\*.*");
//onlyfiles(hFind,r);
p=p->link;
}
printf("\n");
}//end of displaylist()
void _tmain()
{
strcpy(data,pd);
newnode=malloc(sizeof(struct node));
strcpy(newnode->folder,pd);
newnode->link=NULL;
start=newnode;
strcat(r, pd);
strcat(r, z);
files(hFind,r);
FindClose(hFind);
displaylist(start);
}
void files(HANDLE hFind,TCHAR r[]){
hFind = FindFirstFile(r, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFile failed (%d)\n", GetLastError());
return;
}
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0){
_tprintf (TEXT("The first file found is %s\n"),
FindFileData.cFileName);
}
while(FindNextFile(hFind, &FindFileData) != 0){
if(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY && strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0){
strcat(data, FindFileData.cFileName);
strcat(data, "\\");
insertatend(start,data);
strcpy(data,pd);
}
}
}
void onlyfiles(HANDLE hFind,TCHAR r[]){
_tprintf (TEXT("The first file found is %s\n"),
r);
hFind = FindFirstFile(r, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
{
printf ("FindFirstFile failed (%d)\n", GetLastError());
return;
}
if(strcmp(FindFileData.cFileName, ".") != 0 &&
strcmp(FindFileData.cFileName, "..") != 0){
_tprintf (TEXT("The first file found is %s\n"),
FindFileData.cFileName);
}
while(FindNextFile(hFind, &FindFileData) != 0){
if(!(FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(FindFileData.cFileName, ".") != 0 && strcmp(FindFileData.cFileName, "..") != 0){
_tprintf (TEXT("The first file found is %s\n"),
FindFileData.cFileName);
}
}
}