• Announcements

    • xper

      MSFN Sponsorship and AdBlockers!   07/10/2016

      Dear members, MSFN is made available via subscriptions, donations and advertising revenue. The use of ad-blocking software hurts the site. Please disable ad-blocking software or set an exception for MSFN. Alternatively, become a site sponsor and ads will be disabled automatically and by subscribing you get other sponsor benefits.
elyfrank

Reading and Writing to txt file

3 posts in this topic

Hi guys,

I need some help with this script.

This script is working right, it reads all the .txt files in a folder and add content in the first line.

I just need to modify it so I can add the content after the 1st line of the text files.

Thank you very much

file.txt example:

line 1

my text goes here

line 2

line ...

vbs script:

 
Dim FSO, txs, fld, fil, content
Set FSO = CreateObject("Scripting.FileSystemObject")

Set fld = FSO.GetFolder("C:\test\")
For Each fil In fld.Files
If Right(fil.Name, 3) = "pat" Then

Set txs = fil.OpenAsTextStream(1) ' 1 = for reading
content = txs.ReadAll
txs.Close

Set txs = fil.OpenAsTextStream(2) ' 2 = for writing
txs.Write "MyDisclaimer:" & vbCrLf & content
txs.Close

End If
Next

0

Share this post


Link to post
Share on other sites

Try replacing:

        content = txs.ReadAll

with:

        content = txs.ReadLine & vbCrLf & "MyDisclaimer:" & vbCrLf 
Do Until txs.AtEndOfStream ' while not at end of file
content = content & vbCrLf & txs.ReadLine & vbCrLf
Loop

And:

        txs.Write "MyDisclaimer:" & vbCrLf  & content 

with:

        txs.Write content 

References:


http://www.devguru.com/technologies/vbscript/quickref/textstream.html
http://www.devguru.com/technologies/vbscript/quickref/doloop.html

0

Share this post


Link to post
Share on other sites

I modified your script so it adds text every 2 line, I also change the filter for testing

Change From

If Right(fil.Name, 3) = "pat" Then

Change To

If Right(LCase(fil),3) = "txt" Then

VBS Script


Dim C1, FSO, txs, fld, fil, content

Set FSO = CreateObject("Scripting.FileSystemObject")

Set fld = FSO.GetFolder("C:\test\")


For Each fil In fld.Files
If Right(LCase(fil),3) = "txt" Then
Set txs = fil.OpenAsTextStream(1) ' 1 = for reading
'-> Loop Threw The Text File And Add New Contents
Do until txs.AtEndOfStream = True
C1 = C1 + 1
If C1 Mod 2 Then
content = content & txs.ReadLine & vbCrLf
Else
content = content & "MyDisclaimer:" & vbCrLf & txs.ReadLine
End If
Loop
txs.Close
'-> Rebuild The Text File With New Contents
Set txs = fil.OpenAsTextStream(2) ' 2 = for writing
txs.Write content
txs.Close
End If
Next

Example Pat.txt Before Script

Pat Line 1 Text

Pat Line 2 Text

Pat Line 3 Text

Pat Line 4 Text

Example Pat.txt After Script

Pat Line 1 Text

MyDisclaimer:

Pat Line 2 Text

MyDisclaimer:

Pat Line 3 Text

MyDisclaimer:

Pat Line 4 Text

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.