QGIS API Documentation  3.14.0-Pi (9f7028fd23)
Static Public Member Functions | List of all members
QgsFileUtils Class Reference

Class for file utilities. More...

#include <qgsfileutils.h>

Static Public Member Functions

static QString addExtensionFromFilter (const QString &fileName, const QString &filter)
 Ensures that a fileName ends with an extension from the specified filter string. More...
 
static QString ensureFileNameHasExtension (const QString &fileName, const QStringList &extensions)
 Ensures that a fileName ends with an extension from the provided list of extensions. More...
 
static QStringList extensionsFromFilter (const QString &filter)
 Returns a list of the extensions contained within a file filter string. More...
 
static QString findClosestExistingPath (const QString &path)
 Returns the top-most existing folder from path. More...
 
static QStringList findFile (const QString &file, const QString &basepath=QString(), int maxClimbs=4, int searchCeiling=4, const QString &currentDir=QString())
 Will check basepath in an outward spiral up to maxClimbs levels to check if file exists. More...
 
static QString representFileSize (qint64 bytes)
 Returns the human size from bytes. More...
 
static QString stringToSafeFilename (const QString &string)
 Converts a string to a safe filename, replacing characters which are not safe for filenames with an '_' character. More...
 

Detailed Description

Class for file utilities.

Since
QGIS 3.0

Definition at line 30 of file qgsfileutils.h.

Member Function Documentation

◆ addExtensionFromFilter()

QString QgsFileUtils::addExtensionFromFilter ( const QString &  fileName,
const QString &  filter 
)
static

Ensures that a fileName ends with an extension from the specified filter string.

E.g. a fileName of "d:/my_file" with a filter of "GeoTIFF Files (*.tiff *.tif)" will return "d:/my_file.tif", where as fileName of "d:/my_file.TIFF" or "d:/my_file.TIF" will be returned unchanged.

See also
extensionsFromFilter()
ensureFileNameHasExtension()

Definition at line 86 of file qgsfileutils.cpp.

◆ ensureFileNameHasExtension()

QString QgsFileUtils::ensureFileNameHasExtension ( const QString &  fileName,
const QStringList &  extensions 
)
static

Ensures that a fileName ends with an extension from the provided list of extensions.

E.g. if extensions contains "tif" and "tiff", then a fileName of "d:/my_file" will return "d:/my_file.tif". A fileName of "d:/my_file.TIFF" or "d:/my_file.TIF" will be returned unchanged.

See also
extensionsFromFilter()
addExtensionFromFilter()

Definition at line 59 of file qgsfileutils.cpp.

◆ extensionsFromFilter()

QStringList QgsFileUtils::extensionsFromFilter ( const QString &  filter)
static

Returns a list of the extensions contained within a file filter string.

E.g. a filter of "GeoTIFF Files (*.tiff *.tif)" would return a list containing "tiff", "tif". The initial '.' is stripped off the extension.

See also
ensureFileNameHasExtension()
addExtensionFromFilter()

Definition at line 40 of file qgsfileutils.cpp.

◆ findClosestExistingPath()

QString QgsFileUtils::findClosestExistingPath ( const QString &  path)
static

Returns the top-most existing folder from path.

E.g. if path is "/home/user/projects/2018/P4343" and "/home/user/projects" exists but no "2018" subfolder exists, then the function will return "/home/user/projects".

Since
QGIS 3.2

Definition at line 100 of file qgsfileutils.cpp.

◆ findFile()

QStringList QgsFileUtils::findFile ( const QString &  file,
const QString &  basepath = QString(),
int  maxClimbs = 4,
int  searchCeiling = 4,
const QString &  currentDir = QString() 
)
static

Will check basepath in an outward spiral up to maxClimbs levels to check if file exists.

Parameters
fileName or full path of the file to find
basepathcurrent basepath of the file, needed if only the name is specified in file
maxClimbslimit the number of time the search can move up from the basepath
searchCeilinglimits where in the folder hierarchy the search can be performed, 1 = root/drive, 2 = first folder level, 3 = sub folders ( Unix: /usr/bin, Win: C:/Users/Admin ), etc.
currentDiralternative default directory to override the actual current directory during the search
Returns
List of strings of the first matching path in unix format.
Since
QGIS 3.12

Definition at line 133 of file qgsfileutils.cpp.

◆ representFileSize()

QString QgsFileUtils::representFileSize ( qint64  bytes)
static

Returns the human size from bytes.

Definition at line 24 of file qgsfileutils.cpp.

◆ stringToSafeFilename()

QString QgsFileUtils::stringToSafeFilename ( const QString &  string)
static

Converts a string to a safe filename, replacing characters which are not safe for filenames with an '_' character.

Warning
This method strips slashes from the filename, so it is safe to call with file names only, not complete paths.

Definition at line 92 of file qgsfileutils.cpp.


The documentation for this class was generated from the following files: