FileEnumerator


Object Hierarchy:

GLib.FileEnumerator GLib.FileEnumerator GLib.FileEnumerator GLib.Object GLib.Object GLib.Object->GLib.FileEnumerator

Description:

[ CCode ( type_id = "g_file_enumerator_get_type ()" ) ]
public class FileEnumerator : Object

`GFileEnumerator` allows you to operate on a set of [iface@Gio.

File] objects, returning a [class@Gio.FileInfo] structure for each file enumerated (e.g. [method@Gio.File.enumerate_children] will return a `GFileEnumerator` for each of the children within a directory).

To get the next file's information from a `GFileEnumerator`, use [method@Gio.FileEnumerator.next_file] or its asynchronous version, [ method@Gio.FileEnumerator.next_files_async]. Note that the asynchronous version will return a list of [class@Gio.FileInfo] objects, whereas the synchronous will only return the next file in the enumerator.

The ordering of returned files is unspecified for non-Unix platforms; for more information, see [method@GLib.Dir.read_name]. On Unix, when operating on local files, returned files will be sorted by inode number. Effectively you can assume that the ordering of returned files will be stable between successive calls (and applications) assuming the directory is unchanged.

If your application needs a specific ordering, such as by name or modification time, you will have to implement that in your application code.

To close a `GFileEnumerator`, use [method@Gio.FileEnumerator.close], or its asynchronous version, [method@Gio.FileEnumerator.close_async]. Once a `GFileEnumerator` is closed, no further actions may be performed on it, and it should be freed with [method@GObject.Object.unref].

Example: List all files in a directory, sync:

private void list_children (File file, string space = "", Cancellable? cancellable = null) throws Error {
FileEnumerator enumerator = file.enumerate_children (
"standard::*",
FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
cancellable);

FileInfo info = null;
while (cancellable.is_cancelled () == false && ((info = enumerator.next_file (cancellable)) != null)) {
if (info.get_file_type () == FileType.DIRECTORY) {
File subdir = file.resolve_relative_path (info.get_name ());
list_children (subdir, space + " ", cancellable);
} else {
print ("%s%s\n", space, info.get_name ());
print ("%s %s\n", space, info.get_file_type ().to_string ());
print ("%s %s\n", space, info.get_is_symlink ().to_string ());
print ("%s %s\n", space, info.get_is_hidden ().to_string ());
print ("%s %s\n", space, info.get_is_backup ().to_string ());
print ("%s %"+int64.FORMAT+"\n", space, info.get_size ());
}
}

if (cancellable.is_cancelled ()) {
throw new IOError.CANCELLED ("Operation was cancelled");
}
}

public static int main (string[] args) {
if (args.length != 2) {
print ("%s [DIRECTORY]\n", args[0]);
return 0;
}

File file = File.new_for_commandline_arg (args[1]);

try {
list_children (file, "", new Cancellable ());
} catch (Error e) {
print ("Error: %s\n", e.message);
return 0;
}

return 0;
}

valac --pkg gio-2.0 GLib.File.enumerate_children.vala

Example: List all files in a directory, async:

public static int main (string[] args) {
File file = File.new_for_path (".");
MainLoop loop = new MainLoop ();

file.enumerate_children_async.begin ("standard::*", FileQueryInfoFlags.NOFOLLOW_SYMLINKS, Priority.DEFAULT, null, (obj, res) => {
try {
FileEnumerator enumerator = file.enumerate_children_async.end (res);
FileInfo info;
while ((info = enumerator.next_file (null)) != null) {
print ("%s\n", info.get_name ());
print ("\t%s\n", info.get_file_type ().to_string ());
print ("\t%s\n", info.get_is_symlink ().to_string ());
print ("\t%s\n", info.get_is_hidden ().to_string ());
print ("\t%s\n", info.get_is_backup ().to_string ());
print ("\t%"+int64.FORMAT+"\n", info.get_size ());
}
} catch (Error e) {
print ("Error: %s\n", e.message);
}

loop.quit ();
});

loop.run ();
return 0;
}

valac --pkg gio-2.0 GLib.File.enumerate_children_async.vala


Namespace: GLib
Package: gio-2.0

Content:

Properties:

Creation methods:

Methods:

Inherited Members: