Rename Textfile by String within textfile

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alexandra
    New Member
    • Feb 2008
    • 1

    Rename Textfile by String within textfile

    Hi,

    I have a folder with hundreds of text files. Each text file contains a row with a date. I want to rename the text file with the date contained in this text file. Example: Suppose I have 2 textfiles, then one text file would contain a row like this:

    DATE 1 February 2008

    and the other would contain a row like this:

    DATE 2 February 2008

    The rows are not necessarily at the same position (e.g. it's not always the third row) All text files have in common that the date line starts with "Date". Right now the textfiles are named by numbers. How can I rename each text file by the date given within this file? In the above example, I want textfile 1 to be "1February2 008" and textfile 2 "2February2 008"

    Thank you very much for your help!
    Alexandra
  • debasisdas
    Recognized Expert Expert
    • Dec 2006
    • 8119

    #2
    You can follow these steps.

    1.Read the content of the file
    2.Create a file by that name.
    3.Copy the content to the new file.
    4.Delte the old file.

    You have to use FileSystemObjec t for the purpose.

    Comment

    • jamesd0142
      Contributor
      • Sep 2007
      • 471

      #3
      I fancied giving this a go also:

      Came up with this which works:

      sure you can edit it to suit:

      [code=vbnet]


      Imports System.IO
      Public Class Form1

      Dim date1 As String
      Dim j As String
      Dim i As Integer
      Dim pos As Integer

      Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
      DeleteAllSubFol ders("c:\a")
      End Sub

      Private Sub DeleteAllSubFol ders(ByVal StartPath As String)
      Dim myfolder As DirectoryInfo = New DirectoryInfo(S tartPath)
      Dim mySubfolders() As FileInfo = myfolder.GetFil es()
      Dim strFiles() As FileInfo = myfolder.GetFil es()

      For Each myItem As FileInfo In strFiles
      'myItem.Delete( )
      'open File
      Dim a As String = StartPath & "\" & myItem.ToString

      'read File
      Dim EntireLine1 As String
      Dim oFile1 As System.IO.File
      Dim oRead1 As System.IO.Strea mReader
      If System.IO.File. Exists(a) = True Then
      oRead1 = oFile1.OpenText (a)
      EntireLine1 = oRead1.ReadToEn d
      oRead1.Close() 'test line
      End If
      Dim b As String = "2008" 'vbCrLf
      'find date
      date1 = EntireLine1.Sub string(EntireLi ne1.IndexOf("DA TE"), EntireLine1.Ind exOf(b)) ', EntireLine1.Ind exOf("2008"))

      'function cuts of the end of the line where it finds a return character or line feed...
      For i = 1 To Len(date1)
      j = Mid(date1, i, 1)
      If a = vbCr Or a = vbLf Then
      Exit For
      End If
      Next
      date1 = Mid(date1, 6, i - 7)

      'rename File
      If System.IO.File. Exists(a) = True Then
      System.IO.File. Move(a, StartPath & "\" & RTrim(date1) & ".txt")
      End If

      Next

      End Sub
      End Class
      [/code]

      Comment

      • WinblowsME
        New Member
        • Jan 2008
        • 58

        #4
        Backup before you do a test run.

        [CODE=vb]
        ' VBA

        Sub Test()
        Dim source_path As String

        source_path = "C:\Documen ts and Settings\Winblo wsME\Desktop\Te mp"

        Call Rename_Files(so urce_path)
        End Sub

        Private Sub Rename_Files(so urce_path As String)
        Dim regex As Object, file_name As String, curr_date As String

        Set regex = CreateObject("V BScript.RegExp" )

        regex.Pattern = "\\*$"
        source_path = regex.Replace(s ource_path, "\")

        file_name = Dir(source_path & "*.txt")

        Do While file_name <> ""
        curr_date = Get_Date(source _path & file_name)

        If curr_date <> "" Then
        Debug.Print source_path & file_name & "->" & source_path & curr_date & ".txt"

        'Name source_path & file_name As source_path & curr_date & ".txt"
        End If

        file_name = Dir
        Loop
        End Sub

        Private Function Get_Date(file_n ame As String) As String
        Dim regex As Object, curr_line As String, curr_date As String

        Set regex = CreateObject("V BScript.RegExp" )

        regex.Pattern = "^ *DATE *[0-9]{1,2} *.* *[0-9]{4}"
        curr_line = ""
        curr_date = ""

        Open file_name For Input As #1
        Do While Not EOF(1)
        Line Input #1, curr_line

        If regex.Test(curr _line) Then
        curr_date = curr_line
        curr_date = Replace(curr_li ne, "DATE ", "")
        curr_date = Replace(curr_li ne, " ", "")
        Exit Do
        End If
        Loop
        Close #1

        Get_Date = curr_date
        End Function[/CODE]

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Originally posted by debasisdas
          You can follow these steps. ...
          • Far too wasteful to copy everything - just rename them. (What if the files are huge? What if you don't have enough space to copy?)
          • You do not have to use FSO, there are a number of methods which can be used.

          Alexandra, I don't think you told us what version of VB you're using.

          Winnie, pretty sure you've got a bug in lines 48-50.
          Last edited by Killer42; Feb 8 '08, 01:28 AM.

          Comment

          • Robbie
            New Member
            • Mar 2007
            • 180

            #6
            Originally posted by Killer42
            Winnie, pretty sure you've got a bug in lines 48-50.
            Yes, curr_line on lines 49 and 50 should be curr_date (since curr_date became curr_line on line 48). Line 49 removes "DATE ", then line 50 removes any spaces, probably in case there was a space before "DATE " which he doesn't want to exist.

            What sounds dodgy to me though is this removal of any and all spaces; what if there are meant to be spaces? If you want to remove only the space to the left of "DATE ", you could use Mid() to see if it's a space, and then combine 2 Mid()s to piece the first and second half of the line (of the text file) together, on either side of the space which you want to leave out.

            Comment

            • WinblowsME
              New Member
              • Jan 2008
              • 58

              #7
              Sorry, lines 49 and 50 should be curr_date. It was a last second change that I've overlooked.

              Comment

              Working...