• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

Replace text in multiple text files

gurushab

New Member
Posts
1
#1
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 Computer

M

mayayana

#2

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

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

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

T

Todd Vargo

#3
gurushab wrote:

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