![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Howto: Add lines of text from a specific point in a text file.. Hi all, I wonder if you can help me... I have been doing allot of manual updates to ini files that i am using and would like to make things a little easier on myself by trying to automate as much as i can. What i need at the moment is a way of adding some addition text to a file that already exists - the catch is that i need to add it to the end of a group of information and that information starts around the middle of the file. E.g. below. E.g. --- SVCPack.inf --- [CatalogHeader] [Version] BuildNumber=2600 MinorVersion=1 MajorVersion=5 Signature="$WINDOWS NT$" [SetupData] CatalogSubDir="\I386\svcpack" [SetupHotfixesToRun] KB892130.exe /q /n /z wmp11.exe /q /n /z /o wmfdist11.exe /q /n /z /o mscomppackv1.exe /q /n /z /o wudf01000.exe /q /n /z /o [ProductCatalogsToInstall] KB892130.cat wmp11.cat WMFDist11.cat MSCompPackV1.cat Wudf01000.cat --- End file --- So i would like to be able to add additional commands (lines of text) to the [SetupHotfixesToRun] section of the file by running a single vbscript. The text/instructions i would like to be able to have listed in the vbs (or a comma delimited file that can be called from the vbs) I would appreciate any suggestions, code snippets etc that can point me in the write direction. Cheers, Darren |
My System Specs![]() |
| | #2 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. "Daz" <darren.blackley@xxxxxx> wrote in message news:d37aebf2-7853-417c-ab95-5fd7f390dd5a@xxxxxx Quote: > Hi all, > > I wonder if you can help me... I have been doing allot of manual > updates to ini files that i am using and would like to make things a > little easier on myself by trying to automate as much as i can. What > i need at the moment is a way of adding some addition text to a file > that already exists - the catch is that i need to add it to the end of > a group of information and that information starts around the middle > of the file. E.g. below. > Quote: > > So i would like to be able to add additional commands (lines of text) > to the [SetupHotfixesToRun] section of the file by running a single > vbscript. The text/instructions i would like to be able to have > listed in the vbs (or a comma delimited file that can be called from > the vbs) Are you working with multiple files? Is the INF file hundreds of lines long or are there dozens of [sections]? If not, what's wrong with using Notepad to paste in your new lines? How often is this done? What will be the savings if it's automated? |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. > Are the additional lines in a text file? The example above is a classic example and could have different entries and more or less entries in each section. As for additional lines, yes it would as the script solution i am looking for would add these additonal lines - so in the above example, the script would add the additional entries after "wudf01000.exe /q /n /z /o" Quote: > Are you working with multiple files? Quote: > Is the INF file hundreds of lines long or are there dozens of [sections]? no more sections than shown above, however the contents of each section can grow in number. Quote: > If not, what's wrong with using Notepad to paste in your new lines? configuration, however they all use the same ini file name. So as i said originally i am trying to make things a little more manageable. Quote: > How often is this done? *What will be the savings if it's automated? number of utilities etc are often updated. Hope this answers all your questions. |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. Il giorno Mon, 23 Jun 2008 01:55:59 -0700 (PDT), Daz <darren.blackley@xxxxxx> ha scritto: Quote: >Hi all, > >I wonder if you can help me... I have been doing allot of manual >updates to ini files that i am using and would like to make things a >little easier on myself by trying to automate as much as i can. What >i need at the moment is a way of adding some addition text to a file >that already exists - the catch is that i need to add it to the end of >a group of information and that information starts around the middle >of the file. E.g. below. reads Yoursection writes additional data. percorso="C:\mydir\myfile.txt" Const ForReading = 1 'Open a file for reading only. You can't write to this file Const ForWriting = 2 'Open a file for writing q= chr(34) 'virgolette Set fso = CreateObject("Scripting.FileSystemObject") on error resume next fso.DeleteFile percorso & ".BAK" on error goto 0 fso.MoveFile percorso ,percorso & ".BAK" set source = fso.OpenTextFile(percorso & .BAK",ForReading) set dest= fso.OpenTextFile(percorso ,ForWriting,TRUE) Do While source.AtEndOfStream <> True riga = source.ReadLine if instr(1,riga,"[yoursection]")>0 then dest.writeline "[yoursection]" & vbcrlf & "................" else dest.writeline riga end if Loop source.Close dest.close End Sub -- Giovanni Cenati (Bergamo, Italy) Write to "Reventlov" at katamail com http://digilander.libero.it/Cenati (Esempi e programmi in VbScript) -- |
My System Specs![]() |
| | #5 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. > What I meant was: where do lines to be added exist? "The text/instructions i would like to be able to have listed in the vbs (or a comma delimited file that can be called from the vbs)" Quote: > > "they all use the same ini file name" -- sre the files in differnet folders > and/or on different systems? > > Sorry, I'm still trying to gather your requirements. > > It's just not very clear (to me) how you envision an automated > effort to work. > > Do you have a list of filenames with their paths from which to select? > Where are the additional lines -- in a text file or known when needed. I would like to add something like; 7z457.exe /S AC3.exe /S AutoIt32100.exe /S JRE6.exe /quiet /norestart Sysinternals.exe Wget.exe INSTALL_FLASH_PLAYER.EXE /silent WindowsXP-KB923789-x86-ENU.exe /Q:A /R:N DX.exe these entries would be appended to the [SetupHotfixesToRun] section like e.g. below [SetupHotfixesToRun] KB892130.exe /q /n /z wmp11.exe /q /n /z /o wmfdist11.exe /q /n /z /o mscomppackv1.exe /q /n /z /o wudf01000.exe /q /n /z /o ; Added below 7z457.exe /S AC3.exe /S AutoIt32100.exe /S JRE6.exe /quiet /norestart Sysinternals.exe Wget.exe INSTALL_FLASH_PLAYER.EXE /silent WindowsXP-KB923789-x86-ENU.exe /Q:A /R:N DX.exe Quote: > > I developed an HTA (= VBS + a UI) that allows entry of a filename > (which must exist in the HTA's folder); the contents of the file are > displayed in a <textarea> and may be changed and saved. > > An enhancement would be to have a list of path+filenames; > the selection of one would cause it to be displayed in a <textarea> > and a list of the [sections] within it could be displayed as a > dropdown selection list. Another <textarea> would allow for > entry of the "additional lines". *You would then select a > [section] from the list and click a button called "append" to > append the "additional lines" to the end of the selected [section]. > Alternatively, you could manually insert additional lines > where ever you wanted. *You would then click a "save" button. > > Your thoughts? able to add a command-line argument to the entry) and then after creating an array add this to the [SetupHotfixesToRun]. |
My System Specs![]() |
| | #6 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. > "Daz" <darren.blackley@xxxxxx> wrote in message news:2e7038f4-119c-49a9-885f-> bfa16d8e12f2@xxxxxx... Quote: Quote: > > What I meant was: where do lines to be added exist? > "The text/instructions i would like to be able to have > listed in the vbs (or a comma delimited file that can be called from > the vbs)" > Quote: > > > > "they all use the same ini file name" -- sre the files in differnet Quote: Quote: > > and/or on different systems? > > > > Sorry, I'm still trying to gather your requirements. > > > > It's just not very clear (to me) how you envision an automated > > effort to work. > > > > Do you have a list of filenames with their paths from which to select? > > Where are the additional lines -- in a text file or known when needed. > Sorry about that - i will try to give you further examples. Quote: Quote: > > I developed an HTA (= VBS + a UI) that allows entry of a filename > > (which must exist in the HTA's folder); the contents of the file are > > displayed in a <textarea> and may be changed and saved. > > > > An enhancement would be to have a list of path+filenames; > > the selection of one would cause it to be displayed in a <textarea> > > and a list of the [sections] within it could be displayed as a > > dropdown selection list. Another <textarea> would allow for > > entry of the "additional lines". You would then select a > > [section] from the list and click a button called "append" to > > append the "additional lines" to the end of the selected [section]. > > Alternatively, you could manually insert additional lines > > where ever you wanted. You would then click a "save" button. > > > > Your thoughts? > Sound great! - then i could select the file i want (would need to be > able to add a command-line argument to the entry) and then after > creating an array add this to the [SetupHotfixesToRun]. "would need to be able to add a command-line argument to the entry" Why/how would a "comma delimited file" be used for the additional lines? Do you have a list of filenames with their paths from which to select? I don't need them, I just want to know if you have them in a list? |
My System Specs![]() |
| | #7 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. "McKirahan" <News@xxxxxx> wrote in message news:scidnd3zCclfOcLVnZ2dnUVZ_rfinZ2d@xxxxxx Quote: Quote: > > "Daz" <darren.blackley@xxxxxx> wrote in message [snip] I don't have answers to all of my questions but here's something. Will this help? Watch for word-wrap. Save it as "SVCPack.hta" then create "SVCPack.txt" which contains a list of files (with their full path; one per line) that you may want to edit. For example, "C:\Temp\SVCPack.inf". To run it, doubleclick on "SVCPack.hta" (in Windows Explorer), select a file by clicking on its name and its contents will be displayed in the text area. You can now manually edit the file or enter one or more lines of text which you can insert at the cursor location. After making your changes click "Replace" to backup the file then replace it. <html> <head> <title>SVCPack.hta</title> <HTA:Application ID = "HTA" ApplicationName = "SVCPack" Border = "thin" BorderStyle = "normal" Caption = "yes" ContextMenu="yes" Icon = "" InnerBorder="no" MaximizeButton = "yes" MinimizeButton = "yes" Navigable="yes" Scroll="yes" SrollFlat="no" Selection="yes" ShowInTaskBar = "yes" SingleInstance = "yes" SysMenu = "yes" Version = "1.0" WindowState = "maximize" Quote: > Option Explicit '**** '* This HTA (HTML Application) does the following: '* 1) Reads "cTXT" which contains a list of (path+) filenames '* and builds a dropdown list to allow selection. '* 2) Reads the selected file into a textarea; '* (".inf" extensions builds a dropdown list of Sections). '* 3) Changes may be made directly to the file or lines '* may entered in a textarea for inserting into multiple files. '* 4) If the file has been changed then it may be saved which '* replaces the original file after it is "backed up" first by '* copying it with a timestamp extension: ".yyyymmddhhnnss". '**** '* '* Declare Constants '* Const cHTA = "SVCPack.hta" Const cTXT = "SVCPack.txt" '* '* Declare Globals '* Dim sDIR sDIR = CreateObject("WScript.Shell").CurrentDirectory + "\" Dim sERR Dim oFSO Set oFSO = CreateObject("Scripting.FileSystemObject") Dim sINF Dim dNOW Dim sNOW ' yyyymmddhhnnss Dim aOTF Dim iOTF Dim oOTF Dim sOTF Dim iOPT Dim sOPT Dim sSAV Dim sTMP Sub File_List() '**** '* Read file. '**** '* '* Test '* If Not oFSO.FileExists(sDIR & cTXT) Then sERR = "<b>" & cTXT & "</b> does not exist!" document.getElementById("Note").innerHTML = sERR Exit Sub End If '* '* Read '* Set oOTF = oFSO.OpenTextFile(sDIR & cTXT,1) sOTF = oOTF.ReadAll Set oOTF = Nothing '* '* List '* iOPT = 1 sOPT = "<option value=''>" & vbCrLf aOTF = Split(sOTF,vbCrLf) For iOTF = 0 To UBound(aOTF) sOTF = Trim(aOTF(iOTF)) If LCase(Left(sOTF,1)) >= "a" _ And LCase(Left(sOTF,1)) <= "z" _ And Mid(sOTF,2,2) = ":\" Then If oFSO.FileExists(sOTF) Then iOPT = iOPT + 1 sOPT = sOPT & "<option value='" & sOTF & "'>" & sOTF & vbCrLf Else Alert(sOTF & " file does not exist!") End If End If Next sOPT = sOPT & "</select>" & vbCrLf sOPT = "<select id='Pick1' onchange='File_Read()' size='" & iOPT & "'>" & sOPT & vbCrLf '* document.getElementById("List").innerHTML = sOPT End Sub Sub File_Read() '**** '* Read file. '**** document.getElementById("Note").innerHTML = "" document.getElementById("Sect").innerHTML = "" document.getElementById("Data").Value = "" sINF = document.getElementById("Pick1").Value If sINF = "" Then Exit Sub '* '* Test '* If Not oFSO.FileExists(sINF) Then sERR = "<b>" & sINF & "</b> does not exist!" document.getElementById("Note").innerHTML = sERR Exit Sub End If '* '* Read '* Set oOTF = oFSO.OpenTextFile(sINF,1) sOTF = oOTF.ReadAll Set oOTF = Nothing document.getElementById("Data").Value = sOTF sSAV = sOTF '* '* INF Sections '* If Right(LCase(sINF),4) <> ".inf" Then Exit Sub iOPT = 0 sOPT = "" aOTF = Split(sOTF,vbCrLf) For iOTF = 0 To UBound(aOTF) sOTF = Trim(aOTF(iOTF)) If Left(sOTF,1) = "[" _ And Right(sOTF,1) = "]" Then iOPT = iOPT + 1 sOPT = sOPT & "<option value='" & sOTF & "'>" & sOTF & vbCrLf End If Next '* sOPT = sOPT & "</select>" & vbCrLf sOPT = "<select size='" & iOPT & "'>" & sOPT & vbCrLf sOPT = "<b>Sections:</b><br>" & vbCrLf & sOPT & vbCrLf document.getElementById("Sect").innerHTML = sOPT End Sub Sub File_Position() '**** '* Inserting at the cursor using JavaScript '* http://alexking.org/blog/2003/06/02/...ing-javascript / '**** sTMP = document.getElementById("Entry").Value If sTMP = "" Then Exit Sub If MsgBox("Insert text at cursor position?",vbQuestion+vbYesNo,cHTA) = vbYes Then document.getElementById("Data").focus() document.selection.createRange().text = sTMP End If End Sub Sub File_Replace() '**** '* Copy old file and Write new file. '**** dNOW = Now sNOW = Year(dNOW) _ & (10^8 * Month(dNOW)) _ + (10^6 * Day(dNOW)) _ + (10^4 * Hour(dNOW)) _ + (10^2 * Minute(dNOW)) _ + (10^0 * Second(dNOW)) sOTF = document.getElementById("Data").Value '* '* Test '* If sINF = "" Then sERR = "<b>No file selected!</b>" document.getElementById("Note").innerHTML = sERR Exit Sub ElseIf sOTF = "" Then sERR = "<b>" & sINF & "</b> is empty!" document.getElementById("Note").innerHTML = sERR Exit Sub ElseIf sSAV = sOTF Then sERR = "<b>" & sINF & "</b> not changed!" document.getElementById("Note").innerHTML = sERR Exit Sub End If '* '* Copy '* oFSO.CopyFile sINF, sINF & "." & sNOW '* '* Write '* Set oOTF = oFSO.OpenTextFile(sINF,2,true) oOTF.Write(sOTF) Set oOTF = Nothing End Sub Sub Window_Onload() '**** '* List files. '**** Call File_List() End Sub </script> <style type="text/css"> body,td,th { font-family:Arial; font-size:80% } ..butt { background-color:white; width:80px; height:22px } ..text { font-family:Courier; font-size:10pt } ..yell { background-color:yellow } </style> </head> <body> <table border="0" width="90%"> <tr valign="top"> <td> <b>Select a file:</b> <br> <span id="List"></span> <br><br> <span class="yell" id="Note"></span> <br><br> <b>Text to be inserted at cursor position:</b> <br> <textarea class="text" id="Entry" cols="30" rows="10" wrap="virtual"></textarea> <br><br> <input type="button" class="butt" value="Insert" onclick="File_Position()"> <input type="button" class="butt" value="Replace" onclick="File_Replace()"> <input type="button" class="butt" value="Exit" onClick="Self.Close()"> <br><br> <span id="Sect"></span> </td> <td> <textarea class="text" id="Data" cols="72" rows="50" wrap="virtual"></textarea> </td> </tr> </table> </body> </html> |
My System Specs![]() |
| | #8 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. On Jun 23, 9:26*pm, "McKirahan" <N...@xxxxxx> wrote: Quote: Quote: > > "Daz" <darren.black...@xxxxxx> wrote in message > news:2e7038f4-119c-49a9-885f-> bfa16d8e1...@xxxxxx > > > Quote: Quote: > > > What I meant was: where do lines to be added exist? Quote: > > "The text/instructions i would like to be able to have > > listed in the vbs (or a comma delimited file that can be called from > > the vbs)" Quote: Quote: > > > "they all use the same ini file name" -- sre the files in differnet Quote: Quote: > > > and/or on different systems? Quote: Quote: > > > Sorry, I'm still trying to gather your requirements. Quote: Quote: > > > It's just not very clear (to me) how you envision an automated > > > effort to work. Quote: Quote: > > > Do you have a list of filenames with their paths from which to select? > > > Where are the additional lines -- in a text file or known when needed. Quote: > > Sorry about that - i will try to give you further examples. > [snip] > > > Quote: Quote: > > > I developed an HTA (= VBS + a UI) that allows entry of a filename > > > (which must exist in the HTA's folder); the contents of the file are > > > displayed in a <textarea> and may be changed and saved. Quote: Quote: > > > An enhancement would be to have a list of path+filenames; > > > the selection of one would cause it to be displayed in a <textarea> > > > and a list of the [sections] within it could be displayed as a > > > dropdown selection list. Another <textarea> would allow for > > > entry of the "additional lines". You would then select a > > > [section] from the list and click a button called "append" to > > > append the "additional lines" to the end of the selected [section]. > > > Alternatively, you could manually insert additional lines > > > where ever you wanted. You would then click a "save" button. Quote: Quote: > > > Your thoughts? Quote: > > Sound great! - then i could select the file i want (would need to be > > able to add a command-line argument to the entry) and then after > > creating an array add this to the [SetupHotfixesToRun]. > What does this mean? > "would need to be able to add a command-line argument to the entry" argument. Quote: > > Why/how would a "comma delimited file" be used for the additional lines? Quote: > > Do you have a list of filenames with their paths from which to select? > I don't need them, I just want to know if you have them in a list? |
My System Specs![]() |
| | #9 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. On Jun 24, 5:56*am, "McKirahan" <N...@xxxxxx> wrote: Quote: > "McKirahan" <N...@xxxxxx> wrote in message > > news:scidnd3zCclfOcLVnZ2dnUVZ_rfinZ2d@xxxxxx> > "Daz" <darren.black...@xxxxxx> wrote in message Quote: > > news:2e7038f4-119c-49a9-885f-> > bfa16d8e1...@xxxxxx > > [snip] > > I don't have answers to all of my questions but here's something. > > Will this help? *Watch for word-wrap. > > Save it as "SVCPack.hta" then create "SVCPack.txt" which contains a list > of files (with their full path; one per line) that you may want to edit. > For example, "C:\Temp\SVCPack.inf". > > To run it, doubleclick on "SVCPack.hta" (in Windows Explorer), > select a file by clicking on its name and its contents will be displayed > in the text area. *You can now manually edit the file or enter one or more > lines of text which you can insert at the cursor location. *After making > your changes click "Replace" to backup the file then replace it. > Quote: > '* *Inserting at the cursor using JavaScript > '*http://alexking.org/blog/2003/06/02/...rsor-using-jav.... > / I have created the HTA and the TXT file and when i load the hta i get " & sOTF & "'>" & sOTF & vbCrLf End If Next '* sOPT = sOPT & " in the first drop-down menu.... |
My System Specs![]() |
| | #10 (permalink) |
| | Re: Howto: Add lines of text from a specific point in a text file.. > "Daz" <darren.blackley@xxxxxx> wrote in message news:4f1e0127-4f65-43ad-8dcd-> b763f9d8aceb@xxxxxx... Quote: > On Jun 24, 5:56 am, "McKirahan" <N...@xxxxxx> wrote: Quote: > > "McKirahan" <N...@xxxxxx> wrote in message > > > > news:scidnd3zCclfOcLVnZ2dnUVZ_rfinZ2d@xxxxxx> > "Daz" Quote: Quote: Quote: > > > news:2e7038f4-119c-49a9-885f-> > > bfa16d8e1...@xxxxxx > > > > [snip] > > > > I don't have answers to all of my questions but here's something. > > > > Will this help? Watch for word-wrap. > > > > Save it as "SVCPack.hta" then create "SVCPack.txt" which contains a list > > of files (with their full path; one per line) that you may want to edit. > > For example, "C:\Temp\SVCPack.inf". > > > > To run it, doubleclick on "SVCPack.hta" (in Windows Explorer), > > select a file by clicking on its name and its contents will be displayed > > in the text area. You can now manually edit the file or enter one or Quote: Quote: > > lines of text which you can insert at the cursor location. After making > > your changes click "Replace" to backup the file then replace it. > > Quote: > > '* Inserting at the cursor using JavaScript > > Quote: Quote: > > / > Do i need to add this script in here? The URL wrapped -- the lone "/" neloings at the end of the URL. Quote: > I have created the HTA and the TXT file and when i load the hta i get > " & sOTF & "'>" & sOTF & vbCrLf End If Next '* sOPT = sOPT & " in the > first drop-down menu.... sOPT = sOPT & "<option value='" & sOTF & "'>" & sOTF & vbCrLf End If Next Hopefully, your newreader kept the indentations that I had in the code. |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| read text file - but starting at a specific point (not the very first character of the very first line) | VB Script | |||
| Using lines in a text file | PowerShell | |||
| Removing lines from a text file | PowerShell | |||
| HowTo: Get Text from a file and put in a string. | PowerShell | |||
| removing first three lines in a text file | PowerShell | |||