Saving a file w/Date & Time

Hello Everyone,

I am attempting to save a CSV file with current date and time (down to seconds) appended to the file name.
It would look like this : 06242013_080530_Test1.csv

I have the code below and it will save the file as Test1.csv but I need to append the current date and time to the front of the file name.
Thanks

f=GetSaveFolderItem(FileTypes1.csv, "Test1.csv") if f <> Nil then t=TextOutputStream.Create(f) t.WriteLine savetext t.Close

Use the Date class: http://documentation.xojo.com/index.php/Date, that will get you access to all the date elements you want. Then in the FolderItem just update the Name properly accordingly. Something like:

Dim now As New Date Dim f As FolderItem = GetSaveFolderItem(FileTypes1.csv, "Test1.csv") f.Name = now.ShortDate + "-" + now.ShortTime + "-" + f.Name

will do the trick. You may wish to get fancy and format the date/time more to your liking though.

Opps. Sorry, that will not work exactly as planned because changing the name renames the file, and it must exist to work. Something like this would be better (untested, sorry):

Dim now As Date Dim baseFile As FolderItem = GetSaveFolderItem(FileTypes1.csv, "Test1.csv") Dim f As FolderItem = baseFile.Directory.Child(now.ShortDate + "-" + now.ShortTime + "-" + f.Name)

There may be better ways, but that’ll get you started. The docs are your friend.