Re: gtk_file_chooser_get_filenames multiple folders



oops, left a little important bit out of the readFiles() routine, i.e., the actual traversal/processing of the selected folders.  this routine should read:

static void readFiles()
{  // called on user selection from GtkFileChooser
  GSList  *selected, *sel;
  sel = selected = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(fileSel));
  while(selected)
  {
    getFiles(selected->data);
    g_free(selected->data);
    selected=selected->next;
  }
  g_slist_free(sel);
}


On Tue, Feb 8, 2011 at 11:06 AM, richard boaz <ivor boaz gmail com> wrote:
i do this just fine.  but you must do the recursive traversal yourself.

a code snippet illustrating how is below.  this is not stand-alone, i.e., it will not compile.  you must add your own code to:
  • set up and execute the file selector itself (here named fileSel)
  • write the routine registerFile(), which is responsible for registering the file with your program for subsequent processing, whatever that means to you.  (typically, a list that, once completely filled (i.e., as returned from file selector), is traversed for individual file processing; though there are certainly other ways to effectuate this.)
  • as well, the routine fileType() can be expanded to explicitly include/allow for only certain file types, returning: 
    • FTDIR  - it's a directory
    • FTFILE - it's a file to process
    • FTDONOTPROC - it's a file we don't care about, so ignore
cheers,

richard

======= BEGIN SNIPPET ============
enum {
  FTDIR,
  FTFILE,
  FTDONOTPROC
};

static int fileType(char *name)
{  // is the input a file or a directory?
  // can't use extended field d_type from dirent.h, doesn't exist on SOLARIS
  DIR    *dp;
  int   ret = FTFILE;
  
  if (!((dp=opendir(name))==NULL))
  {  // succeeded, it's a directory
    ret = FTDIR;
    closedir(dp);
  }

  return ret;
}

static void getFiles(char *dirFile)
{  // recursive function: 
  // descend into every directory and 
  //  1) if it's a directory call us again, or
  //  2) if it's a file, register for processing
  DIR    *dp;
  struct dirent *ep;
  char *newDirFile;

  if ((dp=opendir(dirFile))==NULL)
  {  // failed, it must be a file
    registerFile(dirFile);
  }
  else
  {  // succeeded, it must be a directory
    while((ep=readdir(dp)))
    {
      if (!strcmp(ep->d_name,".") || 
        !strcmp(ep->d_name, ".."))
        continue;  // don't read '.' and '..' directories

#ifdef WIN32
      newDirFile = g_strdup_printf("%s\\%s", dirFile, ep->d_name);  
#else
      newDirFile = g_strdup_printf("%s/%s", dirFile, ep->d_name);  
#endif
      switch(fileType(newDirFile))
      {
        case FTDIR:
          getFiles(newDirFile);
        break;
        case FTFILE:
          registerFile(newDirFile);
        break;
        case FTDONOTPROC:  // file type we don't care about, ignore...
        break;
      }
      free(newDirFile);
    }
    closedir(dp);
  }
}

static void readFiles()
{  // called on user selection from GtkFileChooser
  GSList  *selected, *sel;
  sel = selected = gtk_file_chooser_get_filenames(GTK_FILE_CHOOSER(fileSel));
}
======= END SNIPPET ============

2011/2/8 David Nečas <yeti physics muni cz>

On Tue, Feb 08, 2011 at 02:35:02AM +0000, Kevin S Anthony gmail com wrote:
> Is there a way, to use gtk_file_chooser_get_filenames to recursively
> descend into all the folders selected?
>
> so you select folders ABC
> and click open,
> you then go into ABC and select and return all the files in each subfolder,

No.  You can use GFileEnumerator or GDir to traverse the tree – deciding
how to handle hidden files, symlinks to directories, crossing file
system boundaries, etc. as the definition of ‘all files’ depends...

Yeti

_______________________________________________
gtk-list mailing list
gtk-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]