MSFN Forum: Reading and Writing to txt file - MSFN Forum

Jump to content


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Reading and Writing to txt file trying to skip first line when writing to a text file Rate Topic: -----

#1 User is offline   elyfrank 

  • Group: Members
  • Posts: 1
  • Joined: 14-September 12
  • OS:none specified
  • Country: Country Flag

Posted 14 September 2012 - 12:05 PM

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




#2 User is offline   jumper 

  • Masters HJ/TJ'er (back in training)
  • PipPipPip
  • Group: Members
  • Posts: 361
  • Joined: 21-January 11
  • OS:98SE
  • Country: Country Flag

Posted 15 September 2012 - 03:35 AM

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:

#3 User is offline   gunsmokingman 

  • MSFN Master
  • Group: Super Moderator
  • Posts: 2,352
  • Joined: 02-August 03
  • OS:none specified
  • Country: Country Flag

Posted 15 September 2012 - 01:07 PM

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

Quote

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

Change To

Quote

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

Quote

Pat Line 1 Text
Pat Line 2 Text
Pat Line 3 Text
Pat Line 4 Text


Example Pat.txt After Script

Quote

Pat Line 1 Text
MyDisclaimer:
Pat Line 2 Text
MyDisclaimer:
Pat Line 3 Text
MyDisclaimer:
Pat Line 4 Text


Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2013 msfn.org
Privacy Policy