Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Replace text in multiple text files

Reply
 
Old 08-22-2009   #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 SpecsSystem Spec
Old 08-22-2009   #2 (permalink)
mayayana


 
 

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.
>
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:
Quote:

> 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.
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 SpecsSystem Spec
Old 08-22-2009   #3 (permalink)
Todd Vargo


 
 

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
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
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46