Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |

# QFileInfo Class Reference

The QFileInfo class provides system-independent file information. More...

    #include <QFileInfo>

Note: All the functions in this class are reentrant.

- List of all members, including inherited members
- Compatibility members

### Public Functions

- QFileInfo ()
- QFileInfo ( const QString & file )
- QFileInfo ( const QFile & file )
- QFileInfo ( const QDir & dir, const QString & file )
- QFileInfo ( const QFileInfo & fileinfo )
- ~QFileInfo ()
- QDir absoluteDir () const
- QString absoluteFilePath () const
- QString absolutePath () const
- QString baseName () const
- bool caching () const
- QString canonicalFilePath () const
- QString canonicalPath () const
- QString completeBaseName () const
- QString completeSuffix () const
- QDateTime created () const
- QDir dir () const
- bool exists () const
- QString fileName () const
- QString filePath () const
- QString group () const
- uint groupId () const
- bool isAbsolute () const
- bool isDir () const
- bool isExecutable () const
- bool isFile () const
- bool isHidden () const
- bool isReadable () const
- bool isRelative () const
- bool isRoot () const
- bool isSymLink () const
- bool isWritable () const
- QDateTime lastModified () const
- QDateTime lastRead () const
- bool makeAbsolute ()
- QString owner () const
- uint ownerId () const
- QString path () const
- bool permission ( uint permissionSpec ) const
- uint permissions () const
- QString readLink () const
- void refresh ()
- void setCaching ( bool enable )
- void setFile ( const QString & file )
- void setFile ( const QFile & file )
- void setFile ( const QDir & dir, const QString & file )
- QIODevice::Offset size () const
- QString suffix () const
- QFileInfo & operator= ( const QFileInfo & fileinfo )

## Detailed Description

The QFileInfo class provides system-independent file information.

QFileInfo provides information about a file's name and position (path) in the file system, its access rights and whether it is a directory or symbolic link, etc. The file's size and last modified/read times are also available.

