Jump to content

Welcome to MSFN Forum
Register now to gain access to all of our features. Once registered and logged in, you will be able to create topics, post replies to existing threads, give reputation to your fellow members, get your own private messenger, post status updates, manage your profile and so much more. This message will be removed once you have signed in.
Login to Account Create an Account


Photo

Reading and Writing to txt file

- - - - -

  • Please log in to reply
2 replies to this topic

#1
elyfrank

elyfrank
  • Member
  • 1 posts
  • Joined 14-September 12
  • OS:none specified
  • Country: Country Flag
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



How to remove advertisement from MSFN

#2
jumper

jumper

    2014 All-American Masters HJ'er

  • Member
  • PipPipPip
  • 476 posts
  • Joined 21-January 11
  • OS:98SE
  • Country: Country Flag
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.c...textstream.html
http://www.devguru.c...ref/doloop.html
Design feedback requested:
IHAtool - IpHlpApi tester; call various functions and report results
--status-> framework is solid; 22 api's fully supported; preview release coming soon
ComDlg32 wrapper - ComDlgEx meets IpHlpApi wrapper
--status-> PrintDlgExW working in latest SumatraPDF 8^)
Future projects: ImportPatcher40 - dialog interface; Kexter - IP40+Ktree+Kexstubs

#3
gunsmokingman

gunsmokingman

    MSFN Master

  • Super Moderator
  • 2,418 posts
  • Joined 02-August 03
  • OS:none specified
  • Country: Country Flag
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




GunSmokingMan






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users



How to remove advertisement from MSFN