Skip to content

Instantly share code, notes, and snippets.

@Rainyan
Created June 21, 2023 00:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rainyan/06aa0083ac59aa3defcf8e09d107593d to your computer and use it in GitHub Desktop.
Save Rainyan/06aa0083ac59aa3defcf8e09d107593d to your computer and use it in GitHub Desktop.
Get files of a directory as DataPack for SourceMod
// For path "dir_path", get files that match the "filter_by_extension" filter,
// and pass them by reference to a valid DataPack "out_datapack".
// Optionally supports SM's OpenDirectory parameters.
// Returns the number of files passed by reference.
// Caller is responsible for initializing and freeing the DataPack memory.
int GetFilesOfDir(const char[] dir_path, DataPack out_datapack,
const char[] filter_by_extension="",
bool use_valve_fs=false, const char[] valve_path_id="GAME")
{
int num_files_found = 0;
DirectoryListing dir = OpenDirectory(dir_path, use_valve_fs,
valve_path_id);
if (dir == null)
{
LogError("Failed to open directory");
return 0;
}
char filename[PLATFORM_MAX_PATH];
FileType ft;
int extension_len = strlen(filter_by_extension);
while(dir.GetNext(filename, sizeof(filename), ft))
{
if (ft != FileType_File)
{
continue;
}
if (extension_len > 0)
{
int pos = StrContains(filename, filter_by_extension);
if (pos <= 0)
{
continue;
}
int extpos = strlen(filename) - extension_len;
if (pos != extpos)
{
continue;
}
}
out_datapack.WriteString(filename);
++num_files_found;
}
return num_files_found;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment