Skip to content

Instantly share code, notes, and snippets.

@40tude
Created January 9, 2014 14:07
Show Gist options
  • Select an option

  • Save 40tude/8334627 to your computer and use it in GitHub Desktop.

Select an option

Save 40tude/8334627 to your computer and use it in GitHub Desktop.
LabWindows/CVI 2013 - Windows This ANSI C code shows how to walk directories using Win SDK (recursively) as well as CVI function calls (not recursively) See http://www.40tude.fr/blog/parcours-de-repertoires-de-maniere-recursive/ for more details (french)
#include <windows.h>
#include <utility.h>
#include "toolbox.h"
#include <stdio.h>
#include <string.h>
// ----------------------------------------------------------------------------
static void WalkDirectoryTree1(const char dir[]);
static void WalkDirectoryTree2(const char dir[]);
// ----------------------------------------------------------------------------
static int gFound=0;
// ----------------------------------------------------------------------------
int main (int argc, char *argv[]) {
const char RootDir[MAX_PATHNAME_LEN]= "C:\\Users\\Public\\Documents\\National Instruments\\CVI2013\\samples";
double t1 = Timer ();
WalkDirectoryTree1(RootDir);
double t2 = Timer ();
printf("%f\n", t2-t1);
printf("Found %d .c files\n", gFound);
gFound=0;
double t3 = Timer ();
WalkDirectoryTree2(RootDir);
double t4 = Timer ();
printf("%f\n", t4-t3);
printf("Found %d .c files\n", gFound);
getchar();
return 0;
}
// ----------------------------------------------------------------------------
static void WalkDirectoryTree1(const char RootDir[]) {
typedef char t_Dir[MAX_PATHNAME_LEN] ;
char PathAndFilePattern[MAX_PATHNAME_LEN];
sprintf(PathAndFilePattern,"%s\\*.c", RootDir);
char FileName[MAX_FILENAME_LEN];
if (!GetFirstFile (PathAndFilePattern, 1, 0, 0, 0, 0, 0, FileName)) {
do{
// Process the file here
gFound++;
} while (!GetNextFile (FileName));
}
char DirPattern[MAX_PATHNAME_LEN];
sprintf(DirPattern,"%s\\*", RootDir);
char DirName[MAX_FILENAME_LEN];
if (!GetFirstFile (DirPattern, 0, 0, 0, 0, 0, 1, DirName)) {
ListType hList = ListCreate (MAX_PATHNAME_LEN);
do{
ListInsertItem (hList, DirName, END_OF_LIST);
}while (!GetNextFile (DirName));
int numDir = ListNumItems (hList);
t_Dir *pListData = (t_Dir*)ListGetDataPtr (hList);
for (int i=0; i!=numDir; ++i) {
sprintf(DirPattern,"%s\\%s", RootDir, *pListData);
WalkDirectoryTree1(DirPattern);
pListData++;
}
ListDispose (hList);
}
}
// ----------------------------------------------------------------------------
static void WalkDirectoryTree2(const char RootDir[]) {
char DirPattern[MAX_PATHNAME_LEN];
sprintf(DirPattern,"%s\\*", RootDir);
WIN32_FIND_DATA ffd;
HANDLE hFind = FindFirstFile(DirPattern, &ffd);
if (hFind!=INVALID_HANDLE_VALUE) {
do{
if(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY){
if (ffd.cFileName[0] == '.') continue;
char NewRootDir[MAX_PATHNAME_LEN];
sprintf(NewRootDir,"%s\\%s", RootDir, ffd.cFileName);
WalkDirectoryTree2(NewRootDir);
}
}while(FindNextFile(hFind, &ffd));
FindClose(hFind);
}
char PathAndFilePattern[MAX_PATHNAME_LEN];
sprintf(PathAndFilePattern,"%s\\*.c", RootDir);
hFind = FindFirstFile(PathAndFilePattern, &ffd);
if (hFind!=INVALID_HANDLE_VALUE) {
do{
// Process the file here
gFound++;
} while (FindNextFile(hFind, &ffd));
}
FindClose(hFind);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment