elyfrank Posted September 14, 2012 Share Posted September 14, 2012 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 Link to comment Share on other sites More sharing options...
jumper Posted September 15, 2012 Share Posted September 15, 2012 Try replacing: content = txs.ReadAllwith: content = txs.ReadLine & vbCrLf & "MyDisclaimer:" & vbCrLf Do Until txs.AtEndOfStream ' while not at end of file content = content & vbCrLf & txs.ReadLine & vbCrLf LoopAnd: txs.Write "MyDisclaimer:" & vbCrLf & content with: txs.Write content References:http://www.devguru.com/technologies/vbscript/quickref/textstream.htmlhttp://www.devguru.com/technologies/vbscript/quickref/doloop.html Link to comment Share on other sites More sharing options...
gunsmokingman Posted September 15, 2012 Share Posted September 15, 2012 I modified your script so it adds text every 2 line, I also change the filter for testingChange FromIf Right(fil.Name, 3) = "pat" Then Change ToIf Right(LCase(fil),3) = "txt" ThenVBS ScriptDim 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 NextExample Pat.txt Before ScriptPat Line 1 TextPat Line 2 TextPat Line 3 TextPat Line 4 TextExample Pat.txt After ScriptPat Line 1 TextMyDisclaimer:Pat Line 2 TextMyDisclaimer:Pat Line 3 TextMyDisclaimer:Pat Line 4 Text Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now