When programing with Winbatch (winbatch.com) I could read the contents of a file into a Binary buffer. This way I would not need to read each line. I know I could use the select-string command, but if I have multiple sections in the file and each section contains the string to be extracted, then select-string would not work as well. In such a case it would have been easy going to the start of section, end of section, extracting the whole section out and then extracting the line in the section, instead of reading the file a line at a time to extract the line as well as keeping track of when you enter and exit a section.
So could this be written in Powershell (This is Winbatch code):
File_HostConnect = "c:\temp\HostConnect.txt" fs_HostConnect = FileSize(File_HostConnect) binbuf_HostConnect = BinaryAlloc(fs_HostConnect) BinaryRead( binbuf_HostConnect, File_HostConnect) Offset_HostConnect = 0 while @true Str_Begin = BinaryIndexNc( binbuf_HostConnect, Offset_HostConnect, "Some string", @FWDSCAN) If Str_Begin == 0 then Break Str_End = BinaryIndexNc( binbuf_HostConnect, Str_Begin, "end of string", @FWDSCAN) If Str_End == 0 then Break Offset_HostConnect = Str_End Str_Full = strtrim(BinaryPeekStr(binbuf_HostConnect, Str_Begin, Str_End - Str_Begin)) Break Endwhile BinaryFree(binbuf_HostConnect)