Replace text in multiple text files


  1.    22 Aug 2009 #1
    Join Date : Aug 2009
    Posts : 1
    64
    Local Time: 22:46

    Replace text in multiple text files


    Im trying to Replace text in multiple text files using the below mentioned code...but im not able to do it.

    Ex:
    Text File Content
    Line 1
    Line 2
    Line 3
    Line 4
    Line 5
    Line 6
    Line 7
    Line 8

    Now all i want the script to do is locate line 6 and delete everything above it in all the text files.

    Script:

    Const FORREADING = 1
    Const FORWRITING = 2

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    For Each objFile In objFSO.GetFolder(".").Files
    If objFSO.GetExtensionName(objFile.Name) = "F" Then
    strNewName = objFSO.GetBaseName(objFile.Name) & ".F"

    Set objTS = objFSO.OpenTextFile(strNewName, FORREADING)
    strText = objTS.ReadAll
    objTS.Close

    Set objTSW = objFSO.OpenTextFile(strNewName, FORWRITING)
    For i = 10 to (Ubound(arrLines))
    objFile.WriteLine arrLines(i)
    Next
    End If
    Next

    Any help would be appreciated!

    Thanks!
    GT
      My System SpecsSystem Spec

  2.    22 Aug 2009 #2
    mayayana Guest

    Re: Replace text in multiple text files



    >
    > Im trying to Replace text in multiple text files using the below
    > mentioned code...but im not able to do it.
    >
    > Ex:
    > Text File Content
    > Line 1
    > Line 2
    > Line 3
    > Line 4
    > Line 5
    > Line 6
    > Line 7
    > Line 8
    >
    > Now all i want the script to do is locate line 6 and delete everything
    > above it in all the text files.
    >
    You seem to have pasted your code toether
    from samples, but the sample sections don't
    match up.

    * The whole base name, extension name
    rebuilding part is pointless and leaves you
    with only a file name when you need a path:

    If objFSO.GetExtensionName(objFile.Name) = "F" Then
    strFilePath = objFile.Path
    Set objTS = objFSO.OpenTextFile(strFilePath, FORREADING)


    * After the read there should be a line like:

    arrLines = Split(strText, vbCrLf)

    (You can't write the arrLines lines if you never
    put the lines into the array.)

    * Then, in your code you start writing with:

    > For i = 10 to (Ubound(arrLines))
    But where does 10 come from? Don't you
    need to find your key line first? So you
    probably need something like:

    KeyLine = "text of line to find goes here"
    For i = 0 to ubound(arrLines)
    s = arrLines(i)
    if s = KeyLine then
    StartLine = i
    exit for
    end if
    Next

    '-- Now you have the file in an array,
    '-- StartLine is the line to begin writing from,
    '-- and you can start your write:

    Set objTSW = objFSO.OpenTextFile(strFilePath, FORWRITING)
    For i = StartLine to (Ubound(arrLines))

    '-- Note that you had objFile here. The Textstream
    '-- does WriteLine. The file object cannot.

    objTSW.WriteLine arrLines(i)
    '-- ...etc...

    Also note that you need to call objTSW.Close
    when you're done writing.

    You need to read through the code and make
    sure you understand what's happening, so that
    when you make mistakes like transposing objFile
    with objTSW you'll be able to catch it.

    > Script:
    >
    > Const FORREADING = 1
    > Const FORWRITING = 2
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    >
    > For Each objFile In objFSO.GetFolder(".").Files
    > If objFSO.GetExtensionName(objFile.Name) = "F" Then
    > strNewName = objFSO.GetBaseName(objFile.Name) & ".F"
    >
    > Set objTS = objFSO.OpenTextFile(strNewName, FORREADING)
    > strText = objTS.ReadAll
    > objTS.Close
    >
    > Set objTSW = objFSO.OpenTextFile(strNewName, FORWRITING)
    > For i = 10 to (Ubound(arrLines))
    > objFile.WriteLine arrLines(i)
    > Next
    > End If
    > Next
    >
    > Any help would be appreciated!
    >
    > Thanks!
    > GT
    >
    >
    > --
    > gurushab

      My System SpecsSystem Spec

  3.    22 Aug 2009 #3
    Todd Vargo Guest

    Re: Replace text in multiple text files


    gurushab wrote:

    >
    > Im trying to Replace text in multiple text files using the below
    > mentioned code...but im not able to do it.
    >
    > Ex:
    > Text File Content
    > Line 1
    > Line 2
    > Line 3
    > Line 4
    > Line 5
    > Line 6
    > Line 7
    > Line 8
    >
    > Now all i want the script to do is locate line 6 and delete everything
    > above it in all the text files.
    >
    > Script:
    >
    > Const FORREADING = 1
    > Const FORWRITING = 2
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    >
    > For Each objFile In objFSO.GetFolder(".").Files
    > If objFSO.GetExtensionName(objFile.Name) = "F" Then
    > strNewName = objFSO.GetBaseName(objFile.Name) & ".F"
    >
    > Set objTS = objFSO.OpenTextFile(strNewName, FORREADING)
    > strText = objTS.ReadAll
    > objTS.Close
    >
    > Set objTSW = objFSO.OpenTextFile(strNewName, FORWRITING)
    > For i = 10 to (Ubound(arrLines))
    > objFile.WriteLine arrLines(i)
    > Next
    > End If
    > Next
    My interpretation of what you want to do is, you want to remove the first
    five lines from each file in current folder having ".F" extension. I
    recommend you create a test folder and save/run it on the test folder to be
    sure it does what you want.

    Const FORREADING = 1
    Const FORWRITING = 2
    Const Lines2Skip = 5

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    For Each objFile In objFSO.GetFolder(".").Files
    If Ucase(objFSO.GetExtensionName(objFile.Name)) = "F" Then
    Set objTS = objFSO.OpenTextFile(objFile.Name, FORREADING)
    arrLines = Split(objTS.ReadAll, vbNewLine)
    objTS.Close

    Set objTSW = objFSO.OpenTextFile(objFile.Name, FORWRITING, True)
    For i = Lines2Skip to (Ubound(arrLines) - 1)
    objTSW.WriteLine arrLines(i)
    Next
    objTSW.Close
    End If
    Next

    --
    Todd Vargo
    (Post questions to group only. Remove "z" to email personal messages)

      My System SpecsSystem Spec

