Simple File/Folder operations using GIO


Simple operations on files and folders can be done using the Gio library. Before we can further using Gio to perform file operations needs to be explained.

To do any file/folder operations, you have to create a Gio object called a GLocalFile object and associate it with the name of the file or folder you are going to perform on it. It needs to be noted that while this GLocalFile object is associated with a file name, it is best not to think of it as a physical link to that file. The reason for this is that you have to create GLocalFile objects in order to create new files or folders and therefore the file does not exist when you create the GLocalFile object. The creation of these files/folders are done by performing additional commands or creating other Gio objects to link to the physical location. The best way of explaining this is by how to create a folder. Firstly you create the GLocalFile object and associate it with the name of the folder you want to create:

var Gio =;
var file = Gio.file_new_for_path("my_folder");

A GLocalFile object called file is created that is associated to my_folder which will be the name of the new folder. As you can see, as the folder has not been created yet, the object is not a physical link to the file. This is done by the following file:


Query File/Folder information

There may be instances where you need to get information about files or folders. Gio has functions to do this. These functions return a result, whether boolean true/false or some specific information like a file name. The following examples are written so that these results are stored in variables and then used in if statements. I have done this for ease of understanding, but these statements can be combined with the if statements i.e.:

if (file.query_exists()) do something;

As mentioned in the introduction, a GLocalFile object needs to be created before information about file/folder can be retrieved. As mentioned before, the following code does this:

var Gio =;
var file = Gio.file_new_for_path("test.txt");

The following examples will use this GLocalFile object.

To find out if a file exists you have will have to create a GLocalFile object with the name of the file and use the query_exists() function. If the file exists then true is returned else false. An example is as follows:

var file_exists = file.query_exists();
if (file_exists == true) print ("file exists"); else ("file doesn't exist"); 

To get the full file path of the file use the get_path() function. An example is as follows:

var file_path = file.get_path();
print (file_path);

This produces the full path name including the name of the file i.e. /home/foo/bar/temp.txt. If you are just looking for the file path i.e. /home/foo/bar, rather than using string manipulation functions, Gio provides a way of doing this by using the get_parent() function. As it's name suggests, it gets the parent of the file which will be the folder that the file or folder belongs to. You then use the get_path() function to get the address. Example as follows:

var parent = file.get_parent();

There may be instances where you need to know what type of file it is, i.e, an ordinary file, folder, link or special file. The function to get this is by the query_file_type() function. This returns a value which corresponds to fields in the Gio.FileType object. These fields contains descriptions of these file types which makes it easier to read. The details of the fields are as follows (Details from the GIO reference manual):


File's type is unknown.


File handle represents a regular file.


File handle represents a directory.


File handle represents a symbolic link (Unix systems).


File is a "special" file, such as a socket, fifo, block device, or character device.


File is a mountable location.

The following line of code is how the file_type function is used:

var file_type = file.query_file_type(); 

You would use the results with the Gio.FileType object. The following example uses an if statement to see if the file is a folder/directory:

if (file_type == Gio.FileType.DIRECTORY) print ("Is a folder");

If you wanted to see if it was a regular file:

if (file_type == Gio.FileType.REGULAR) print ("Is a regular file");

File/Folder commands

Again with file/folder commands, a GLocalFile object needs to be created:

var Gio =;
var file = Gio.file_new_for_path("test.txt");

To delete a file use the trash() function:


As mentioned in the introduction, to create a folder use the make_directory() function:


You can get a list of a files in a particular folder. These are stored in a FileEnumerator object. You would then use this with a while function to use the individual file-names in the object. Firstly, you have to create a GLocalFile of the object. An example of this would be if you wanted to get the contents of folder '/foo/bar':

var Gio =;
file = Gio.file_new_for_path("/foo/bar");

If you wanted to get the contents of the current folder:

var Gio =;
file = Gio.file_new_for_path(".");

To get the list of files in the GLocalFileEnumerator object you would use the enumerate_children() function on the GLocalFile object:

enumerator = file.enumerate_children("*");

This returns a GLocalFileEnumerator object with all the attributes of the files. If you only wanted to get certain attributes you would name them as parameters in the enumerate_children() function. Full details are in the GIO reference manual. For example if you wanted just to get the name, size and type attributes you would use the following:

enumerator = file.enumerate_children("standard::name,standard::size, standard::type");

To get the file you would use the next_file() function on the GLocalFileEnumerator object.

child = enumerator.next_file()   

On the first call of this function, this will get the first file in the list and return the result as a GFileInfo object in which you can get information about it. On calling it again, the next file will be returned, and so on, until the last file is called. The following example displays the name, size and type of all files in the list:

while ((child = enumerator.next_file())){
    Seed.printf("%s\t%d",child.get_name(), child.get_size());

Projects/Seed/Tutorial/simple_gio (last edited 2013-11-22 19:19:53 by WilliamJonMcCann)