date and time in file name

Please support our VB.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2007
Posts: 3
Reputation: poppo is an unknown quantity at this point 
Solved Threads: 0
poppo poppo is offline Offline
Newbie Poster

date and time in file name

 
0
  #1
Mar 7th, 2007
I have a vb.net 2005 project that is writting an audit trail to a text file. I would like to format the file name so that it includes the date and time it was created. Such as myfile_030707_1645.txt I am not having much luck doing so. I am new at vb.net so any help is appreciated.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 5
Reputation: qb45Mike is an unknown quantity at this point 
Solved Threads: 0
qb45Mike qb45Mike is offline Offline
Newbie Poster

Re: date and time in file name

 
0
  #2
Mar 7th, 2007
You could do something like this (from a snipplet of existing code):

Dim directoryEntries As String() = System.IO.Directory.GetFileSystemEntries("C:\theDirectory")
Dim fileEntries As String()
'Iterates through each directory
For Each strDirs As String In directoryEntries
'assign each file to fileentries()
fileEntries = System.IO.Directory.GetFiles(strDirs)
'Iterates throught each file
For Each strFiles As String In fileEntries
Dim fdt As DateTime = System.IO.File.GetLastWriteTime(strFiles)
'Do other stuff...
Next
Next

the system.io.file is what you will want to play around with...

Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: poppo is an unknown quantity at this point 
Solved Threads: 0
poppo poppo is offline Offline
Newbie Poster

Re: date and time in file name

 
0
  #3
Mar 8th, 2007
I don't think that is what I am looking for. Below is my code-

  1.  
  2. Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  3. ' Write transactions to text file for audit purposes
  4. ' Folder c:\MTTLog Upload must exist on 'c' drive
  5.  
  6. Dim TransLog As String = "c:\MTTap Upload\MTTLogSent"
  7. Dim TLDateTime As String
  8. Dim TLDay As String
  9. Dim TLMonth As Integer
  10. Dim TLYear As Integer
  11. Dim TLHour As Integer
  12. Dim TLMinute As Integer
  13. Dim TLDate As String
  14. Dim TLTime As String
  15. TLDay = DateTime.Now.Day
  16. TLMonth = DateTime.Now.Month
  17. TLYear = DateTime.Now.Year
  18. TLHour = DateTime.Now.Hour
  19. TLMinute = DateTime.Now.Minute
  20. TLDate = TLMonth.ToString + TLDay.ToString + TLYear.ToString
  21. TLTime = TLHour.ToString + TLMinute.ToString
  22. TLDateTime = TLDate + "_" + TLTime
  23. TransLog = TransLog + TLDateTime + ".txt"
  24. FileOpen(1, TransLog, OpenMode.Append)
  25. Dim dgvRow As DataGridViewRow
  26. Dim SentCnt As String
  27. SentCnt = dgView1.SelectedRows.Count
  28. lblSent.Text = SentCnt + " Records selected and sent"
  29. Dim transrecord As String
  30.  
  31. For Each dgvRow In dgView1.SelectedRows
  32. 'Get Transaction column from DataGridView of runninglog table in Access DB TransLogger
  33. 'The data in this column is only 122 bytes - MQ Client requires 300 bytes so
  34. 'We need to pad the transrecord string before sending to the MQ client
  35. transrecord = dgvRow.Cells(7).Value
  36. transrecord = transrecord.PadRight(300)
  37.  
  38.  
  39. ' Write transaction to text file for audit purposes
  40. ' To stop logging to the text file, comment out the nextline and the fileclose() line below
  41.  
  42. WriteLine(1, transrecord)
  43. Next
  44. fileclose()

Yields - MTTLogSent382007_119.txt
Should look like - MTTLogSent03082007_1109.txt

Found solution by playing around with code shortly after posting this. Here is my solution.

  1.  
  2. Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
  3. ' Write transactions to text file for audit purposes
  4. ' Folder c:\MTTLog Upload must exist on 'c' drive
  5. ' Create file name including date and time stamp
  6. Dim TransLog As String = "c:\MTTap Upload\MTTLogSent"
  7. Dim TLDateTime As String
  8. Dim TLDay As String
  9. Dim TLMonth As Integer
  10. Dim TLYear As Integer
  11. Dim TLHour As Integer
  12. Dim TLMinute As Integer
  13. Dim TLDate As String
  14. Dim TLTime As String
  15. Dim TLSecond As Integer
  16. TLDay = DateTime.Now.Day
  17. TLMonth = DateTime.Now.Month
  18. TLYear = DateTime.Now.Year
  19. TLHour = DateTime.Now.Hour
  20. TLMinute = DateTime.Now.Minute
  21. TLSecond = DateTime.Now.Second
  22.  
  23. Dim MyDate As New DateTime(TLYear, TLMonth, TLDay, TLHour, TLMinute, TLSecond)
  24. Dim MyString As String = MyDate.ToString("MMMddyyyy_HHmmss")
  25.  
  26. TLDate = TLMonth.ToString + TLDay.ToString + TLYear.ToString
  27. TLTime = TLHour.ToString + TLMinute.ToString
  28. TLDateTime = TLDate + "_" + TLTime
  29. TransLog = TransLog + MyString + ".txt"
  30. FileOpen(1, TransLog, OpenMode.Append)
  31. Dim dgvRow As DataGridViewRow
  32. Dim SentCnt As String
  33. SentCnt = dgView1.SelectedRows.Count
  34. lblSent.Text = SentCnt + " Records selected and sent"
  35. Dim transrecord As String
  36.  
  37. For Each dgvRow In dgView1.SelectedRows
  38. 'Get Transaction column from DataGridView of runninglog table in Access DB TransLogger
  39. 'The data in this column is only 122 bytes - MQ Client requires 300 bytes so
  40. 'We need to pad the transrecord string before sending to the MQ client
  41. transrecord = dgvRow.Cells(7).Value
  42. transrecord = transrecord.PadRight(300)
  43.  
  44.  
  45. ' Write transaction to text file for audit purposes
  46. ' To stop logging to the text file, comment out the nextline and the fileclose() line below
  47.  
  48. WriteLine(1, transrecord)
  49. Next
  50. fileclose()