A QFileInfo can point to a file with either a relative or an absolute file path. Absolute file paths begin with the directory separator "/" (or with a drive specification on Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current working directory. An example of an absolute path is the string "/tmp/quartz". A relative path might look like "src/fatlib". You can use the function isRelative() to check whether a QFileInfo is using a relative or an absolute file path. You can call the function makeAbsolute() to convert a relative QFileInfo's path to an absolute path.

The file that the QFileInfo works on is set in the constructor or later with setFile(). Use exists() to see if the file exists and size() to get its size.

To speed up performance, QFileInfo caches information about the file. Because files can be changed by other users or programs, or even by other parts of the same program, there is a function that refreshes the file information: refresh(). If you want to switch off a QFileInfo's caching and force it to access the file system every time you request information from it call setCaching(false).

The file's type is obtained with isFile(), isDir() and isSymLink(). The readLink() function provides the name of the file the symlink points to.

Elements of the file's name can be extracted with dirPath() and fileName(). The fileName()'s parts can be extracted with baseName() and extension().

The file's dates are returned by created(), lastModified() and lastRead(). Information about the file's access permissions is obtained with isReadable(), isWritable() and isExecutable(). The file's ownership is available from owner(), ownerId(), group() and groupId(). You can examine a file's permissions and ownership in a single statement using the permission() function.

If you need to read and traverse directories, see the QDir class.

## Member Function Documentation

### QFileInfo::QFileInfo ()

Constructs a new empty QFileInfo.

### QFileInfo::QFileInfo ( const QString & file )

Constructs a new QFileInfo that gives information about the given file. The file can also include an absolute or relative path.

See also setFile(), isRelative(), QDir::setCurrent(), and QDir::isRelativePath().

### QFileInfo::QFileInfo ( const QFile & file )

Constructs a new QFileInfo that gives information about file file.

If the file has a relative path, the QFileInfo will also have a relative path.

See also isRelative().

### QFileInfo::QFileInfo ( const QDir & dir, const QString & file )

Constructs a new QFileInfo that gives information about the given file in the directory dir.

If dir has a relative path, the QFileInfo will also have a relative path.

See also isRelative().

### QFileInfo::QFileInfo ( const QFileInfo & fileinfo )

Constructs a new QFileInfo that is a copy of the given fileinfo.

### QFileInfo::~QFileInfo ()

Destroys the QFileInfo and frees its resources.

### QDir QFileInfo::absoluteDir () const

Returns the file's absolute path as a QDir object.

See also filePath(), fileName(), isRelative(), and dir().

### QString QFileInfo::absoluteFilePath () const

Returns the absolute path including the file name.

The absolute path name consists of the full path and the file name. On Unix this will always begin with the root, '/', directory. On Windows this will always begin 'D:/' where D is a drive letter, except for network shares that are not mapped to a drive letter, in which case the path will begin '//sharename/'.

This function returns the same as filePath(), unless isRelative() is true.

If the QFileInfo is empty it returns QDir::currentPath().

This function can be time consuming under Unix (in the order of milliseconds).

See also isRelative() and filePath().

### QString QFileInfo::absolutePath () const

Returns the file's path absolute path.

This does not include the filename. ###

See also dir(), filePath(), fileName(), isRelative(), and path().

### QString QFileInfo::baseName () const

Returns the base name of the file without the path.

The base name consists of all characters in the file up to (but not including) the first '.' character.

Example:

        QFileInfo fi("/tmp/archive.tar.gz");
        QString base = fi.baseName();  // base = "archive"

See also fileName(), suffix(), completeSuffix(), and completeBaseName().

### bool QFileInfo::caching () const

Returns TRUE if caching is enabled; otherwise returns FALSE.

See also setCaching() and refresh().

### QString QFileInfo::canonicalFilePath () const

Returns the canonical path, i.e. a path without symbolic links or redundant "." or ".." elements.

On systems that do not have symbolic links this function will always return the same string that absoluteFilePath() returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalFilePath() returns an empty string.

See also filePath(), absoluteFilePath(), and QString::isNull().

### QString QFileInfo::canonicalPath () const

Returns the canonical path, i.e. a path without symbolic links or redundant "." or ".." elements.

On systems that do not have symbolic links this function will always return the same string that absoluteFilePath() returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalPath() returns an empty string.

See also absoluteFilePath() and QString::isNull().

### QString QFileInfo::completeBaseName () const

Returns the complete base name of the file without the path.

The complete base name consists of all characters in the file up to (but not including) the last '.' character.

Example:

        QFileInfo fi("/tmp/archive.tar.gz");
        QString base = fi.completeBaseName();  // base = "archive.tar"

See also fileName(), suffix(), completeSuffix(), and baseName().

### QString QFileInfo::completeSuffix () const

Returns the complete suffix of the file.

The complete suffix consists of all characters in the file after (but not including) the first '.'.

Example:

        QFileInfo fi("/tmp/archive.tar.gz");
        QString ext = fi.completeSuffix();  // ext = "tar.gz"

See also fileName(), suffix(), baseName(), and completeBaseName().

### QDateTime QFileInfo::created () const

Returns the date and time when the file was created.

On platforms where this information is not available, returns the same as lastModified().

See also created(), lastModified(), and lastRead().

### QDir QFileInfo::dir () const

Returns the file's path as a QDir object.

See also dirPath(), filePath(), fileName(), isRelative(), and absoluteDir().

### bool QFileInfo::exists () const

Returns true if the file exists; otherwise returns false.

### QString QFileInfo::fileName () const

Returns the name of the file, excluding the path.

Example:

        QFileInfo fi("/tmp/archive.tar.gz");
        QString name = fi.fileName();                // name = "archive.tar.gz"

See also isRelative(), filePath(), baseName(), and extension().

### QString QFileInfo::filePath () const

Returns the file name, including the path (which may be absolute or relative).

See also isRelative() and absoluteFilePath().

### QString QFileInfo::group () const

Returns the group of the file. On Windows, on systems where files do not have groups, or if an error occurs, an empty string is returned.

This function can be time consuming under Unix (in the order of milliseconds).

See also groupId(), owner(), and ownerId().

### uint QFileInfo::groupId () const

Returns the id of the group the file belongs to.

On Windows and on systems where files do not have groups this function always returns (uint) -2.

See also group(), owner(), and ownerId().

### bool QFileInfo::isAbsolute () const

Returns true if the file path name is absolute, otherwise returns false if the path is relative.

See also isRelative().

### bool QFileInfo::isDir () const

Returns true if this object points to a directory or to a symbolic link to a directory; otherwise returns false.

See also isFile() and isSymLink().

### bool QFileInfo::isExecutable () const

Returns true if the file is executable; otherwise returns false.

See also isReadable(), isWritable(), and permission().

### bool QFileInfo::isFile () const

Returns true if this object points to a file. Returns false if the object points to something which isn't a file, e.g. a directory or a symlink.

See also isDir() and isSymLink().

### bool QFileInfo::isHidden () const

Returns true if this is a `hidden' file; otherwise returns false.

### bool QFileInfo::isReadable () const

Returns true if the user can read the file; otherwise returns false.

See also isWritable(), isExecutable(), and permission().

### bool QFileInfo::isRelative () const

Returns true if the file path name is relative, otherwise returns false if the path is absolute (e.g. under Unix a path is absolute if it begins with a "/").

See also isAbsolute().

### bool QFileInfo::isRoot () const

Returns true if the object points to a directory or to a symbolic link to a directory, and that directory is the root directory; otherwise returns false.

### bool QFileInfo::isSymLink () const

Returns true if this object points to a symbolic link (or to a shortcut on Windows); otherwise returns false.

See also isFile(), isDir(), and readLink().

### bool QFileInfo::isWritable () const

Returns true if the user can write to the file; otherwise returns false.

See also isReadable(), isExecutable(), and permission().

### QDateTime QFileInfo::lastModified () const

Returns the date and time when the file was last modified.

See also created(), lastModified(), and lastRead().

### QDateTime QFileInfo::lastRead () const

Returns the date and time when the file was last read (accessed).

On platforms where this information is not available, returns the same as lastModified().

See also created(), lastModified(), and lastRead().

### bool QFileInfo::makeAbsolute ()

Converts the file's path to an absolute path.

If it is already absolute, nothing is done.

See also filePath() and isRelative().

### QString QFileInfo::owner () const

Returns the owner of the file. On systems where files do not have owners, or if an error occurs, an empty string is returned.

This function can be time consuming under Unix (in the order of milliseconds).

See also ownerId(), group(), and groupId().

### uint QFileInfo::ownerId () const

Returns the id of the owner of the file.

On Windows and on systems where files do not have owners this function returns ((uint) -2).

See also owner(), group(), and groupId().

### QString QFileInfo::path () const

Returns the file's path.

y

See also dir(), filePath(), fileName(), isRelative(), and absolutePath().

### bool QFileInfo::permission ( uint permissionSpec ) const

Tests for file permissions. The permissionSpec argument can be several flags of type `QFile::PermissionSpec` OR-ed together to check for permission combinations.

On systems where files do not have permissions this function always returns true.

Example:

        QFileInfo fi("/tmp/archive.tar.gz");
        if (fi.permission(QFile::WriteUser | QFile::ReadGroup))
            qWarning("I can change the file; my group can read the file");
        if (fi.permission(QFile::WriteGroup | QFile::WriteOther))
            qWarning("The group or others can change the file");

See also isReadable(), isWritable(), isExecutable(), and QFile::PermissionSpec.

### uint QFileInfo::permissions () const

Returns the complete OR-ed together combination of QFile::PermissionSpec for the file.

See also QFileInfo::permission() and QFile::PermissionSpec.

### QString QFileInfo::readLink () const

Returns the name a symlink (or shortcut on Windows) points to, or a an empty string if the object isn't a symbolic link.

This name may not represent an existing file; it is only a string. QFileInfo::exists() returns true if the symlink points to an existing file.

See also exists(), isSymLink(), isDir(), and isFile().

### void QFileInfo::refresh ()

Refreshes the information about the file, i.e. reads in information from the file system the next time a cached property is fetched.

### void QFileInfo::setCaching ( bool enable )

If enable is TRUE, enables caching of file information. If enable is FALSE caching is disabled.

When caching is enabled, QFileInfo reads the file information from the file system the first time it's needed, but generally not later.

Caching is enabled by default.

See also refresh() and caching().

### void QFileInfo::setFile ( const QString & file )

Sets the file that the QFileInfo provides information about to file.

The file can also include an absolute or relative file path. Absolute paths begin with the directory separator (e.g. "/" under Unix) or a drive specification (under Windows). Relative file names begin with a directory name or a file name and specify a path relative to the current directory.

Example:

        QString absolute = "/local/bin";
        QString relative = "local/bin";
        QFileInfo absFile(absolute);
        QFileInfo relFile(relative);

        QDir::setCurrent(QDir::rootPath());
        // absFile and relFile now point to the same file

        QDir::setCurrent("/tmp");
        // absFile now points to "/local/bin",
        // while relFile points to "/tmp/local/bin"

See also isRelative(), QDir::setCurrent(), and QDir::isRelativePath().

### void QFileInfo::setFile ( const QFile & file )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the file that the QFileInfo provides information about to file.

If file includes a relative path, the QFileInfo will also have a relative path.

See also isRelative().

### void QFileInfo::setFile ( const QDir & dir, const QString & file )

This is an overloaded member function, provided for convenience. It behaves essentially like the above function.

Sets the file that the QFileInfo provides information about to file in directory dir.

If file includes a relative path, the QFileInfo will also have a relative path.

See also isRelative().

### QIODevice::Offset QFileInfo::size () const

Returns the file size in bytes, or 0 if the file does not exist or if the size is 0 or if the size cannot be fetched.

### QString QFileInfo::suffix () const

Returns the suffix of the file.

The suffix consists of all characters in the file after (but not including) the last '.'.

Example:

        QFileInfo fi("/tmp/archive.tar.gz");
        QString ext = fi.suffix();  // ext = "gz"

See also fileName(), completeSuffix(), baseName(), and completeBaseName().

### QFileInfo & QFileInfo::operator= ( const QFileInfo & fileinfo )

Makes a copy of the given fileinfo and assigns it to this QFileInfo.