script to delete all text before a specific string

Posted on 2009-04-15
Last Modified: 2012-05-06
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.
Question by:AManoux
    LVL 38

    Expert Comment

    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

    LVL 74

    Accepted Solution

    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

    LVL 1

    Author Comment

    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!
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Glad to assist!
    LVL 1

    Author Comment

    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.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Join & Write a Comment

    Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
    Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

    755 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now