Last edited by poppo; Mar 8th, 2007 at 12:54 pm. Reason: found solution
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 3
Reputation: modom is an unknown quantity at this point 
Solved Threads: 0
modom modom is offline Offline
Newbie Poster

Re: date and time in file name

 
0
  #4
Jun 4th, 2007
First off I want to say sorry for any breach of posting ethics by noobieness I'm trying to do the something close to this but in VB not VB.net. I thought I had hit the target with the code above but it doesn't work. All I need it to do is create the file with the date and time on it. Ex ReportMMMMDDDDYYYYHHMMSS.pdf


Here is the macro -
Sub main()

Dim dDate As Date
Dim boDP As busobj.DataProvider
Dim sLast As String
Dim sFileName As String
Dim sDir As String
Dim dTime As Integer
Dim MyTime As String

''''''Define the path to save the file to

'sDir = "\\ReportDB\CIC\"
sDir = "D:\ET_REPORTS\"

'Application.Interactive = False

ActiveDocument.Refresh

Set boDP = ActiveDocument.DataProviders.Item(1)

sLast = boDP.LastExecutionDate
MyTime = boDP.LastExecutionTime

'The line below captures the last time the report was refreshed.

dDate = CDate(sLast)
dTime = CDate(MyTime)

sFileName = sDir & ActiveDocument.Name & "(" & CStr(Month(dDate)) & "-" & CStr(Day(dDate)) & "-" & CStr(Year(dDate)) & "_" & CStr(Time(dTime)) & ")" & ".pdf"

ActiveDocument.Reports.Item(1).ExportAsPDF (sFileName)

End Sub


and it gives me error
(303)Error with no ErrorHandler with no BreakOnVBAError = False

Any help would be greaty appreciated (yes flame away if you want to also)
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 3
Reputation: poppo is an unknown quantity at this point 
Solved Threads: 0
poppo poppo is offline Offline
Newbie Poster

Re: date and time in file name

 
0
  #5
Jun 4th, 2007
With some perseverance, I got my problem solved.

Here is what I ended up with. don't know if this will help you or not. I too am a newbie at vb.net.

code

code=vb.net




' Write transactions to text file for audit purposes
' Folder c:\MTTLog Upload\ must exist on 'c' drive
' Create file name including date and time stamp
' Creates file name as MTTDownLogSent_May212007_135038.txt
Dim TransLog As String = "c:\MTTap Upload\MTTDownLogSent_"
Dim TLDateTime As String
Dim TLDay As String
Dim TLMonth As Integer
Dim TLYear As Integer
Dim TLHour As Integer
Dim TLMinute As Integer
Dim TLDate As String
Dim TLTime As String
Dim TLSecond As Integer
TLDay = DateTime.Now.Day
TLMonth = DateTime.Now.Month
TLYear = DateTime.Now.Year
TLHour = DateTime.Now.Hour
TLMinute = DateTime.Now.Minute
TLSecond = DateTime.Now.Second

Dim MyDate As New DateTime(TLYear, TLMonth, TLDay, TLHour, TLMinute, TLSecond)
Dim MyString As String = MyDate.ToString("MMMddyyyy_HHmmss")

TLDate = TLMonth.ToString + TLDay.ToString + TLYear.ToString
TLTime = TLHour.ToString + TLMinute.ToString
TLDateTime = TLDate + "_" + TLTime
TransLog = TransLog + MyString + ".txt"
FileOpen(1, TransLog, OpenMode.Append)
/code



It may not be the best way to do it, but it worked for what I need.



Bill
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 2
Reputation: mivc is an unknown quantity at this point 
Solved Threads: 0
mivc mivc is offline Offline
Newbie Poster

Re: date and time in file name

 
0
  #6
Jun 8th, 2007
Try this;

  1. sFilename = Format(date,"MMMM") & Format(date,"DDDD") & Format(date,"YYYY") & Format(Time,"HH") & Format(Time,"NN") & Format(Time,"SS") & ".PDF"

The above code will result in JuneFriday2007173710.PDF
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC