![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| 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) |
| 64 | 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 Specs![]() |
| | #2 (permalink) |
| | Re: Replace text in multiple text files Quote: > > 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. > 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: Quote: > For i = 10 to (Ubound(arrLines)) 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. Quote: > 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 Specs![]() |
| | #3 (permalink) |
| | Re: Replace text in multiple text files gurushab wrote: Quote: > > 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 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 Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Remove lines from multiple text files | PowerShell | |||
| Newbie question: replace multiple strings in multiple text files | VB Script | |||
| replacing text on multiple text files | VB Script | |||
| How to search and replace text in a string | PowerShell | |||
| Convert Unix text files to PC text files | PowerShell | |||