# QFileInfo Class Reference

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

`#include <qfileinfo.h>`

List of all member functions.

## Public Members

- QFileInfo ()
- QFileInfo ( const char * file )
- QFileInfo ( const QFile & )
- QFileInfo ( const QDir &, const char * fileName )
- QFileInfo ( const QFileInfo & )
- ~QFileInfo ()
- QFileInfo& operator= ( const QFileInfo & )
- void setFile ( const char * file )
- void setFile ( const QFile & )
- void setFile ( const QDir &, const char * fileName )
- bool exists () const
- void refresh () const
- bool caching () const
- void setCaching ( bool )
- const char* filePath () const
- QString fileName () const
- QString absFilePath () const
- QString baseName () const
- QString extension () const
- QString dirPath ( bool absPath = FALSE ) const
- QDir dir ( bool absPath = FALSE ) const
- bool isReadable () const
- bool isWritable () const
- bool isExecutable () const
- bool isRelative () const
- bool convertToAbs ()
- bool isFile () const
- bool isDir () const
- bool isSymLink () const
- QString readLink () const
- const char* owner () const
- uint ownerId () const
- const char* group () const
- uint groupId () const
- bool permission ( int permissionSpec ) const
- uint size () const
- QDateTime lastModified () const
- QDateTime lastRead () const

## 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 a symbolic link. Its size and last modified/read times are also available.

To speed up performance QFileInfo caches information about the file. Since 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 would rather like a QFileInfo to access the file system every time you request information from it, you can call the function setCaching( FALSE ).

A QFileInfo can point to a file using either a relative or an absolute file path. Absolute file paths begin with the directory separator ('/') or a drive specification (not applicable to UNIX). Relative file names begin with a directory name or a file name and specify a path relative to the current 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 if a QFileInfo is using a relative or an absolute file path. You can call the function convertToAbs() to convert a relative QFileInfo to an absolute one.

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

Examples: dirview/dirview.cpp biff/biff.cpp

## Member Function Documentation

### QFileInfo::QFileInfo ()

Constructs a new empty QFileInfo.

### QFileInfo::QFileInfo ( const QDir & d, const char * fileName )

Constructs a new QFileInfo that gives information about the file named fileName in the directory d.

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

See also: isRelative().

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

Constructs a new QFileInfo that gives information about file.

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

See also: isRelative().

### QFileInfo::QFileInfo ( const QFileInfo & fi )

Constructs a new QFileInfo that is a copy of fi.

### QFileInfo::QFileInfo ( const char * file )

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

See also: bool, setFile(const, char*), isRelative(), QDir::setCurrent() and QDir::isRelativePath().

### QFileInfo::~QFileInfo ()

Destroys the QFileInfo.

### QString QFileInfo::absFilePath () const

Returns the absolute path name.

The absolute path name is the file name including the absolute path. If the QFileInfo is absolute (i.e. not relative) this function will return the same string as filePath().

Note that this function can be time-consuming under UNIX. (in the order of milliseconds on a 486 DX2/66 running Linux).

See also: isRelative() and filePath().

Examples: biff/biff.cpp

### QString QFileInfo::baseName () const

Returns the base name of the file.

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

Example:

         QFileInfo fi( "/tmp/abdomen.lower" );
         QString base = fi.baseName();              // base = "abdomen"

See also: fileName() and extension().

### bool QFileInfo::caching () const

Returns TRUE if caching is enabled.

See also: setCaching() and refresh().

### bool QFileInfo::convertToAbs ()

Converts the file path name to an absolute path.

If it is already absolute nothing is done.

See also: filePath() and isRelative().

### QDir QFileInfo::dir ( bool absPath = FALSE ) const

Returns the directory path of the file.

If the QFileInfo is relative and absPath is FALSE, the QDir will be relative, otherwise it will be absolute.

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

### QString QFileInfo::dirPath ( bool absPath = FALSE ) const

Returns the directory path of the file.

If absPath is TRUE an absolute path is always returned.

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

### bool QFileInfo::exists () const

Returns TRUE if the file pointed to exists, otherwise FALSE.

Examples: biff/biff.cpp

### QString QFileInfo::extension () const

Returns the extension name of the file.

The file name extension consists of all characters in the file name after (but not including) the first '.' character.

For a file named "archive.tar.gz" this function will return "tar.gz".

Example:

         QFileInfo fi( "/tmp/abdomen.lower" );
         QString ext = fi.extension();              // ext = "lower"

See also: fileName() and baseName().

### QString QFileInfo::fileName () const

Returns the name of the file, the file path is not included.

