GameMaker: Studio

GameMaker: Studio

View Stats:
Zappy Sep 13, 2018 @ 1:56am
Use wildcard for folders in file_find_first()?
I'm trying to make something where I'd like to find every folder that contains a file called "File.ini". This is basically the script that I'm using:
var _result=0; if file_find_first(working_directory+"Folder\*\File.ini",0)!="" { _result=1; while file_find_next()!="" _result+=1; } file_find_close(); return _result;
In working_directory, I have a folder called "Folder", and inside of that, I have a folder called "Subfolder" and another called "Subfolder2", both of which have "File.ini" within them.

But yet, the above script returns 0, not 2. If I change the file-path stuff of file_find_first to working_directory+"Folder\Subfolder\File.ini" or working_directory+"Folder\Subfolder\*.ini", however, it will return 1, so the issue is with the wildcard (*) being unsupported in folder names.

Is there a proper way to use wildcards in folder names?
Last edited by Zappy; Sep 13, 2018 @ 1:58am
Originally posted by Shatter:
As far as I can tell, the proper way to use wildcards in folder names is to specify them as the last part of the search mask.

Folder\* will work as a search to find all files. You will have to specify fa_directory if you want to include folders in your search as well.

Folder\*\File.ini therefore will not work.

You could rework your script to recursively search arbitrarily-named folders to test for the presence of File.ini or any other file.

Perhaps something called scrFind that looks more like the following:
// scrFind var _folder = argument0 + "\"; var _fname = argument1; var _result = 0; var _queue = ds_queue_create(); // search folder for filename (may contain wildcards) if ( file_find_first(_folder + _fname, 0) != "" ) _result = 1; file_find_close(); var _file = file_find_first(_folder + "*", fa_directory); while ( _file != "" ) { if ( directory_exists(_folder + _file) ) ds_queue_enqueue(_queue, _file); _file = file_find_next(); } file_find_close(); while ( !ds_queue_empty(_queue) ) { _result += scrFind(_folder + ds_queue_dequeue(_queue), _fname); } ds_queue_destroy(_queue); return _result;
call scrFind(working_directory + "Folder", "File.ini") to recursively search all files/folders under "working_directory\Folder" for File.ini and return a count of folders found.

or call scrFind(working_directory + "Folder", "*.ini") to search for all .ini files.

or more generally scrFind(working_directory + "Folder", "Name.filetype")

hope this helps.
< >
Showing 1-5 of 5 comments
The Winter Bud Sep 13, 2018 @ 4:58am 
from my research, only the file name can use the wildcard symbol. As in the example the Documentation gives under directory_exists(filename)[docs.yoyogames.com]

if directory_exists(working_directory + "\Saves\") { file = file_find_first(working_directory + "\Saves\*.doc", fa_readonly); }
The Winter Bud Sep 13, 2018 @ 5:02am 
If you know ahead of time what subdirectories you have you could store subfolder names in a list and then iterate through the list

// assuming you addes all subfolder names to a ds_list called folderList var _result=0; for(var nameIndex=0;nameIndex<ds_list_size(folderList);nameIndex++){ if file_find_first(working_directory+ "Folder\"+ ds_list_find_value(folderList,nameIndex)+ "\File.ini",0)!="" { _result=1; while file_find_next()!="" _result+=1; } } file_find_close(); return _result;
Last edited by The Winter Bud; Sep 13, 2018 @ 5:05am
Zappy Sep 13, 2018 @ 5:09am 
Originally posted by The Winter Bud:
If you know ahead of time what subdirectories -
Sadly, I don't; The whole point was that there could be as many arbitrarily-named folders as wanted.

I'm not going to be storing that many files per folder (like, usually less than 5-ish), however, so I may just store them like "Folder\Name1.filetype1" and "Folder\Name1.filetype2" and so on, then repeating that for "Name2" or such, storing all of the files in the same folder. Not what I wanted, but if I can't do exactly what I want, then I'm okay with a compromise.
Last edited by Zappy; Sep 13, 2018 @ 5:10am
The author of this thread has indicated that this post answers the original topic.
Shatter Sep 13, 2018 @ 7:17am 
As far as I can tell, the proper way to use wildcards in folder names is to specify them as the last part of the search mask.

Folder\* will work as a search to find all files. You will have to specify fa_directory if you want to include folders in your search as well.

Folder\*\File.ini therefore will not work.

You could rework your script to recursively search arbitrarily-named folders to test for the presence of File.ini or any other file.

Perhaps something called scrFind that looks more like the following:
// scrFind var _folder = argument0 + "\"; var _fname = argument1; var _result = 0; var _queue = ds_queue_create(); // search folder for filename (may contain wildcards) if ( file_find_first(_folder + _fname, 0) != "" ) _result = 1; file_find_close(); var _file = file_find_first(_folder + "*", fa_directory); while ( _file != "" ) { if ( directory_exists(_folder + _file) ) ds_queue_enqueue(_queue, _file); _file = file_find_next(); } file_find_close(); while ( !ds_queue_empty(_queue) ) { _result += scrFind(_folder + ds_queue_dequeue(_queue), _fname); } ds_queue_destroy(_queue); return _result;
call scrFind(working_directory + "Folder", "File.ini") to recursively search all files/folders under "working_directory\Folder" for File.ini and return a count of folders found.

or call scrFind(working_directory + "Folder", "*.ini") to search for all .ini files.

or more generally scrFind(working_directory + "Folder", "Name.filetype")

hope this helps.
Zappy Sep 13, 2018 @ 7:40am 
Originally posted by Shateaux Gateaux:
- You will have to specify fa_directory if you want to include folders in your search as well. -
That sounds nice. For the thing that I'm trying to do, that might work, though I can't test stuff related to that right now. (But I'll keep it in mind for the next time that I work on this project.)

Originally posted by Shateaux Gateaux:
- You could rework your script to recursively search arbitrarily-named folders to test for the presence of File.ini or any other file. -
To be fair, the only reason that I wanted to check for File.ini or such in the first place was so that I could find(/could've hopefully found) the folders. In this script, I simply care about the amount of folders, not their contents. Stuff relating to the contents will come in a more specialized manner on its own.

Originally posted by Shateaux Gateaux:
- Perhaps something called scrFind that looks more like the following:
- _result += scrFind(_folder + ds_queue_dequeue(_queue), _fname); -
I don't think that that would work. I remember GameMaker: Studio telling me off once for trying to call a script from within that same script, so having a script called scrFind use scrFind wouldn't even compile the executable if I remember correctly.

Originally posted by Shateaux Gateaux:
- hope this helps.
Despite the above two responses of mine, yes, that should help a lot. Thank you.
Last edited by Zappy; Sep 13, 2018 @ 7:41am
< >
Showing 1-5 of 5 comments
Per page: 1530 50

Date Posted: Sep 13, 2018 @ 1:56am
Posts: 5