Results 1 to 3 of 3

Thread: How to Create file but if name exists add incremental number to file name

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2021
    Posts
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to Create file but if name exists add incremental number to file name

    You can check if the file exists using QFile::exists(filename) and if yes, you can add to the new file a new name.
    Qt Code:
    1. QString new_file(“Qtfile.txt);
    2. if(QFile::exists(new_file) {
    3. new_file = “Qtfile.txt1.txt);
    4. }
    To copy to clipboard, switch view to plain text mode 
    And then create the file
    Last edited by d_stranz; 26th March 2021 at 15:32. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,327
    Thanks
    317
    Thanked 871 Times in 858 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to Create file but if name exists add incremental number to file name

    Here is a somewhat more complete way to do this with any file, which will create a new filename with a number suffix that is one greater than the suffix of the name that was passed in. This code is not tested, so there may be "off-by-one" errors in computing string lengths and positions. It also does not correctly handle the case where a filename has no extension but end in a ".". That is easy to fix but it is a good exercise to find where to do it.

    Qt Code:
    1. QString testAndIncrementFilename( const QString & testName )
    2. (
    3. QFileInfo fInfo( testName ); // Use QFileInfo to test and manipulate filename
    4. if ( !fInfo.exists() ) // File does not exist, so use it
    5. return testName;
    6.  
    7. // Retrieve the extension
    8. QString ext = fileInfo.suffix();
    9.  
    10. // and the left-hand side of the path
    11. QString path;
    12. if ( ext.isEmpty() ) // if there is no extension, then the left side is the whole name
    13. path = testName;
    14. else // otherwise it is everything left of the "."
    15. path = testName.left( testName.lastIndexof( '.' ) );
    16.  
    17. // Now, start at the right end and look for numeric digits
    18. QString digits;
    19. auto pos = path.length() - 1;
    20. while ( pos > 0 && ( path[ pos ] ).isDigit() )
    21. {
    22. // Since we are reading in reverse order, we need to push front instead of back
    23. digits.push_front( path[ pos ] );
    24. pos--;
    25. }
    26.  
    27. // Save the rest of the path minus the digits for building the new path
    28. QString newPath = path.left( pos );
    29.  
    30. // Build the new number suffix. Start with 1 as the first increment
    31. int number = 1;
    32. if ( !digits.isEmpty() )
    33. {
    34. // Convert the string to an int and increment it
    35. number = digits.toInt() + 1;
    36. }
    37.  
    38. // Convert it back to a string. Note: if you want something prettier, like "0001", "0002", etc.
    39. // then use a field width and fill character: QString.arg( number, 4, '0' ). If you will have more
    40. // than 10000 files, use a 5-digit field width and consider how badly you are filling up your
    41. // customer's disk drive
    42. digits = QString.arg( number );
    43.  
    44. // Now build the path again
    45. newPath += digits; // add the new number
    46. if ( !ext.isEmpty() ) // if the extensionis not empty add it along with the '.' separator
    47. {
    48. newPath += "."; // add the extension separator
    49. newPath += ext; // add the extension
    50. }
    51.  
    52. // and here's the new filename
    53. return newPath;
    54. )
    To copy to clipboard, switch view to plain text mode 

    I sure hope I didn't just give you the answer to your homework problem...
    Last edited by d_stranz; 26th March 2021 at 15:36.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Appending data, if file exists.
    By rookee in forum Newbie
    Replies: 1
    Last Post: 10th December 2015, 15:11
  2. File exists even if it should have deleted
    By davidlamhauge in forum Qt Programming
    Replies: 1
    Last Post: 27th January 2014, 02:28
  3. File exists on server
    By Chops211 in forum Qt Programming
    Replies: 5
    Last Post: 6th August 2011, 20:22
  4. Replies: 2
    Last Post: 21st February 2011, 14:52
  5. Can't create folder if file with same name exists
    By Barry79 in forum Qt Programming
    Replies: 1
    Last Post: 6th May 2009, 16:33

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.