script to delete all text before a specific string

I have a bunch of text files in a Windows file folder.  I need a script that will open up each file, delete everything in the text before a specific string value and then save the file with the new changes.  The string value is "From: <" (without the quotes).  The value only appears once in each text file.
Who is Participating?
käµfm³d 👽Connect With a Mentor Commented:
Will this fit your needs?
dim fso, folder, files, contents, tempFO, index
set fso = CreateObject("Scripting.FileSystemObject")
set folder = fso.GetFolder("C:\Documents and Settings\kaufmank\Desktop\New Folder")
set files = folder.Files
For Each file in files
  set tempFO = fso.OpenTextFile(file, 1)
  contents = tempFO.ReadAll()
  index = instr(contents, "From: <")
  If index > 0 Then
	set tempFO = fso.OpenTextFile(file, 2)
	tempFO.Write(Mid(contents, index))
  End If

Open in new window

Paste the script below into a text file with a .vbs extension.  Customize the value of the strFolder variable on line 5 with the location of the folder containing the files.  Running the script will check each .txt file in the folder and remove any text in each line which precedes the string defined in the strRemove variable.

Please test this carefully before running it on a production system.

Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
strFolder = "c:\files"
strExt = "txt"
strRemove = "From: <"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strFolder)
For Each objFile in objFolder.Files
    strFile = objFile.Path
    strFileExt = objFSO.GetExtensionName(strFile)
    intLines = 0
    If LCase(strFileExt) = LCase(strExt) Then
        Set objTextFile = objFSO.OpenTextFile(strFile, ForReading, False, TriStateUseDefault)
        arrText = Split(objTextFile.ReadAll, vbCrLf)
        For Each strNextLine in arrText
            intLineFinder = InStr(strNextLine, strRemove)
            If intLineFinder <> 0 Then
                strNextLine = Mid(strNextLine, intLineFinder)
            End If
            If intLines < UBound(arrText) Then
                strNextLine = strNextLine & vbCrLf
            End If
            strNewFile = strNewFile & strNextLine  
            intLines = intLines + 1
        Set objTextFile = objFSO.OpenTextFile(strFile, ForWriting)
        objTextFile.Write strNewFile
    End If

Open in new window

AManouxAuthor Commented:
Hi Shift-3, I tried your script first and it didn't appear to work correctly.  I saw that the modified date on the text file changed, so the script was at least executing, but it did not remove any information from the file.

Kaufmed, your script worked correctly.  Thanks!
käµfm³d 👽Commented:
Glad to assist!
AManouxAuthor Commented:
Hey kaufmed,
I'm going to post a new question that's a related request to the changes you helped me make to the text files.  If you'd like to acquire some more points look out for my name in the same topic section.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.