![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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 Specs![]() |
| | #2 (permalink) |
| | Re: replacing text on multiple text files chufa72 wrote: Quote: > Hello. I am looking for a way to automatically replace text on multiple Quote: > files. I am writing my first script for work, but I've hit a brick wall. Quote: > ways I've tried and failed only work on a single text file at a time. 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 Specs![]() |
| | #3 (permalink) |
| | Re: replacing text on multiple text files "chufa72" <chufa72@xxxxxx> wrote in message news:179CCD9C-E0EF-467B-8298-46734DC712FA@xxxxxx Quote: > 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, 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 Specs![]() |
| | #4 (permalink) |
| | 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: Quote: > chufa72 wrote: Quote: > > Hello. I am looking for a way to automatically replace text on multiple Quote: > > files. I am writing my first script for work, but I've hit a brick wall. Quote: > > ways I've tried and failed only work on a single text file at a time. > > 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 Specs![]() |
| | #5 (permalink) |
| | Re: replacing text on multiple text files "chufa72" <chufa72@xxxxxx> wrote in message news:FE567A4B-1E26-40A4-958B-3FDF5AA98183@xxxxxx Quote: > 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 Quote: > fails) > > Set objFS = CreateObject("Scripting.FileSystemObject") > Set objTS = objFS.OpenTextFile(strFileName, FOR_WRITING) 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 Specs![]() |
| | #6 (permalink) |
| | 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: Quote: > > "chufa72" <chufa72@xxxxxx> wrote in message > news:FE567A4B-1E26-40A4-958B-3FDF5AA98183@xxxxxx Quote: > > 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 Quote: > > 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 Specs![]() |
| | #7 (permalink) |
| | 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: Quote: > > "chufa72" <chufa72@xxxxxx> wrote in message > news:FE567A4B-1E26-40A4-958B-3FDF5AA98183@xxxxxx Quote: > > 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 Quote: > > 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 Specs![]() |
| | #8 (permalink) |
| | Re: replacing text on multiple text files chufa72 wrote: Quote: > By the way, I forgot to mention in my previous reply that the code you Quote: > works, thanks! > > I do have a follow-up question, though: is it possible to make it work at Quote: > variable current folder it is in rather than a fixed location Quote: > realize now that in practice, this script would be executed at many Quote: > folders and not just c:\scripts. 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 Specs![]() |
| | #9 (permalink) |
| | Re: replacing text on multiple text files Thank you very much! Works like a charm. "Todd Vargo" wrote: Quote: > > chufa72 wrote: Quote: > > By the way, I forgot to mention in my previous reply that the code you Quote: > > works, thanks! > > > > I do have a follow-up question, though: is it possible to make it work at Quote: > > variable current folder it is in rather than a fixed location Quote: > > realize now that in practice, this script would be executed at many Quote: > > 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Replace text in multiple text files | VB Script | |||
| Replacing text | PowerShell | |||
| Remove lines from multiple text files | PowerShell | |||
| Newbie question: replace multiple strings in multiple text files | VB Script | |||
| Replacing text in ascii file | PowerShell | |||