Replace text in multiple text files

Similar Threads
Thread Forum
compare two text files and report differences in a text file format
I thought that perhaps that Compare-Object could be used as the basis of a script that takes two text files and creates a text file documenting...
PowerShell
Remove lines from multiple text files
Hi, Bit of a noob question - I came across this bit of code the other day: $f=${C:\Somepath\file.txt} $f=$null ${C:\Somepath\file.txt}=$f ...
PowerShell
Newbie question: replace multiple strings in multiple text files
Hi there -- I've never created a script before now, and what I want to do is periodically have a script go into fifteen text files and do...
VB Script
replacing text on multiple text files
Hello. I am looking for a way to automatically replace text on multiple text files. I am writing my first script for work, but I've hit a brick...
VB Script
How to search and replace text in a string
I need to be able to read a filename like "Doe, John" and then convert that into a string like "jdoe" (a username) so I can use it later to apply...
PowerShell
Search and replace in a text file?
Is there an easy way to search for a string in a text file, and replace it with something else? Preferably using a regex, but it's not really that...
PowerShell
Convert Unix text files to PC text files
Has anyone got the code to convert unix text files (lines end with char(10)) to PC text files (lines end with char(10) and char(13)) ? Thanks in...
PowerShell

Our Sites
  • Ten Forums
  • Eight Forums
  • Seven Forums
  • Help Me Bake
  • Site Links
  • Contact Us
  • Privacy and Cookies
  • About Us
    Windows Vista Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

    Designer Media Ltd
    All times are GMT -5. The time now is 23:46.
    .