replacing text on multiple text files


  1.    14 Aug 2008 #1
    chufa72 Guest

    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 wall. The
    ways I've tried and failed only work on a single text file at a time. Thanks,

    David
      My System SpecsSystem Spec

  2.    14 Aug 2008 #2
    Todd Vargo Guest

    Re: replacing text on multiple text files


    chufa72 wrote:

    > 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 wall.
    The

    > ways I've tried and failed only work on a single text file at a time.
    Thanks,

    This may be a dumb question, but what ways did you try for multiple files?
    We can not tell why you hit a brick wall if we can not see the code you
    used.

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

      My System SpecsSystem Spec

  3.    14 Aug 2008 #3
    Al Dunbar Guest

    Re: replacing text on multiple text files



    "chufa72" <chufa72@xxxxxx> wrote in message
    news:179CCD9C-E0EF-467B-8298-46734DC712FA@xxxxxx

    > 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 wall.
    > The
    > ways I've tried and failed only work on a single text file at a time.
    > Thanks,
    If you have code that works for a single file, all you need to do is to put
    that into a loop where you supply a different filename each time. If your
    code looks like this:

    file2change = "C:\test\file1.txt"
    rem - your code to modify the file

    change it to this:

    for each file2change in
    array("C:\test\file1.txt","C:\test\file2.txt","C:\test\file3.txt")
    rem - your code to modify the file
    next

    This still only does one file at a time, as it does not start processing the
    second file until the first has been done. But at least it will process
    multiple files in one run of the script.

    Depending on the particulars of your situation there could be better ways to
    organize this, the most obvious being to pass as a parameter to your script
    the name of a file that contains a list of files to be processed. Without
    knowing precisely the situation or the code you have working, though, it is
    hard to be more specific.

    /Al


      My System SpecsSystem Spec

  4.    14 Aug 2008 #4
    chufa72 Guest

    Re: replacing text on multiple text files


    I have tried this code, which is a chimera from several ones in the
    depository (pardon my ignorance, I am completely newbish).

    Const OverwriteExisting = TRUE

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
    objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

    Set FileList = objWMIService.ExecQuery _
    ("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
    & "ResultClass = CIM_DataFile")

    For Each objFile In FileList
    If objFile.Extension = "txt" Then
    strNewName = objFile.Drive & objFile.Path & _
    objFile.FileName & "." & "pcr"
    errResult = objFile.Rename(strNewName)
    End If
    Next
    Const FOR_READING = 1
    Const FOR_WRITING = 2
    strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where it
    fails)

    Set objFS = CreateObject("Scripting.FileSystemObject")
    Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)

    "Todd Vargo" wrote:

    > chufa72 wrote:

    > > 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 wall.
    > The

    > > ways I've tried and failed only work on a single text file at a time.
    > Thanks,
    >
    > This may be a dumb question, but what ways did you try for multiple files?
    > We can not tell why you hit a brick wall if we can not see the code you
    > used.
    >
    > --
    > Todd Vargo
    > (Post questions to group only. Remove "z" to email personal messages)
    >
    >
      My System SpecsSystem Spec

  5.    15 Aug 2008 #5
    Todd Vargo Guest

    Re: replacing text on multiple text files



    "chufa72" <chufa72@xxxxxx> wrote in message
    news:FE567A4B-1E26-40A4-958B-3FDF5AA98183@xxxxxx

    > I have tried this code, which is a chimera from several ones in the
    > depository (pardon my ignorance, I am completely newbish).
    >
    > Const OverwriteExisting = TRUE
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
    > objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
    > strComputer = "."
    > Set objWMIService = GetObject("winmgmts:" _
    > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    >
    > Set FileList = objWMIService.ExecQuery _
    > ("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
    > & "ResultClass = CIM_DataFile")
    >
    > For Each objFile In FileList
    > If objFile.Extension = "txt" Then
    > strNewName = objFile.Drive & objFile.Path & _
    > objFile.FileName & "." & "pcr"
    > errResult = objFile.Rename(strNewName)
    > End If
    > Next
    > Const FOR_READING = 1
    > Const FOR_WRITING = 2
    > strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where
    it

    > fails)
    >
    > Set objFS = CreateObject("Scripting.FileSystemObject")
    > Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
    Unfortunately, not enough information was given. Assuming from the code
    above, it appears you want to create a temp folder, copy all .txt files to
    it, rename the .txt files in temp folder to .pcr extension, then open
    for_writing to the oldname in the TEMP folder. The following code does that.

    Const FOR_READING = 1
    Const FOR_WRITING = 2
    Const OverwriteExisting = TRUE
    TEMP = "C:\scripts\temp"

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    If objFSO.FolderExists(TEMP) Then objFSO.DeleteFolder(TEMP)
    Set objFolder = objFSO.CreateFolder(TEMP)
    objFSO.CopyFile "C:\scripts\*.txt", TEMP, OverwriteExisting

    For Each objFile In objFSO.GetFolder("E:\scripts\temp").Files
    'Condition below is not really needed because
    ' the TEMP folder only contains .txt files.
    'If objFSO.GetExtensionName(objFile.Name) = "txt" Then
    strOldName = objFile.Path
    strNewName = Left(strOldName, InstrRev(strOldName, ".")) & "pcr"
    objFSO.MoveFile strOldName, strNewName
    Set objTS = objFSO.OpenTextFile(strOldName, FOR_WRITING, True)
    objTS.Write "What do you want to write here?"
    'End If
    Next

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

      My System SpecsSystem Spec

  6.    15 Aug 2008 #6
    chufa72 Guest

    Re: replacing text on multiple text files


    Thank you very much for your reply. Basically what I wanted to do is for
    every text file in the working folder, create a new one with the same file
    name, but extension .pcr instead of .txt. The contents of this .pcr file
    would be its file name written four times, in four different lines. Sounds
    complicated, I know.

    The way I was going to go about it was, like you said, to create a temp
    folder, copy all .txt files to it, rename the .txt files in temp folder to
    ..pcr extension, then (and this is where I got stuck) replacing the text
    inside.

    Thanks,

    David

    "Todd Vargo" wrote:

    >
    > "chufa72" <chufa72@xxxxxx> wrote in message
    > news:FE567A4B-1E26-40A4-958B-3FDF5AA98183@xxxxxx

    > > I have tried this code, which is a chimera from several ones in the
    > > depository (pardon my ignorance, I am completely newbish).
    > >
    > > Const OverwriteExisting = TRUE
    > >
    > > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > > Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
    > > objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
    > > strComputer = "."
    > > Set objWMIService = GetObject("winmgmts:" _
    > > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    > >
    > > Set FileList = objWMIService.ExecQuery _
    > > ("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
    > > & "ResultClass = CIM_DataFile")
    > >
    > > For Each objFile In FileList
    > > If objFile.Extension = "txt" Then
    > > strNewName = objFile.Drive & objFile.Path & _
    > > objFile.FileName & "." & "pcr"
    > > errResult = objFile.Rename(strNewName)
    > > End If
    > > Next
    > > Const FOR_READING = 1
    > > Const FOR_WRITING = 2
    > > strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where
    > it

    > > fails)
    > >
    > > Set objFS = CreateObject("Scripting.FileSystemObject")
    > > Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
    >
    > Unfortunately, not enough information was given. Assuming from the code
    > above, it appears you want to create a temp folder, copy all .txt files to
    > it, rename the .txt files in temp folder to .pcr extension, then open
    > for_writing to the oldname in the TEMP folder. The following code does that.
    >
    > Const FOR_READING = 1
    > Const FOR_WRITING = 2
    > Const OverwriteExisting = TRUE
    > TEMP = "C:\scripts\temp"
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > If objFSO.FolderExists(TEMP) Then objFSO.DeleteFolder(TEMP)
    > Set objFolder = objFSO.CreateFolder(TEMP)
    > objFSO.CopyFile "C:\scripts\*.txt", TEMP, OverwriteExisting
    >
    > For Each objFile In objFSO.GetFolder("E:\scripts\temp").Files
    > 'Condition below is not really needed because
    > ' the TEMP folder only contains .txt files.
    > 'If objFSO.GetExtensionName(objFile.Name) = "txt" Then
    > strOldName = objFile.Path
    > strNewName = Left(strOldName, InstrRev(strOldName, ".")) & "pcr"
    > objFSO.MoveFile strOldName, strNewName
    > Set objTS = objFSO.OpenTextFile(strOldName, FOR_WRITING, True)
    > objTS.Write "What do you want to write here?"
    > 'End If
    > Next
    >
    > --
    > Todd Vargo
    > (Post questions to group only. Remove "z" to email personal messages)
    >
    >
      My System SpecsSystem Spec

  7.    15 Aug 2008 #7
    chufa72 Guest

    Re: replacing text on multiple text files


    By the way, I forgot to mention in my previous reply that the code you posted
    works, thanks!

    I do have a follow-up question, though: is it possible to make it work at a
    variable current folder it is in rather than a fixed location (c:\scripts)? I
    realize now that in practice, this script would be executed at many different
    folders and not just c:\scripts.

    "Todd Vargo" wrote:

    >
    > "chufa72" <chufa72@xxxxxx> wrote in message
    > news:FE567A4B-1E26-40A4-958B-3FDF5AA98183@xxxxxx

    > > I have tried this code, which is a chimera from several ones in the
    > > depository (pardon my ignorance, I am completely newbish).
    > >
    > > Const OverwriteExisting = TRUE
    > >
    > > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > > Set objFolder = objFSO.CreateFolder("C:\scripts\temp")
    > > objFSO.CopyFile "C:\scripts\*.txt" , "C:\scripts\temp" , OverwriteExisting
    > > strComputer = "."
    > > Set objWMIService = GetObject("winmgmts:" _
    > > & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
    > >
    > > Set FileList = objWMIService.ExecQuery _
    > > ("ASSOCIATORS OF {Win32_Directory.Name='C:\scripts\temp'} Where " _
    > > & "ResultClass = CIM_DataFile")
    > >
    > > For Each objFile In FileList
    > > If objFile.Extension = "txt" Then
    > > strNewName = objFile.Drive & objFile.Path & _
    > > objFile.FileName & "." & "pcr"
    > > errResult = objFile.Rename(strNewName)
    > > End If
    > > Next
    > > Const FOR_READING = 1
    > > Const FOR_WRITING = 2
    > > strFileName = "C:\scripts\temp\*.txt" (comment: this right here is where
    > it

    > > fails)
    > >
    > > Set objFS = CreateObject("Scripting.FileSystemObject")
    > > Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING)
    >
    > Unfortunately, not enough information was given. Assuming from the code
    > above, it appears you want to create a temp folder, copy all .txt files to
    > it, rename the .txt files in temp folder to .pcr extension, then open
    > for_writing to the oldname in the TEMP folder. The following code does that.
    >
    > Const FOR_READING = 1
    > Const FOR_WRITING = 2
    > Const OverwriteExisting = TRUE
    > TEMP = "C:\scripts\temp"
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    > If objFSO.FolderExists(TEMP) Then objFSO.DeleteFolder(TEMP)
    > Set objFolder = objFSO.CreateFolder(TEMP)
    > objFSO.CopyFile "C:\scripts\*.txt", TEMP, OverwriteExisting
    >
    > For Each objFile In objFSO.GetFolder("E:\scripts\temp").Files
    > 'Condition below is not really needed because
    > ' the TEMP folder only contains .txt files.
    > 'If objFSO.GetExtensionName(objFile.Name) = "txt" Then
    > strOldName = objFile.Path
    > strNewName = Left(strOldName, InstrRev(strOldName, ".")) & "pcr"
    > objFSO.MoveFile strOldName, strNewName
    > Set objTS = objFSO.OpenTextFile(strOldName, FOR_WRITING, True)
    > objTS.Write "What do you want to write here?"
    > 'End If
    > Next
    >
    > --
    > Todd Vargo
    > (Post questions to group only. Remove "z" to email personal messages)
    >
    >
      My System SpecsSystem Spec

  8.    15 Aug 2008 #8
    Todd Vargo Guest

    Re: replacing text on multiple text files



    chufa72 wrote:

    > By the way, I forgot to mention in my previous reply that the code you
    posted

    > works, thanks!
    >
    > I do have a follow-up question, though: is it possible to make it work at
    a

    > variable current folder it is in rather than a fixed location
    (c:\scripts)? I

    > realize now that in practice, this script would be executed at many
    different

    > folders and not just c:\scripts.
    The following script will operate on files in whatever folder is current and
    eliminates creation of the TEMP folder.

    Const FOR_READING = 1
    Const FOR_WRITING = 2
    Const OverwriteExisting = TRUE

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    For Each objFile In objFSO.GetFolder(".").Files
    If objFSO.GetExtensionName(objFile.Name) = "txt" Then
    strNewName = objFSO.GetBaseName(objFile.Name) & ".pcr"
    Set objTS = objFSO.OpenTextFile(strNewName, FOR_WRITING,
    OverwriteExisting)
    For i = 1 to 4
    objTS.WriteLine objFSO.GetBaseName(objFile.Name)
    Next
    End If
    Next

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

      My System SpecsSystem Spec

  9.    15 Aug 2008 #9
    chufa72 Guest

    Re: replacing text on multiple text files


    Thank you very much! Works like a charm.

    "Todd Vargo" wrote:

    >
    > chufa72 wrote:

    > > By the way, I forgot to mention in my previous reply that the code you
    > posted

    > > works, thanks!
    > >
    > > I do have a follow-up question, though: is it possible to make it work at
    > a

    > > variable current folder it is in rather than a fixed location
    > (c:\scripts)? I

    > > realize now that in practice, this script would be executed at many
    > different

    > > folders and not just c:\scripts.
    >
    > The following script will operate on files in whatever folder is current and
    > eliminates creation of the TEMP folder.
    >
    > Const FOR_READING = 1
    > Const FOR_WRITING = 2
    > Const OverwriteExisting = TRUE
    >
    > Set objFSO = CreateObject("Scripting.FileSystemObject")
    >
    > For Each objFile In objFSO.GetFolder(".").Files
    > If objFSO.GetExtensionName(objFile.Name) = "txt" Then
    > strNewName = objFSO.GetBaseName(objFile.Name) & ".pcr"
    > Set objTS = objFSO.OpenTextFile(strNewName, FOR_WRITING,
    > OverwriteExisting)
    > For i = 1 to 4
    > objTS.WriteLine objFSO.GetBaseName(objFile.Name)
    > Next
    > End If
    > Next
    >
    > --
    > Todd Vargo
    > (Post questions to group only. Remove "z" to email personal messages)
    >
    >
      My System SpecsSystem Spec

replacing text on 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
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...
VB Script
Replacing text
I need to replace the first 15 characters of a string with another string, i.e. change a prefix. I thought replace would do this, but I can't see...
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 in ascii file
I'm attempting to replace text in a file with the command, Get-Content -encoding ascii test.txt | ForEach { $_ -replace...
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 02:58.
    .