954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

rename text file by current date

Hi Alls,

I need help regarding to rename my event file by current date. It's mean my file will be rename by date automaticly.....i can write to the text but the name not replace with the current date....can u help me...plz

kerek2
Junior Poster
118 posts since Sep 2007
Reputation Points: 20
Solved Threads: 0
 

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
 

Tq bro...but how to use string.isnullorempty ?...can help me

kerek2
Junior Poster
118 posts since Sep 2007
Reputation Points: 20
Solved Threads: 0
 

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
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You