Here's a Q&D solution
Public Function NewFileName(ByVal FileName As String, ByVal FileExt As String) As String
'
Dim NewName As String
Dim DateStr As String
Dim TempStr() As String
Dim i As Integer
' Get date and time
DateStr = System.DateTime.Now.ToString
' Remove time
TempStr = Split(DateStr, " ")
DateStr = TempStr(0) ' Date part only
' Append
If String.IsNullOrEmpty(FileName) Then
NewName = DateStr
Else
NewName = FileName & DateStr
End If
' Remove illegal characters
For i = 0 To Path.GetInvalidFileNameChars.GetUpperBound(0)
If NewName.IndexOf(Path.GetInvalidFileNameChars(i)) >= 0 Then
' Remove illegal character
NewName.Replace(Path.GetInvalidFileNameChars(i), "")
End If
Next i
' Add file extension
If Not String.IsNullOrEmpty(FileExt) Then
NewName = NewName & "." & FileExt
End If
Return NewName
End Function
Call it: FileName = NewFileName("MyLog", "")
or FileName = NewFileName("MyLog", "log")
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203
String.IsNullOrEmpty() comes with .NET 2.0 or later so you're obviously using some .NET 1.x version.
I fixed the code to be compatible with .NET 1.x, at least for String.IsNullOrEmpty() part. I just commented out two lines of the original code in the case you will some day use .NET 2.0 or later
Public Function NewFileName(ByVal FileName As String, ByVal FileExt As String) As String
'
Dim NewName As String
Dim DateStr As String
Dim TempStr() As String
Dim i As Integer
' Get date and time
DateStr = System.DateTime.Now.ToString
' Remove time
TempStr = Split(DateStr, " ")
DateStr = TempStr(0) ' Date part only
' .NET 1.x compatibility replacement for String.IsNullOrEmpty()
If FileName Is Nothing Then
FileName = ""
End If
If FileExt Is Nothing Then
FileExt = ""
End If
' Append
'If String.IsNullOrEmpty(FileName) Then
If FileName.Length = 0 Then
NewName = DateStr
Else
NewName = FileName & DateStr
End If
' Remove illegal characters
For i = 0 To Path.GetInvalidFileNameChars.GetUpperBound(0)
If NewName.IndexOf(Path.GetInvalidFileNameChars(i)) >= 0 Then
' Remove illegal character
NewName.Replace(Path.GetInvalidFileNameChars(i), "")
End If
Next i
' Add file extension
'If Not String.IsNullOrEmpty(FileExt) Then
If FileExt.Length > 0 Then
NewName = NewName & "." & FileExt
End If
Return NewName
End Function
Teme64
Veteran Poster
1,031 posts since Aug 2008
Reputation Points: 218
Solved Threads: 203