GMLscripts.com

Discuss and collaborate on GML scripts
Invert

You are not logged in.

#1 2008-03-24 13:26:16

Quimp
Member
Registered: 2007-10-09
Posts: 15

file_find_list

I came across this set of scripts. The scr_find_files() looks fantastic: http://gmc.yoyogames.com/index.php?showtopic=256775

I adapted the script so it creates the list automatically instead of using a user-provided one, I made it possible to search for more than one extension at a time and finally I added an argument to search through subdirectories.

Expand// This script searches all files under the specified directory.
// file_find_list(root, extensions, subdir);
// returns a list data structure containing the full path of the files found
// my_list = file_find_list("C:\Windows", ".jpg|.gif|.bmp", true);

var list, subdir_queue, file_name, dir_name, full_path;

list = ds_list_create();
subdir_queue = ds_queue_create();
ds_queue_enqueue(subdir_queue, argument0);
argument1 = "|" + argument1 + "|";

while (!ds_queue_empty(subdir_queue))
 {
  dir_name = ds_queue_dequeue(subdir_queue);
  file_name = file_find_first(dir_name + '\*', fa_directory);

  while (file_name != '')
   {
    full_path = dir_name + '\' + file_name;
    
    if (file_attributes(full_path, fa_directory) && argument2)
     {
      if (file_name != '.' && file_name != '..')
       {
        ds_queue_enqueue(subdir_queue, full_path);
       }
     }
    else
     {
      if (string_pos("|" + string_lower(filename_ext(file_name)) + "|", argument1) > 0)
       {
        ds_list_add(list, full_path);
       }
     }
    file_name = file_find_next();
   }
  file_find_close();
 }

ds_queue_destroy(subdir_queue);
return (list);

A quick test (don't forget to destroy the list) :

Expandlist = file_find_list(get_directory(''), get_string("File extension :", ".jpg|.gif|.bmp"), true);
Expandvar i;
for (i=0; i<ds_list_size(list); i+=1)
 {
  draw_text(50, 50 + 30*i, ds_list_find_value(list, i));
 }

The script would probably be faster if instead of delimiting the extensions the script asked for an array (for loop comparison) or a list (ds_list_find_index > -1). Have your say!

Cheers,
Quimp

Offline

#2 2008-03-24 16:06:04

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: file_find_list

Thanks for pointing out this script, Quimp. I'd been wanting to get something like this for the site, I just never got around to writing it. Since at first glance this looks pretty close to torigara's script I'll need to get his permission before I can accept this.


Abusing forum power since 1986.

Offline

#3 2008-03-24 17:34:12

Grand-High Gamer
Member
Registered: 2007-10-09
Posts: 18

Re: file_find_list

Nice work Quimp. I'll be using this one.


sigimage1.png

Offline

#4 2008-03-24 17:44:11

paul23
Member
Registered: 2007-10-17
Posts: 110

Re: file_find_list

just wondering, why do you use this check:
            if (file_name != '.' && file_name != '..')
? - I believe directories can't have a name of simply dots, so it can't happen that that if statment returns false. And if it is to prevent faultry filenames, (which windows prevents), you should also check for: "\/?:*<>|" - and for any number of dots, like:

Expandif (string_count(file_name,".")=string_length(filename) /*&& string_pos("\") = 0 && string_pos("/") = 0 && string_pos("?") = 0  && string_pos(":") = 0  && string_pos("*") = 0 && string_pos("<") = 0 && string_pos(">") = 0 && string_pos("|") = 0*/ )

(commented code is if you want to test it completely)

However there might be a better reason to check for 1 or 2 dots?

Offline

#5 2008-03-24 17:49:01

Quimp
Member
Registered: 2007-10-09
Posts: 15

Re: file_find_list

The big chuck was indeed written by torigara. I was heading towards PMing him/her for a submission here, when I finally decided to make some modifications. But they're minor, so I'd rather see torigara in the credits than my name.

Offline

#6 2008-03-24 18:03:21

xot
Administrator
Registered: 2007-08-18
Posts: 1,239

Re: file_find_list

paul23 wrote:

However there might be a better reason to check for 1 or 2 dots?

Those are special meta-directories. One dot means the current directory, two dots mean the parent directory. I haven't tested this out yet to see if the check is necessary, but it's hardly unusual for this kind of procedure; in many languages/APIs not checking for them can lead to an infinite loop.

Quimp wrote:

The big chuck was indeed written by torigara. I was heading towards PMing him/her for a submission here, when I finally decided to make some modifications. But they're minor, so I'd rather see torigara in the credits than my name.

Both of you would get credit, and maybe paul23 as well. Now somebody cook up an indefinite gender pronoun for English, I really despise he/she/him/her.


Abusing forum power since 1986.

Offline

#7 2008-03-25 00:13:15

Yourself
Member
Registered: 2007-10-09
Posts: 48

Re: file_find_list

I always use "they" for that.

http://en.wikipedia.org/wiki/Singular_they

Offline

Board footer

Powered by FluxBB