dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Auto Numbering Backups

  1. #1
    Join Date
    Jan 2020
    Posts
    1

    Auto Numbering Backups

    I have a radio Amateur's program for logging I am working on. I can get it to create the backup with date and time to keep them unique, But cant find any example of consecutively numbering the backups.

    I would like the output to be "County Hunter - K8EMS - Bak1.accdb
    County Hunter - K8EMS - Bak2.accdb and on and on

    can someone point me to an example.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,208

    Re: Auto Numbering Backups

    If you want sequentially numbered backups (i.e. 1, 2, etc.), then you'll need to store the last number so you can increment it on the next back up. You can store this in a app.config (or appsetting.json if using .net core) file or in the registry.

    Another approach is to date stamp the file.

    Not a vb.net expert but in C#, it would be something like:
    Code:
    var backupFileName = $"County Hunter - K8EMS - Bak{DateTime.UtcNow.GetString("yyyyMMdd")}.accdb";
    This would produce:
    Code:
    County Hunter - K8EMS - Bak20200115.accdb
    Note: this uses string interpolation and I used UTC time. Call DateTime.Now for local time.

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,972

    Re: Auto Numbering Backups

    But cant find any example of consecutively numbering the backups.
    I can't give an example as I'm C++, but one way to find the next number to use is to iterate the folder in which the files are stored, find the file name with the highest number and then create the new file name with this number + 1. Note that depending upon how many files there are in the folder, this may have a performance impact due to the time to iterate the folder. However it does mean that you don't have to store the last number used somewhere etc.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2019 (16.4.2)

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,208

    Re: Auto Numbering Backups

    Quote Originally Posted by 2kaud View Post
    I can't give an example as I'm C++, but one way to find the next number to use is to iterate the folder in which the files are stored, find the file name with the highest number and then create the new file name with this number + 1. Note that depending upon how many files there are in the folder, this may have a performance impact due to the time to iterate the folder. However it does mean that you don't have to store the last number used somewhere etc.
    It takes less time to interate a folder than one might think, but this approach assumes the user doesn't move the files from the backup location.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)