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
  1. Home
  2. General Programming
  3. C#
  4. create a .csv file using a variable as the name

create a .csv file using a variable as the name

Scheduled Pinned Locked Moved C#
helpquestion
24 Posts 7 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.
  • M Offline
    M Offline
    mocasu
    wrote on last edited by
    #1

    Hi, I would like to know how I can create a .csv file with today's date and a variable name. I have the following: int day = date.Day; int month= date.Month; int year= date.Year; string str="hi"; int res="123"; Now I want to create a .csv file with the following name : 13102007_hi_123 I though of putting everything into a single string (lets' say, result) but then, how will the following code replace result with the string!? At present it creates a .csv file with the name of result if (!File.Exists("c:\\code\\result.txt")) { File.Create("c:\\code\\result.txt"); } Any help will be greatly appreciated...Cheers! ALSO, can I add a heather that will be ignored by excel?

    G P 2 Replies Last reply
    0
    • M mocasu

      Hi, I would like to know how I can create a .csv file with today's date and a variable name. I have the following: int day = date.Day; int month= date.Month; int year= date.Year; string str="hi"; int res="123"; Now I want to create a .csv file with the following name : 13102007_hi_123 I though of putting everything into a single string (lets' say, result) but then, how will the following code replace result with the string!? At present it creates a .csv file with the name of result if (!File.Exists("c:\\code\\result.txt")) { File.Create("c:\\code\\result.txt"); } Any help will be greatly appreciated...Cheers! ALSO, can I add a heather that will be ignored by excel?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      Separating the date into it's components isn't very useful. It's much easier to just format the date the way that you want: date.ToString("ddMMyyyy"). If you don't know how to concatenate strings and use variables instead of literal string values, you really need to pick up a beginners book and gets some basics down.

      --- single minded; short sighted; long gone;

      M 1 Reply Last reply
      0
      • G Guffa

        Separating the date into it's components isn't very useful. It's much easier to just format the date the way that you want: date.ToString("ddMMyyyy"). If you don't know how to concatenate strings and use variables instead of literal string values, you really need to pick up a beginners book and gets some basics down.

        --- single minded; short sighted; long gone;

        M Offline
        M Offline
        mocasu
        wrote on last edited by
        #3

        ...I know how to concatenate strings! the problem is how I then refer to that "variable" when creating the .csv

        M G 2 Replies Last reply
        0
        • M mocasu

          ...I know how to concatenate strings! the problem is how I then refer to that "variable" when creating the .csv

          M Offline
          M Offline
          mocasu
          wrote on last edited by
          #4

          ...In case it is not clear... int day = date.Day; int month= date.Month; int year= date.Year; string str="hi"; int res="123"; string fileName = day + month + year + "_" + str + "_" + res However, if I do : File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName ...any ideas?

          C D D C 4 Replies Last reply
          0
          • M mocasu

            ...In case it is not clear... int day = date.Day; int month= date.Month; int year= date.Year; string str="hi"; int res="123"; string fileName = day + month + year + "_" + str + "_" + res However, if I do : File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName ...any ideas?

            C Offline
            C Offline
            Colin Angus Mackay
            wrote on last edited by
            #5

            Have you heard of string.Format()? Surely you must have seen it in the intellisence and wondered if it might be useful.

            mocasu wrote:

            File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName

            Have you thought of also concatenating the file path into the string too?


            Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

            M 1 Reply Last reply
            0
            • C Colin Angus Mackay

              Have you heard of string.Format()? Surely you must have seen it in the intellisence and wondered if it might be useful.

              mocasu wrote:

              File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName

              Have you thought of also concatenating the file path into the string too?


              Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

              M Offline
              M Offline
              mocasu
              wrote on last edited by
              #6

              ...what can I say...thanks both of you for being so ...

              C 1 Reply Last reply
              0
              • M mocasu

                ...what can I say...thanks both of you for being so ...

                C Offline
                C Offline
                Colin Angus Mackay
                wrote on last edited by
                #7

                mocasu wrote:

                ...what can I say...thanks both of you for being so ...

                "Sarcastic"? (That's the normal accusation) :rose:


                Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                M 1 Reply Last reply
                0
                • M mocasu

                  Hi, I would like to know how I can create a .csv file with today's date and a variable name. I have the following: int day = date.Day; int month= date.Month; int year= date.Year; string str="hi"; int res="123"; Now I want to create a .csv file with the following name : 13102007_hi_123 I though of putting everything into a single string (lets' say, result) but then, how will the following code replace result with the string!? At present it creates a .csv file with the name of result if (!File.Exists("c:\\code\\result.txt")) { File.Create("c:\\code\\result.txt"); } Any help will be greatly appreciated...Cheers! ALSO, can I add a heather that will be ignored by excel?

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

                  System.IO.FileInfo fi = new System.IO.FileInfo
                  (
                  string.Format
                  (
                  "{0:ddMMyyyy}_{1}_{2}.csv"
                  ,
                  System.DateTime.Now
                  ,
                  "hi"
                  ,
                  123
                  )
                  ) ;

                  using ( System.IO.FileStream tw = fi.OpenWrite() )
                  {
                  ...
                  }

                  However, I do suggest you format the date as "yyyyMMdd" to comply with ISO 8601. Don't bother testing for file existence unless you want to read it, not write it. The OpenWrite will create the file if it doesn't exist, and overwrite it if it does.

                  M 1 Reply Last reply
                  0
                  • C Colin Angus Mackay

                    mocasu wrote:

                    ...what can I say...thanks both of you for being so ...

                    "Sarcastic"? (That's the normal accusation) :rose:


                    Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                    M Offline
                    M Offline
                    mocasu
                    wrote on last edited by
                    #9

                    ...if that's what you think you were being...Not everybody thinks the same...All of us can finish it with something different, it is just a matter of judgement

                    C 1 Reply Last reply
                    0
                    • M mocasu

                      ...if that's what you think you were being...Not everybody thinks the same...All of us can finish it with something different, it is just a matter of judgement

                      C Offline
                      C Offline
                      Colin Angus Mackay
                      wrote on last edited by
                      #10

                      mocasu wrote:

                      ...if that's what you think you were being...

                      I didn't say that I thought it. I said it was what I (we) are normally accused of. :)


                      Upcoming FREE developer events: * Glasgow: SQL Server Managed Objects AND Reporting Services ... My website

                      1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        System.IO.FileInfo fi = new System.IO.FileInfo
                        (
                        string.Format
                        (
                        "{0:ddMMyyyy}_{1}_{2}.csv"
                        ,
                        System.DateTime.Now
                        ,
                        "hi"
                        ,
                        123
                        )
                        ) ;

                        using ( System.IO.FileStream tw = fi.OpenWrite() )
                        {
                        ...
                        }

                        However, I do suggest you format the date as "yyyyMMdd" to comply with ISO 8601. Don't bother testing for file existence unless you want to read it, not write it. The OpenWrite will create the file if it doesn't exist, and overwrite it if it does.

                        M Offline
                        M Offline
                        mocasu
                        wrote on last edited by
                        #11

                        Thanks for the info.;P I was checking for file existance in case the file had already been created, so the new file should have some extra character in the name to differenciate them.

                        P 2 Replies Last reply
                        0
                        • M mocasu

                          ...In case it is not clear... int day = date.Day; int month= date.Month; int year= date.Year; string str="hi"; int res="123"; string fileName = day + month + year + "_" + str + "_" + res However, if I do : File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName ...any ideas?

                          D Offline
                          D Offline
                          DaveX86
                          wrote on last edited by
                          #12

                          go like this: File.Create("c:\\code\\" + filename + ".txt"); It's one of those obvious things that you can't see...like having to look a little right or left to see a dim star. Dave

                          M 1 Reply Last reply
                          0
                          • D DaveX86

                            go like this: File.Create("c:\\code\\" + filename + ".txt"); It's one of those obvious things that you can't see...like having to look a little right or left to see a dim star. Dave

                            M Offline
                            M Offline
                            mocasu
                            wrote on last edited by
                            #13

                            Thanks a lot David! really appreciate...that is what i was looking for. -- modified at 11:39 Saturday 13th October, 2007

                            D 1 Reply Last reply
                            0
                            • M mocasu

                              ...In case it is not clear... int day = date.Day; int month= date.Month; int year= date.Year; string str="hi"; int res="123"; string fileName = day + month + year + "_" + str + "_" + res However, if I do : File.Create("c:\\code\\fileName.txt"), it creates a file with the name "fileName"...rather than the value of fileName ...any ideas?

                              D Offline
                              D Offline
                              Dave Kreskowiak
                              wrote on last edited by
                              #14

                              Colins sarcasm came from the fact that you knew how to do string concantenation, but when it came to giving a string representing a filename, you suddenly forgot how to do string concantenation! Someone told you that "1+1=2" and you said "I KNOW!!", then when they asked you "what's 1+1?", you said you didn't know!

                              A guide to posting questions on CodeProject[^]
                              Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                   2006, 2007

                              M D 2 Replies Last reply
                              0
                              • M mocasu

                                Thanks for the info.;P I was checking for file existance in case the file had already been created, so the new file should have some extra character in the name to differenciate them.

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

                                Well that's alright then.

                                1 Reply Last reply
                                0
                                • M mocasu

                                  Thanks a lot David! really appreciate...that is what i was looking for. -- modified at 11:39 Saturday 13th October, 2007

                                  D Offline
                                  D Offline
                                  DaveX86
                                  wrote on last edited by
                                  #16

                                  You're welcome :)

                                  1 Reply Last reply
                                  0
                                  • M mocasu

                                    Thanks for the info.;P I was checking for file existance in case the file had already been created, so the new file should have some extra character in the name to differenciate them.

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

                                    System.IO.FileInfo fi ;
                                    int seq = 0 ;

                                    do
                                    {
                                    fi = new System.IO.FileInfo
                                    (
                                    string.Format
                                    (
                                    "{0:ddMMyyyy}_{1}_{2:000}.csv"
                                    ,
                                    System.DateTime.Now
                                    ,
                                    "hi"
                                    ,
                                    ++seq
                                    )
                                    ) ;
                                    } while ( fi.Exists ) ;

                                    using ( System.IO.FileStream tw = fi.OpenWrite() )
                                    {

                                    }

                                    M 1 Reply Last reply
                                    0
                                    • D Dave Kreskowiak

                                      Colins sarcasm came from the fact that you knew how to do string concantenation, but when it came to giving a string representing a filename, you suddenly forgot how to do string concantenation! Someone told you that "1+1=2" and you said "I KNOW!!", then when they asked you "what's 1+1?", you said you didn't know!

                                      A guide to posting questions on CodeProject[^]
                                      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                           2006, 2007

                                      M Offline
                                      M Offline
                                      mocasu
                                      wrote on last edited by
                                      #18

                                      ...and what "good" did sarcasm brought to the whole afair!? ... I see your point Dave... " but when it came to giving a string representing a filename, you suddenly forgot how to do string concantenation! " It wasn't that I forgot,I just didn't know that it could be done like that...as you all might have gueesed by now I am new with this. Anyway, thanks to PIEBALconsul and David for the help

                                      G 1 Reply Last reply
                                      0
                                      • P PIEBALDconsult

                                        System.IO.FileInfo fi ;
                                        int seq = 0 ;

                                        do
                                        {
                                        fi = new System.IO.FileInfo
                                        (
                                        string.Format
                                        (
                                        "{0:ddMMyyyy}_{1}_{2:000}.csv"
                                        ,
                                        System.DateTime.Now
                                        ,
                                        "hi"
                                        ,
                                        ++seq
                                        )
                                        ) ;
                                        } while ( fi.Exists ) ;

                                        using ( System.IO.FileStream tw = fi.OpenWrite() )
                                        {

                                        }

                                        M Offline
                                        M Offline
                                        mocasu
                                        wrote on last edited by
                                        #19

                                        Cheers!

                                        1 Reply Last reply
                                        0
                                        • D Dave Kreskowiak

                                          Colins sarcasm came from the fact that you knew how to do string concantenation, but when it came to giving a string representing a filename, you suddenly forgot how to do string concantenation! Someone told you that "1+1=2" and you said "I KNOW!!", then when they asked you "what's 1+1?", you said you didn't know!

                                          A guide to posting questions on CodeProject[^]
                                          Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
                                               2006, 2007

                                          D Offline
                                          D Offline
                                          DaveX86
                                          wrote on last edited by
                                          #20

                                          Dave Kreskowiak wrote:

                                          Someone told you that "1+1=2" and you said "I KNOW!!", then when they asked you "what's 1+1?", you said you didn't know!

                                          ...depends on whether or not you're working in binary, hex, decimal, octal...you need to be more specific :)

                                          P 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