Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions |
The TQDir class provides access to directory structures and their contents in a platform-independent way. More...
All the functions in this class are reentrant when TQt is built with thread support.
#include <qdir.h>
A TQDir is used to manipulate path names, access information regarding paths and files, and manipulate the underlying file system.
A TQDir can point to a file using either a relative or an absolute path. Absolute paths begin with the directory separator "/" (optionally preceded by a drive specification under Windows). If you always use "/" as a directory separator, TQt will translate your paths to conform to the underlying operating system. Relative file names begin with a directory name or a file name and specify a path relative to the current directory.
The "current" path refers to the application's working directory. A TQDir's own path is set and retrieved with setPath() and path().
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 TQDir is using a relative or an absolute file path. Call convertToAbs() to convert a relative TQDir to an absolute one. For a simplified path use cleanDirPath(). To obtain a path which has no symbolic links or redundant ".." elements use canonicalPath(). The path can be set with setPath(), and changed with cd() and cdUp().
TQDir provides several static functions, for example, setCurrent() to set the application's working directory and currentDirPath() to retrieve the application's working directory. Access to some common paths is provided with the static functions, current(), home() and root() which return TQDir objects or currentDirPath(), homeDirPath() and rootDirPath() which return the path as a string. If you want to know about your application's path use TQApplication::applicationDirPath().
The number of entries in a directory is returned by count(). Obtain a string list of the names of all the files and directories in a directory with entryList(). If you prefer a list of TQFileInfo pointers use entryInfoList(). Both these functions can apply a name filter, an attributes filter (e.g. read-only, files not directories, etc.), and a sort order. The filters and sort may be set with calls to setNameFilter(), setFilter() and setSorting(). They may also be specified in the entryList() and entryInfoList()'s arguments.
Create a new directory with mkdir(), rename a directory with rename() and remove an existing directory with rmdir(). Remove a file with remove(). You can interrogate a directory with exists(), isReadable() and isRoot().
To get a path with a filename use filePath(), and to get a directory name use dirName(); neither of these functions checks for the existence of the file or directory.
The list of root directories is provided by drives(); on Unix systems this returns a list containing one root directory, "/"; on Windows the list will usually contain "C:/", and possibly "D:/", etc.
It is easiest to work with "/" separators in TQt code. If you need to present a path to the user or need a path in a form suitable for a function in the underlying operating system use convertSeparators().
Examples:
See if a directory exists.
TQDir d( "example" ); // "./example" if ( !d.exists() ) qWarning( "Cannot find the example directory" );
Traversing directories and reading a file.
TQDir d = TQDir::root(); // "/" if ( !d.cd("tmp") ) { // "/tmp" qWarning( "Cannot find the \"/tmp\" directory" ); } else { TQFile f( d.filePath("ex1.txt") ); // "/tmp/ex1.txt" if ( !f.open(IO_ReadWrite) ) qWarning( "Cannot create the file %s", f.name() ); }
A program that lists all the files in the current directory (excluding symbolic links), sorted by size, smallest first:
#include <stdio.h> #include <qdir.h> int main( int argc, char **argv ) { TQDir d; d.setFilter( TQDir::Files | TQDir::Hidden | TQDir::NoSymLinks ); d.setSorting( TQDir::Size | TQDir::Reversed ); const TQFileInfoList *list = d.entryInfoList(); TQFileInfoListIterator it( *list ); TQFileInfo *fi; printf( " Bytes Filename\n" ); while ( (fi = it.current()) != 0 ) { printf( "%10li %s\n", fi->size(), fi->fileName().latin1() ); ++it; } return 0; }
See also TQApplication::applicationDirPath() and Input/Output and Networking.
This enum describes the filtering options available to TQDir, e.g. for entryList() and entryInfoList(). The filter value is specified by OR-ing together values from the following list:
If you do not set any of Readable, Writable or Executable, TQDir will set all three of them. This makes the default easy to write and at the same time useful.
Examples: Readable|Writable means list all files for which the application has read access, write access or both. Dirs|Drives means list drives, directories, all files that the application can read, write or execute, and also symlinks to such files/directories.
This enum describes the sort options available to TQDir, e.g. for entryList() and entryInfoList(). The sort value is specified by OR-ing together values from the following list:
You can only specify one of the first four.
If you specify both DirsFirst and Reversed, directories are still put first, but in reverse order; the files will be listed after the directories, again in reverse order.
See also currentDirPath().
The default nameFilter is an empty string, which excludes nothing; the default filterSpec is All, which also means exclude nothing. The default sortSpec is Name|IgnoreCase, i.e. sort by name case-insensitively.
Example that lists all the files in "/tmp":
TQDir d( "/tmp" ); for ( int i = 0; i < d.count(); i++ ) printf( "%s\n", d[i] );
If path is "" or TQString::null, TQDir uses "." (the current directory). If nameFilter is "" or TQString::null, TQDir uses the name filter "*" (all files).
Note that path need not exist.
See also exists(), setPath(), setNameFilter(), setFilter(), and setSorting().
See also operator=().
If acceptAbsPath is TRUE a fileName starting with a separator "/" will be returned without change. If acceptAbsPath is FALSE an absolute path will be prepended to the fileName and the resultant string returned.
See also filePath().
See also setPath(), canonicalPath(), exists(), cleanDirPath(), dirName(), and absFilePath().
Example: fileiconview/qfileiconview.cpp.
On systems that do not have symbolic links this function will always return the same string that absPath() returns. If the canonical path does not exist (normally due to dangling symbolic links) canonicalPath() returns TQString::null.
See also path(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath(), and TQString::isNull().
If acceptAbsPath is TRUE a path starting with separator "/" will cause the function to change to the absolute directory. If acceptAbsPath is FALSE any number of separators at the beginning of dirName will be removed and the function will descend into dirName.
Returns TRUE if the new directory exists and is readable; otherwise returns FALSE. Note that the logical cd() operation is not performed if the new directory does not exist.
Calling cd( ".." ) is equivalent to calling cdUp().
See also cdUp(), isReadable(), exists(), and path().
Example: fileiconview/mainwindow.cpp.
Returns TRUE if the new directory exists and is readable; otherwise returns FALSE. Note that the logical cdUp() operation is not performed if the new directory does not exist.
See also cd(), isReadable(), exists(), and path().
Symbolic links are kept. This function does not return the canonical path, but rather the simplest version of the input. For example, "./local" becomes "local", "local/../bin" becomes "bin" and "/local/usr/../bin" becomes "/local/bin".
See also absPath() and canonicalPath().
On Windows, convertSeparators("c:/winnt/system32") returns "c:\winnt\system32".
The returned string may be the same as the argument on some operating systems, for example on Unix.
Examples: dirview/dirview.cpp and fileiconview/qfileiconview.cpp.
See also isRelative().
Equivalent to entryList().count().
See also operator[]() and entryList().
Use path() to access a TQDir object's path.
See also currentDirPath() and TQDir::TQDir().
See also current().
Examples: dirview/dirview.cpp, helpviewer/helpwindow.cpp, and qdir/qdir.cpp.
No check is made to ensure that a directory with this name actually exists.
See also path(), absPath(), absFilePath(), exists(), and TQString::isNull().
The returned pointer is owned by TQt. Callers should not delete or modify it.
Example: dirview/main.cpp.
This function is included to easy porting from TQt 1.x to TQt 2.0, it is the same as entryList(), but encodes the filenames as 8-bit strings using TQFile::encodedName().
It is more efficient to use entryList().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
This function is included to easy porting from TQt 1.x to TQt 2.0, it is the same as entryList(), but encodes the filenames as 8-bit strings using TQFile::encodedName().
It is more efficient to use entryList().
The filter and sorting specifications can be overridden using the nameFilter, filterSpec and sortSpec arguments.
Returns 0 if the directory is unreadable or does not exist.
The returned pointer is a const pointer to a TQFileInfoList. The list is owned by the TQDir object and will be reused on the next call to entryInfoList() for the same TQDir instance. If you want to keep the entries of the list after a subsequent call to this function you must copy them.
Note: TQFileInfoList is really a TQPtrList See also entryList(), setNameFilter(), setSorting(), and setFilter().
Examples: dirview/dirview.cpp and fileiconview/qfileiconview.cpp.
Returns a list of TQFileInfo objects for all the files and
directories in the directory, ordered in accordance with
setSorting() and filtered in accordance with setFilter() and
setNameFilter().
The filter and sorting specifications can be overridden using the
filterSpec and sortSpec arguments.
Returns 0 if the directory is unreadable or does not exist.
The returned pointer is a const pointer to a TQFileInfoList. The
list is owned by the TQDir object and will be reused on the next
call to entryInfoList() for the same TQDir instance. If you want to
keep the entries of the list after a subsequent call to this
function you must copy them.
Note: TQFileInfoList is really a TQPtrList See also entryList(), setNameFilter(), setSorting(), and setFilter().
The filter and sorting specifications can be overridden using the
nameFilter, filterSpec and sortSpec arguments.
Returns an empty list if the directory is unreadable or does not
exist.
See also entryInfoList(), setNameFilter(), setSorting(), and setFilter().
Example: table/statistics/statistics.cpp.
Returns a list of the names of all the files and directories in
the directory, ordered in accordance with setSorting() and
filtered in accordance with setFilter() and setNameFilter().
The filter and sorting specifications can be overridden using the
filterSpec and sortSpec arguments.
Returns an empty list if the directory is unreadable or does not
exist.
See also entryInfoList(), setNameFilter(), setSorting(), and setFilter().
If acceptAbsPath is TRUE a path starting with separator "/"
will check the file with the absolute path. If acceptAbsPath is
FALSE any number of separators at the beginning of name will be
removed and the resultant file name will be checked.
Returns TRUE if the file exists; otherwise returns FALSE.
See also TQFileInfo::exists() and TQFile::exists().
Returns TRUE if the directory exists; otherwise returns FALSE.
(If a file with the same name is found this function will return
FALSE).
See also TQFileInfo::exists() and TQFile::exists().
If acceptAbsPath is TRUE a fileName starting with a
separator "/" will be returned without change. If acceptAbsPath
is FALSE an absolute path will be prepended to the fileName and
the resultant string returned.
See also absFilePath(), isRelative(), and canonicalPath().
Returns the value set by setFilter()
Under Windows the HOME environment variable is used. If this
does not exist the USERPROFILE environment variable is used. If
that does not exist the path is formed by concatenating the HOMEDRIVE and HOMEPATH environment variables. If they don't
exist the rootDirPath() is used (this uses the SystemDrive
environment variable). If none of these exist "C:\" is used.
Under non-Windows operating systems the HOME environment
variable is used if it exists, otherwise rootDirPath() is used.
See also homeDirPath().
Returns the absolute path of the user's home directory.
See also home().
Warning: A FALSE value from this function is not a guarantee that
files in the directory are not accessible.
See also TQFileInfo::isReadable().
Examples: dirview/dirview.cpp and fileiconview/qfileiconview.cpp.
See also convertToAbs().
See also isRelative().
Note: If the directory is a symbolic link to the root directory
this function returns FALSE. If you want to test for this use
canonicalPath(), e.g.
See also root() and rootDirPath().
(See TQRegExp wildcard
matching.)
See also TQRegExp::match().
Returns TRUE if the fileName matches any of the wildcard (glob)
patterns in the list of filters; otherwise returns FALSE.
(See TQRegExp wildcard
matching.)
See also TQRegExp::match().
Returns the value set by setMatchAllDirs()
See also setMatchAllDirs().
If acceptAbsPath is TRUE a path starting with a separator ('/')
will create the absolute directory; if acceptAbsPath is FALSE
any number of separators at the beginning of dirName will be
removed.
Returns TRUE if successful; otherwise returns FALSE.
See also rmdir().
Returns the string set by setNameFilter()
Returns TRUE if directory d and this directory have different
paths or different sort or filter settings; otherwise returns
FALSE.
Example:
Sets the directory path to be the given path.
Example:
Returns a TQString::null if the index is out of range or if the
entryList() function failed.
See also count() and entryList().
Returns the path, this may contain symbolic links, but never
contains redundant ".", ".." or multiple separators.
The returned path can be either absolute or relative (see
setPath()).
See also setPath(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath(), and convertSeparators().
If acceptAbsPath is TRUE a path starting with separator "/"
will remove the file with the absolute path. If acceptAbsPath
is FALSE any number of separators at the beginning of fileName
will be removed and the resultant file name will be removed.
Returns TRUE if the file is removed successfully; otherwise
returns FALSE.
If acceptAbsPaths is TRUE a path starting with a separator
('/') will rename the file with the absolute path; if acceptAbsPaths is FALSE any number of separators at the beginning
of the names will be removed.
Returns TRUE if successful; otherwise returns FALSE.
On most file systems, rename() fails only if oldName does not
exist or if newName and oldName are not on the same
partition. On Windows, rename() will fail if newName already
exists. However, there are also other reasons why rename() can
fail. For example, on at least one file system rename() fails if
newName points to an open file.
Example: fileiconview/qfileiconview.cpp.
If acceptAbsPath is TRUE a path starting with a separator ('/')
will remove the absolute directory; if acceptAbsPath is FALSE
any number of separators at the beginning of dirName will be
removed.
The directory must be empty for rmdir() to succeed.
Returns TRUE if successful; otherwise returns FALSE.
See also mkdir().
See also rootDirPath() and drives().
For UNIX operating systems this returns "/". For Windows file
systems this normally returns "c:/".
You do not need to use this function to build file paths. If you
always use "/", TQt will translate your paths to conform to the
underlying operating system.
See also filter() and setNameFilter().
See also matchAllDirs().
The nameFilter is a wildcard (globbing) filter that understands
"*" and "?" wildcards. (See TQRegExp wildcard matching.) You may specify several filter
entries all separated by a single space " " or by a semi-colon
";".
For example, if you want entryList() and entryInfoList() to list
all files ending with either ".cpp" or ".h", you would use either
dir.setNameFilter("*.cpp *.h") or dir.setNameFilter("*.cpp;*.h").
See also nameFilter() and setFilter().
The path can be either absolute or relative. Absolute paths begin
with the directory separator "/" (optionally preceded by 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. An example of an absolute path is the string
"/tmp/quartz", a relative path might look like "src/fatlib".
See also path(), absPath(), exists(), cleanDirPath(), dirName(), absFilePath(), isRelative(), and convertToAbs().
The sortSpec is specified by OR-ing values from the enum
TQDir::SortSpec.
See also sorting() and SortSpec.
Returns the value set by setSorting()
See also setSorting() and SortSpec.
This file is part of the TQt toolkit.
Copyright © 1995-2007
Trolltech. All Rights Reserved.const TQFileInfoList * TQDir::entryInfoList ( int filterSpec = DefaultFilter, int sortSpec = DefaultSort ) const [virtual]
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
TQStringList TQDir::entryList ( const TQString & nameFilter, int filterSpec = DefaultFilter, int sortSpec = DefaultSort ) const [virtual]
Returns a list of the names of all the files and directories in
the directory, ordered in accordance with setSorting() and
filtered in accordance with setFilter() and setNameFilter().
TQStringList TQDir::entryList ( int filterSpec = DefaultFilter, int sortSpec = DefaultSort ) const [virtual]
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
bool TQDir::exists ( const TQString & name, bool acceptAbsPath = TRUE ) [virtual]
Checks for the existence of the file name.
bool TQDir::exists () const [virtual]
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
TQString TQDir::filePath ( const TQString & fileName, bool acceptAbsPath = TRUE ) const [virtual]
Returns the path name of a file in the directory. Does not
check if the file actually exists in the directory. If the TQDir is
relative the returned path name will also be relative. Redundant
multiple separators or "." and ".." directories in fileName
will not be removed (see cleanDirPath()).
FilterSpec TQDir::filter () const
TQDir TQDir::home () [static]
Returns the home directory.
TQString TQDir::homeDirPath () [static]
bool TQDir::isReadable () const [virtual]
Returns TRUE if the directory is readable and we can open files
by name; otherwise returns FALSE.
bool TQDir::isRelative () const [virtual]
Returns TRUE if the directory path is relative to the current
directory and returns FALSE if the path is absolute (e.g. under
UNIX a path is relative if it does not start with a "/").
bool TQDir::isRelativePath ( const TQString & path ) [static]
Returns TRUE if path is relative; returns FALSE if it is
absolute.
bool TQDir::isRoot () const [virtual]
Returns TRUE if the directory is the root directory; otherwise
returns FALSE.
TQDir d( "/tmp/root_link" );
d = d.canonicalPath();
if ( d.isRoot() )
qWarning( "It is a root link" );
bool TQDir::match ( const TQString & filter, const TQString & fileName ) [static]
Returns TRUE if the fileName matches the wildcard (glob)
pattern filter; otherwise returns FALSE. The filter may
contain multiple patterns separated by spaces or semicolons.
bool TQDir::match ( const TQStringList & filters, const TQString & fileName ) [static]
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
bool TQDir::matchAllDirs () const
bool TQDir::mkdir ( const TQString & dirName, bool acceptAbsPath = TRUE ) const [virtual]
Creates a directory.
TQString TQDir::nameFilter () const
bool TQDir::operator!= ( const TQDir & d ) const [virtual]
// The current directory is "/usr/local"
TQDir d1( "/usr/local/bin" );
TQDir d2( "bin" );
if ( d1 != d2 )
qDebug( "They differ" );
TQDir & TQDir::operator= ( const TQDir & d )
Makes a copy of TQDir d and assigns it to this TQDir.
TQDir & TQDir::operator= ( const TQString & path )
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
bool TQDir::operator== ( const TQDir & d ) const [virtual]
Returns TRUE if directory d and this directory have the same
path and their sort and filter settings are the same; otherwise
returns FALSE.
// The current directory is "/usr/local"
TQDir d1( "/usr/local/bin" );
TQDir d2( "bin" );
d2.convertToAbs();
if ( d1 == d2 )
qDebug( "They're the same" );
TQString TQDir::operator[] ( int index ) const
Returns the file name at position index in the list of file
names. Equivalent to entryList().at(index).
TQString TQDir::path () const [virtual]
void TQDir::refresh () const
Refreshes the directory information.
bool TQDir::remove ( const TQString & fileName, bool acceptAbsPath = TRUE ) [virtual]
Removes the file, fileName.
bool TQDir::rename ( const TQString & oldName, const TQString & newName, bool acceptAbsPaths = TRUE ) [virtual]
Renames a file or directory.
bool TQDir::rmdir ( const TQString & dirName, bool acceptAbsPath = TRUE ) const [virtual]
Removes a directory.
TQDir TQDir::root () [static]
Returns the root directory.
TQString TQDir::rootDirPath () [static]
Returns the absolute path for the root directory.
char TQDir::separator () [static]
Returns the native directory separator; "/" under UNIX (including
Mac OS X) and "\" under Windows.
bool TQDir::setCurrent ( const TQString & path ) [static]
Sets the application's current working directory to path.
Returns TRUE if the directory was successfully changed; otherwise
returns FALSE.
void TQDir::setFilter ( int filterSpec ) [virtual]
Sets the filter used by entryList() and entryInfoList() to filterSpec. The filter is used to specify the kind of files that
should be returned by entryList() and entryInfoList(). See
TQDir::FilterSpec.
void TQDir::setMatchAllDirs ( bool enable ) [virtual]
If enable is TRUE then all directories are included (e.g. in
entryList()), and the nameFilter() is only applied to the files.
If enable is FALSE then the nameFilter() is applied to both
directories and files.
void TQDir::setNameFilter ( const TQString & nameFilter ) [virtual]
Sets the name filter used by entryList() and entryInfoList() to nameFilter.
void TQDir::setPath ( const TQString & path ) [virtual]
Sets the path of the directory to path. The path is cleaned of
redundant ".", ".." and of multiple separators. No check is made
to ensure that a directory with this path exists.
void TQDir::setSorting ( int sortSpec ) [virtual]
Sets the sort order used by entryList() and entryInfoList().
SortSpec TQDir::sorting () const
Copyright © 2007
Trolltech Trademarks