Example:

         QFileInfo fi( "/tmp/abdomen.lower" );
         QString name = fi.fileName();              // name = "abdomen.lower"

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

Examples: dirview/dirview.cpp

### const char * QFileInfo::filePath () const

Returns the name, i.e. the file name including the path (which can be absolute or relative).

See also: isRelative() and absFilePath().

### const char * QFileInfo::group () const

Returns the group the file belongs to.

On systems where files do not have groups this function always returns 0.

Note that this function can be time-consuming under UNIX (in the order of milliseconds on a 486 DX2/66 running Linux).

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

### uint QFileInfo::groupId () const

Returns the id of the group the file belongs to.

On systems where files do not have groups this function always returns ((uind) -2).

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

### bool QFileInfo::isDir () const

Returns TRUE if we are pointing to a directory or a symbolic link to a directory.

See also: isFile() and isSymLink().

Examples: dirview/dirview.cpp

### bool QFileInfo::isExecutable () const

Returns TRUE if the file is executable.

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

### bool QFileInfo::isFile () const

Returns TRUE if we are pointing to a file or a symbolic link to a file.

See also: isDir() and isSymLink().

Examples: dirview/dirview.cpp

### bool QFileInfo::isReadable () const

Returns TRUE if the file is readable.

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

### bool QFileInfo::isRelative () const

Returns TRUE if the file path name is relative to the current directory, FALSE if the path is absolute (e.g. under UNIX a path is relative if it does not start with a '/').

According to Einstein this function should always return TRUE.

### bool QFileInfo::isSymLink () const

Returns TRUE if we are pointing to a symbolic link.

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

Examples: dirview/dirview.cpp

### bool QFileInfo::isWritable () const

Returns TRUE if the file is writable.

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

### QDateTime QFileInfo::lastModified () const

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

See also: lastRead().

Examples: biff/biff.cpp

### QDateTime QFileInfo::lastRead () const

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

On systems that do not support last read times, the modification time is returned.

See also: lastModified().

Examples: biff/biff.cpp

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

Makes a copy of fi and assigns it to this QFileInfo.

### const char * QFileInfo::owner () const

Returns the owner of the file.

On systems where files do not have owners this function returns 0.

Note that this function can be time-consuming under UNIX. (in the order of milliseconds on a 486 DX2/66 running Linux).

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

### uint QFileInfo::ownerId () const

Returns the id of the owner of the file.

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

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

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

Tests for file permissions. The permissionSpec argument can be several flags of type 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/tonsils" );
        if ( fi.permission( QFileInfo::WriteUser | QFileInfo::ReadGroup ) )
            warning( "Tonsils can be changed by me, and the group can read them.");
        if ( fi.permission( QFileInfo::WriteGroup | QFileInfo::WriteOther ) )
            warning( "Danger! Tonsils can be changed by the group or others!" );

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

### QString QFileInfo::readLink () const

Returns the name a symlink points to, or a null QString if the object does not refer to 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 () const

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

See also: setCaching().

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

Enables caching of file information if enable is TRUE, or disables it if enable is FALSE.

When caching is enabled, QFileInfo reads the file information the first time

Caching is enabled by default.

See also: refresh() and caching().

### void QFileInfo::setFile ( const QDir & d, const char * fileName )

Sets the file to obains information about to fileName in the directory d.

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

See also: isRelative().

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

Sets the file to obtain information about.

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

See also: isRelative().

### void QFileInfo::setFile ( const char * file )

Sets the file to obtain information about.

The string given can be an absolute or a relative file path. Absolute file paths begin with the directory separator (e.g. '/' under UNIX) or a drive specification (not applicable to UNIX). Relative file names begin with a directory name or a file name and specify a path relative to the current directory.

Example:

        #include <qfileinfo.h>
        #include <qdir.h>

        void test()
        {
            const char *absolute = "/liver/aorta";
            const char *relative = "liver/aorta";
            QFileInfo fi1( absolute );
            QFileInfo fi2( relative );

            QDir::setCurrent( QDir::rootDirPath() );
                                    // fi1 and fi2 now point to the same file

            QDir::setCurrent( "/tmp" );
                                    // fi1 now points to "/liver/aorta",
                                    // while fi2 points to "/tmp/liver/aorta"
        }

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

Examples: biff/biff.cpp

### uint QFileInfo::size () const

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

Search the documentation, FAQ, qt-interest archive and more (uses www.troll.no):

This file is part of the Qt toolkit, copyright © 1995-99 Troll Tech, all rights reserved.

It was generated from the following files:

- qfileinfo.h: 1998/08/19
- qfileinfo.cpp: 1998/07/03