C++/VB - Replace Lines between 2 lines
Asked By V
30-May-07 12:21 PM
Hi All,
I am trying to edit an ini file. I would like to replace lines between to
lines e.g:
[Test1]
asdfasd
asdfasdf
[1st Line]
lines i would like to edit
lines i would like to edit
lines i would like to edit
[2nd Line]
I would like to replace everything between [1st Line] & [2nd Line]
is this possible? I have been trying all day but no luck. Don't know much VB
but learning as i'm going.
Regards
VJ
StrFind
(1)
StrFile
(1)
ObjFile
(1)
FileSystemObject
(1)
ObjFSO
(1)
StrNewFile
(1)
StrReplace
(1)
JHP
(1)
Richard Mueller [MVP] replied...
You can use the FileSystemObject to either read a file, write a new file, or
append to an existing file. The only way to modify an existing file (other
than appending) is to read the old file, write to a new file, close both
files, delete the old file, and finally rename the old file. You can use the
ReadLine method of a file object to read lines, and the WriteLine method of
another file object to write lines. This link should help:
http://www.microsoft.com/technet/scriptcenter/guide/sas_scr_iikh.mspx
Use the TOC to the left in the link to check out the topics "Reading text
files" and "Writing text files".
--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
JHP replied...
Try this:
There are a few way to achieve this... this is just a simple one.
(watch for word wrap)
Const forReading = 1
strFind1 = "[1st Line]"
strFind2 = "[2nd Line]"
strReplace = "This is all new stuff"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("test.txt", forReading)
strFile = objFile.ReadAll
strNewFile = Left(strFile, InStr(1, strFile, strFind1) + Len(strFind1)) &_
+ strReplace + vbCRLF + Mid(strFile, InStr(1, strFile, strFind2) - 1)
WScript.Echo strNewFile
objFile.Close
Set objFile = Nothing
Set objFSO = Nothing
V replied...
Thanks JHP!
Works wonders!!!!
Had to use the VBScript reference which Richard mentioned to see what each
bit does
Thanks again
Regards
VJ
objWMIService.ExecQuery _ ("ASSOCIATORS OF {Win32_Directory.Name = 'y: \ mailbox'} Where " _ & "ResultClass = CIM_DataFile") For Each objFile In FileList strDate = Left(objFile.FileName & "_" & objFile.CreationDate, 30) strNewName = objFile.Drive & objFile.Path & _ strDate & "." & "pst" strNameCheck = Replace(strNewName, " \ ", " \ ") i = 1 Do While True Set colFiles = objWMIService ExecQuery _ ("Select * from Cim_Datafile Where Name = '" & strNameCheck & "'") If colFiles.Count = 0 Then errResult = objFile.Rename(strNewName) Exit Do Else i = i + 1 strNewName = objFile.Drive & objFile.Path & _ strDate & "_" & i & "." & "pst" strNameCheck = Replace(strNewName, " \ ", " \ ") End If Loop Next '[End Rename
on error resume next strComputer = "." Const OverwriteExisting = TRUE Const ForReading = 1 Const ForAppending = 8 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objComputer = CreateObject("Shell.LocalMachine") Set objWMIService = GetObject("winmgmts: \ " & strComputer & " \ root \ CIMV2") pathfile = "C: \ " & objComputer ExecQuery( _ ' \ ntbackup \ backups \ exchange \ '", , 48) Set colItemsBALsexchange = objWMIService.ExecQuery( _ ' \ NTBACKUP \ BACKUPS \ '", , 48) If objFSO.FileExists(pathfile) Then Set objFile = objFSO.Opentextfile(pathfile, ForAppending, TRUE, -1) Else Set objFile = objFSO.CreateTextFile(pathfile, ForAppending, TRUE) objFile.Write("Date" & vbTab & "Heure" & vbTab & "Memoire physique dispo:" _ & vbTab & "Memoire Physique totale:" & vbTab & "Nombre
in advance (never mind the msgboxes, I just put them in there for testing): * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** Dim objFSO Dim strLocalFolder Dim strFile Dim objFile Dim strLine Dim strNumLines Dim strFrank Dim strHeader Dim strFooter Dim strNewText Set objFSO = CreateObject("Scripting.FileSystemObject") Set strLocalFolder = objFSO.GetFolder("C: \ PHFN007") Set WshShell = WScript.CreateObject("WScript.Shell") Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 For Each strFile In strLocalFolder.Files strFileName = strLocalFolder & " \ " & strFile.Name If Left(strFile.Name, 7) = "PHFN007" Then Set objFile = objFSO.OpenTextFile(strFileName, ForReading) 'msgbox
Const adOpenStatic = 3 Const adLockOptimistic = 3 Const adCmdText = &H0001 Dim fs, strheader, resultsfile, res Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile("report.vbs") Set fs = WScript.CreateObject("Scripting.FileSystemObject") ' / / / / / Variable Declare Dim book, report , objfile , csvbook ' / / / / / Open objects Dim objFSO : Set objFSO = CreateObject ("Scripting.FileSystemObject") ' - File system object Dim XL : Set XL = CreateObject ("Excel.Application") ' - Excel object
Function DeleteLine(strKey) 'DeleteLine Function by TomRiddle 2008 Const ForReading = 1:Const ForWriting = 2 Dim objFSO, objFile, strLine, strLineCase, strNewFile, strFile, LineNumber REM, CheckCase strFile = "C: \ proxyaddresses.txt" strKey = "x400" Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(strFile, ForReading) Do Until objFile.AtEndOfStream strLine = objFile.Readline REM If CheckCase = 0 then