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

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.

I don't think that is what I am looking for. Below is my code-

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        ' Write transactions to text file for audit purposes
        ' Folder c:\MTTLog Upload must exist on 'c' drive

        Dim TransLog As String = "c:\MTTap Upload\MTTLogSent"
        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
        TLDay = DateTime.Now.Day
        TLMonth = DateTime.Now.Month
        TLYear = DateTime.Now.Year
        TLHour = DateTime.Now.Hour
        TLMinute = DateTime.Now.Minute
        TLDate = TLMonth.ToString + TLDay.ToString + TLYear.ToString
        TLTime = TLHour.ToString + TLMinute.ToString
        TLDateTime = TLDate + "_" + TLTime
        TransLog = TransLog + TLDateTime + ".txt"
        FileOpen(1, TransLog, OpenMode.Append)
        Dim dgvRow As DataGridViewRow
        Dim SentCnt As String
        SentCnt = dgView1.SelectedRows.Count
        lblSent.Text = SentCnt + " Records selected and sent"
        Dim transrecord As String

        For Each dgvRow In dgView1.SelectedRows
            'Get Transaction column from DataGridView of runninglog table in Access DB TransLogger
            'The data in this column is only 122 bytes - MQ Client requires 300 bytes so 
            'We need to pad the transrecord string before sending to the MQ client
            transrecord = dgvRow.Cells(7).Value
            transrecord = transrecord.PadRight(300)


            ' Write transaction to text file for audit purposes
            ' To stop logging to the text file, comment out the nextline and the fileclose() line below

            WriteLine(1, transrecord)
        Next
       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.

Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        ' 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
        Dim TransLog As String = "c:\MTTap Upload\MTTLogSent"
        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)
        Dim dgvRow As DataGridViewRow
        Dim SentCnt As String
        SentCnt = dgView1.SelectedRows.Count
        lblSent.Text = SentCnt + " Records selected and sent"
        Dim transrecord As String

        For Each dgvRow In dgView1.SelectedRows
            'Get Transaction column from DataGridView of runninglog table in Access DB TransLogger
            'The data in this column is only 122 bytes - MQ Client requires 300 bytes so 
            'We need to pad the transrecord string before sending to the MQ client
            transrecord = dgvRow.Cells(7).Value
            transrecord = transrecord.PadRight(300)


            ' Write transaction to text file for audit purposes
            ' To stop logging to the text file, comment out the nextline and the fileclose() line below

            WriteLine(1, transrecord)
        Next
        fileclose()

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

[B]Here is the macro -[/B] 
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)

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

Try this;

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

here is my 2-3 lines of code for the same

Dim tDate As New DateTime(Date.Today.Year, Date.Today.Month, Date.Today.Day, Date.Now.Hour, Date.Now.Minute, Date.Now.Second)
Dim csvFile As String
csvFile = "E:\test\" & tDate.ToString("yyyyMMdd_HHmmss") & ".csv"

commented: Don't care; this post is over 5 years old. I'm sure the OP has moved on by now. -1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.