Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. File creation and naming increment

File creation and naming increment

Scheduled Pinned Locked Moved C#
questionsysadminhelp
25 Posts 8 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Terick

    Thank you all for your posts. It's been helpful. I'm still a little stuck, but I have the general idea. The files may get deleted, so I will need to take that into account. Also, I'm not using any database and for this application wont be using one. This is what I have so far. Please let me know if there are better methods:

    public void filecount(string file)
    {
    int x;
    int y;
    string file;

    int filepaths = Directory.GetFiles(@"C:\Desktop\Request", "*txt").Length;
    x = filepaths +1;

    StreamWriter sw;
    sw = File.CreateText(@"C:\Desktop\Request\Text" +y +".txt");
    file = Path.GetFileName(@"C:\Desktop\Request\Text" +y +".txt").ToString;
    sw.Close();

    return file;
    }

    public void btn_Click(object sender, EventArgs e)
    {
    filecount(file);
    }

    Thanks again for helping a newbie out!

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #15

    That's how I'd do it:

    string[] logfiles = Directory.GetFiles(@"../Files/", @"Text*.txt");
    Array.Sort(logfiles, CompareFileNumbers);

    int highestNumber = int.Parse(Regex.Match(logfiles[0], @"(\d+)").Value);

    with the custom comparer being:

    public static int CompareFileNumbers(string fileA, string fileB)
    {
    Regex regexNumber = new Regex(@"(\d+)");

    int fileNumberA = int.Parse(regexNumber.Match(fileA).Value);
    int fileNumberB = int.Parse(regexNumber.Match(fileB).Value);
    
    return -fileNumberA.CompareTo(fileNumberB); // the miuns sign will sort descending
    

    }

    Tested and it works. You need to add error checking though in case the filenames don't have numbers (the regexes will fail) and other cases. regards

    1 Reply Last reply
    0
    • E Ennis Ray Lynch Jr

      Incrementing the integer requires the following steps: 1) Find the highest file 2) Attempt to create the next number 3) Repeat on failure A time stamp requires: 1) Attempt to create the file 2) Repeat on failure Unfortunately, only the Win32 API allows you to do this without a try catch loop which I find poor practice.

      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
      If you don't ask questions the answers won't stand in your way.
      Most of this sig is for Google, not ego.

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #16

      You can check to see if a file with that name already exists.

      E 1 Reply Last reply
      0
      • P PIEBALDconsult

        You can check to see if a file with that name already exists.

        E Offline
        E Offline
        Ennis Ray Lynch Jr
        wrote on last edited by
        #17

        Doesn't work in concurrent systems as the check for existence and creation have to be the same act, otherwise File.Exists then File.Create can fail.

        Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
        If you don't ask questions the answers won't stand in your way.
        Most of this sig is for Google, not ego.

        P 1 Reply Last reply
        0
        • E Ennis Ray Lynch Jr

          Doesn't work in concurrent systems as the check for existence and creation have to be the same act, otherwise File.Exists then File.Create can fail.

          Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
          If you don't ask questions the answers won't stand in your way.
          Most of this sig is for Google, not ego.

          P Offline
          P Offline
          PIEBALDconsult
          wrote on last edited by
          #18

          I know. I don't think there's any way around a try/catch. I would still opt for a database.

          E 1 Reply Last reply
          0
          • P PIEBALDconsult

            I know. I don't think there's any way around a try/catch. I would still opt for a database.

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #19

            http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx[^]

            Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
            If you don't ask questions the answers won't stand in your way.
            Most of this sig is for Google, not ego.

            P 1 Reply Last reply
            0
            • E Ennis Ray Lynch Jr

              http://msdn.microsoft.com/en-us/library/aa363858(VS.85).aspx[^]

              Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
              If you don't ask questions the answers won't stand in your way.
              Most of this sig is for Google, not ego.

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #20

              Didn't the original poster mention the API?

              E 2 Replies Last reply
              0
              • P PIEBALDconsult

                Didn't the original poster mention the API?

                E Offline
                E Offline
                Ennis Ray Lynch Jr
                wrote on last edited by
                #21

                ??

                Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                If you don't ask questions the answers won't stand in your way.
                Most of this sig is for Google, not ego.

                T P 2 Replies Last reply
                0
                • E Ennis Ray Lynch Jr

                  ??

                  Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                  If you don't ask questions the answers won't stand in your way.
                  Most of this sig is for Google, not ego.

                  T Offline
                  T Offline
                  Terick
                  wrote on last edited by
                  #22

                  I'm using C# with ASP.NET.

                  1 Reply Last reply
                  0
                  • P PIEBALDconsult

                    Didn't the original poster mention the API?

                    E Offline
                    E Offline
                    Ennis Ray Lynch Jr
                    wrote on last edited by
                    #23

                    Using the Win32 API is a perfectly valid way to overcome limitations in .NET. (Or at least a way to cut off your nose to spite your face)

                    Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                    If you don't ask questions the answers won't stand in your way.
                    Most of this sig is for Google, not ego.

                    1 Reply Last reply
                    0
                    • E Ennis Ray Lynch Jr

                      ??

                      Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
                      If you don't ask questions the answers won't stand in your way.
                      Most of this sig is for Google, not ego.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #24

                      Oh, no, I see, you did.

                      1 Reply Last reply
                      0
                      • T Terick

                        Thank you all for your posts. It's been helpful. I'm still a little stuck, but I have the general idea. The files may get deleted, so I will need to take that into account. Also, I'm not using any database and for this application wont be using one. This is what I have so far. Please let me know if there are better methods:

                        public void filecount(string file)
                        {
                        int x;
                        int y;
                        string file;

                        int filepaths = Directory.GetFiles(@"C:\Desktop\Request", "*txt").Length;
                        x = filepaths +1;

                        StreamWriter sw;
                        sw = File.CreateText(@"C:\Desktop\Request\Text" +y +".txt");
                        file = Path.GetFileName(@"C:\Desktop\Request\Text" +y +".txt").ToString;
                        sw.Close();

                        return file;
                        }

                        public void btn_Click(object sender, EventArgs e)
                        {
                        filecount(file);
                        }

                        Thanks again for helping a newbie out!

                        R Offline
                        R Offline
                        riced
                        wrote on last edited by
                        #25

                        This won't work - in fact I don't think it will compile. 1 The return type is void but the function tries to return a string. 2 The parameter name (file) conflicts with the local variable also called file. 3 The value of y is not set but is used to construct the file name. Couple of other comments. 1 Why use the x and y variables when you have filepaths. You could just set it to Directory.GetFiles(@"C:\Desktop\Request", "*txt").Length + 1. Then use it instead of x or y. 2 This approach does not deal with deleted files. Suppose there are three files originally (text1.txt, text2.txt, text3.txt). Then text1.txt is deleted. What will the file count be? 3 The function is named filecount but it actually tries to return the next filename. 4 Read some of the suggestions carefully - there's good stuff in them that should solve the problem. Regards David R

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups