Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading then copying one line from multiple files to mulitiple files

Status
Not open for further replies.

CarlCrusher

Technical User
Oct 30, 2009
2
US
Hi,

I am not sure if this is even possible but I thought I may ask anyway.

I have dozens of files that have different extensions (but all text based). I need to find and copy a specific line within these files and paste them into files with the same extension.

ie.

Files with lines that need to be found then copied

Test.user.johnsmith
Test.user.janedoe
Test.user.mikejones

(the line that needs to be found begins with "Middle"

The files that need to have the "middle" line appended to it are:

Final.user.johnsmith
Final.user.janedoe
Final.user.mikejones

In an added complication the "Final" files may already contain a line that begins with "Middle" in these cases these lines need to be replace.

Does anyone have any suggestions? Is this even possible?

Thanks in advanced.
 
I'm just being overly observant but it sounds to me like a class assignment you are trying to get the answer for. What makes me say this? The constant "Is this even possible", the ultra specific and impractical program requirements, plus the fact that you JUST became a member for this thread. Nonetheless, I love to help people so I will offer a construct.

Code:
set objFSO = wscript.CreateObject("Scripting.FileSystemObject")
set objFiles = objFSO.GetFolder("C:\path\").Files

strSearch = "Middle"
for each objFile in objFiles
   set objStream = objFSO.OpenTextFile(objFile.Path, 1)
   do while not (objStream.AtEndOfStream)
      strLine = objStream.ReadLine
      if (inStr(strLine, strSearch)) then
         strFinalFile = objFile.Path & replace(objFile.name, "test", "final")
         set objFinal = objFSO.OpenTextFile(strFinalFile)
         strContent = ""
         boolReplaced = false
         do while not (objFinal.AtEndOfStream)
            strLine2 = objFinal.ReadLine
            if (inStr(strLine2, strLine)) then
               strContent = strContent & strLine & vbNewline
               boolReplaced = true
            else
               strContent = strContent & strLine2 & vbNewline
            end if
         loop
         objFinal.Close
         if (boolReplaced = false) then strContent = strContent & strLine
         set objOutput = objFSO.OpenTextFile(strFinalFile, 2, true)
         objOutput.Write strContent
         objOutput.Close
         exit loop
      end if
   loop
next

Or something like that. Hope you get an A!

- Geates
[/code]
 
Thanks,

Im going to give this a shot.

You are being overly observant. These files are configuration files for a program we have at work and I would like to be able to copy a similar config from one config file to another. Without a script I would have to copy and paste each configuration line from one file to another for hundreds of users.

While this is a great first step I still need to try to make an HTA out of this so my coworkers can do this simply without having to modify this script.

 
where are the files located?

you need to seperate out all of your tasks and tackle then one at a time.

1. find a collection of the files you are reading
2. read the contents of each file you are interested into memory.
3. find a collction of the target files you are writing to
4. read the contents of a target file into memory
5. manip the target file in memory
6. write out manip'd target file

with regards the HTA, what is it purpose? why would anyone need to modify the script? what data are you planning on holding in the script which needs to be modified?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top