[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 752
  • Last Modified:

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.
0
AManoux
Asked:
AManoux
  • 2
  • 2
1 Solution
 
Shift-3Commented:
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)
        objTextFile.Close
        
        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
        Next
 
        Set objTextFile = objFSO.OpenTextFile(strFile, ForWriting)
        objTextFile.Write strNewFile
        objTextFile.Close
    End If
Next

Open in new window

0
 
käµfm³d 👽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()
  tempFO.Close()
  index = instr(contents, "From: <")
  
  If index > 0 Then
	set tempFO = fso.OpenTextFile(file, 2)
	tempFO.Write(Mid(contents, index))
	tempFO.Close()
  End If
Next

Open in new window

0
 
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!
0
 
käµfm³d 👽Commented:
Glad to assist!
0
 
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.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now