(
QFileInfo fInfo
( testName
);
// Use QFileInfo to test and manipulate filename if ( !fInfo.exists() ) // File does not exist, so use it
return testName;
// Retrieve the extension
// and the left-hand side of the path
if ( ext.isEmpty() ) // if there is no extension, then the left side is the whole name
path = testName;
else // otherwise it is everything left of the "."
path = testName.left( testName.lastIndexof( '.' ) );
// Now, start at the right end and look for numeric digits
auto pos = path.length() - 1;
while ( pos > 0 && ( path[ pos ] ).isDigit() )
{
// Since we are reading in reverse order, we need to push front instead of back
digits.push_front( path[ pos ] );
pos--;
}
// Save the rest of the path minus the digits for building the new path
QString newPath
= path.
left( pos
);
// Build the new number suffix. Start with 1 as the first increment
int number = 1;
if ( !digits.isEmpty() )
{
// Convert the string to an int and increment it
number = digits.toInt() + 1;
}
// Convert it back to a string. Note: if you want something prettier, like "0001", "0002", etc.
// then use a field width and fill character: QString.arg( number, 4, '0' ). If you will have more
// than 10000 files, use a 5-digit field width and consider how badly you are filling up your
// customer's disk drive
// Now build the path again
newPath += digits; // add the new number
if ( !ext.isEmpty() ) // if the extensionis not empty add it along with the '.' separator
{
newPath += "."; // add the extension separator
newPath += ext; // add the extension
}
// and here's the new filename
return newPath;
)
QString testAndIncrementFilename( const QString & testName )
(
QFileInfo fInfo( testName ); // Use QFileInfo to test and manipulate filename
if ( !fInfo.exists() ) // File does not exist, so use it
return testName;
// Retrieve the extension
QString ext = fileInfo.suffix();
// and the left-hand side of the path
QString path;
if ( ext.isEmpty() ) // if there is no extension, then the left side is the whole name
path = testName;
else // otherwise it is everything left of the "."
path = testName.left( testName.lastIndexof( '.' ) );
// Now, start at the right end and look for numeric digits
QString digits;
auto pos = path.length() - 1;
while ( pos > 0 && ( path[ pos ] ).isDigit() )
{
// Since we are reading in reverse order, we need to push front instead of back
digits.push_front( path[ pos ] );
pos--;
}
// Save the rest of the path minus the digits for building the new path
QString newPath = path.left( pos );
// Build the new number suffix. Start with 1 as the first increment
int number = 1;
if ( !digits.isEmpty() )
{
// Convert the string to an int and increment it
number = digits.toInt() + 1;
}
// Convert it back to a string. Note: if you want something prettier, like "0001", "0002", etc.
// then use a field width and fill character: QString.arg( number, 4, '0' ). If you will have more
// than 10000 files, use a 5-digit field width and consider how badly you are filling up your
// customer's disk drive
digits = QString.arg( number );
// Now build the path again
newPath += digits; // add the new number
if ( !ext.isEmpty() ) // if the extensionis not empty add it along with the '.' separator
{
newPath += "."; // add the extension separator
newPath += ext; // add the extension
}
// and here's the new filename
return newPath;
)
To copy to clipboard, switch view to plain text mode
Bookmarks