Link to home
Start Free TrialLog in
Avatar of mficorp
mficorp

asked on

Navigating the Memory Buffer

It seams apparent that the Windows Scripting Host (WSH) does not contain sufficient properties or methods in connection with manipulating the contents of a text file. I have no problem opening a text file and reading line by line or the entire file into memory at once. From there I can execute replace functions with RegExp and Replace().

But how can I navigate the file pointer around to pluck individual elements from the bufferized file? I need to be able to do this in order to extract needed data and write it to a new file.

All my VBScript, WSH and ASP web and book resources happily point out that this is a breeze once the datastream has been fed into the memory buffer, but no information or direction is supplied on how to work with a bufferized file beyond the range of arsenal offered by WSH and VBScript. Have I missed something in VBScript, cause I am almost certain I haven't missed anything in WSH?

In summary, once the file is in memory, I need to be able to position the pointer at specific coordinates using either variable logic or coordinate numbers, and then read from there forward. Preferably, I would process line by line and need to have the ability to remove blank lines. Does such a command/function even existing within the scripting domain to do this?

Please point me in the right direction and/or provide examples. Allow me to express my thanks and gratitude in advance.

Ken
Avatar of VincentPuglia
VincentPuglia

Hi Ken,

search msdn.microsoft.com for:

  var ForReading = 1;
  fso = new ActiveXObject("Scripting.FileSystemObject")
  ts = fso.OpenTextFile(txtFile, ForReading)
  s = ts.ReadAll()

If there is a 'ReadAll' (& writeLine() ) method, there must be a ReadLine method

Vinny
Hi,

From
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/sgWorkingWithFiles.asp
[VBScript]
Sub ReadFiles
   Dim fso, f1, ts, s
   Const ForReading = 1
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
   ' Write a line.
   Response.Write "Writing file <br>"
   f1.WriteLine "Hello World"
   f1.WriteBlankLines(1)
   f1.Close
   ' Read the contents of the file.
   Response.Write "Reading file <br>"
   Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
   s = ts.ReadLine
   Response.Write "File contents = '" & s & "'"
   ts.Close
End Sub
[JScript]
function ReadFiles()
{
   var fso, f1, ts, s;
   var ForReading = 1;
   fso = new ActiveXObject("Scripting.FileSystemObject");
   f1 = fso.CreateTextFile("c:\\testfile.txt", true);
   // Write a line.
   Response.Write("Writing file <br>");
   f1.WriteLine("Hello World");
   f1.WriteBlankLines(1);
   f1.Close();
   // Read the contents of the file.
   Response.Write("Reading file <br>");
   ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
   s = ts.ReadLine();
   Response.Write("File contents = '" + s + "'");
   ts.Close();
}
Avatar of mficorp

ASKER

Thank you for the feedback, but this reply did not even come close to answering my question. You can imagine with the detailed description of my problem that basic resources such as MSDN had already been explored. But I appreciate your effort and encourage you to reread my original posting and explore the depth of the question.

Ken
Avatar of mficorp

ASKER

Vinny,

Again, thanks. But the problem I am having is not reading the file into memory (iow a variable), rather, manipulating the file in expert fashion once inside the memory buffer. Moving around the memory buffer.... using whatever logic or coordinate function/system that may be available.

Ken

I did read your post the first time, you are asking how to access lines within a buffer I had assumed the code I posted had a 'skipLine' method within it; that's what I get for assuming.

from the page posted above
If you use the Read or ReadLine method and want to skip to a particular portion of data, use the Skip or SkipLine method. The resulting text of the read methods is stored in a string which can be displayed in a control, parsed by string functions (such as Left, Right, and Mid), concatenated, and so forth.

if I am still wrong in what you are asking...

Vinny
I am neither an asper nor a vber (javascript/c  ok)

but what you want to do is something like:
   do {
   s = ts.ReadLine();
   if (s.indexOf(someVar) != -1)    doSomething(s)
   else ts.SkipLine();
   } while (!ts.eof)

go to msdn.microsoft.com and search for SkipLine
Vinny
from:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/wsmthskipline.asp

Dim StdIn, StdOut, Str1, Str2

Set StdIn = WScript.StdIn
Set StdOut = WScript.StdOut

Str1 = ""
Str2 = ""

For i = 0 to 4
   StdIn.SkipLine
Next

i = 0
Do While Not StdIn.AtEndOfStream
     If i >= 2 Then
          StdOut.WriteLine Str1
     End If
     i = i + 1
     Str1 = Str2
     Str2 = StdIn.ReadLine
Loop

Vinny
Avatar of mficorp

ASKER

Vinny,

I think now you understand my question. Am I correct in assuming that you are telling me there is no better way to move left or right across a line to hone in on a data segment other than using VB parse functions Left,Right,,Mid? Because that was my true question.. I needed a better way to parse, due to the limited and cumbersome nature of those very VB functions!

Your code sample was interesting, but I didn't recognize it as fully VBScript.

Ken
Hi Ken,

  If you mean the do while loop, it isn't vbscript -- it's pseudo-javascript.
I don't know vbscript, I only code.  Doesn't vb have an indexOf method?  some function that tells you if something is contained by something else?
If not, then I would suggest one of 2 things:

1) either call a javascript function from within your vb/asp
2) or, split each line on " " (space) into an array and loop through the array  in javascript it would be something like:

  line= fs.ReadLine();
  tmp = line.split(" ")
  for (i = 0; i < tmp.length; i++)
   if (tmp[i] == someValue)
      doSomething();

Vinny
Avatar of mficorp

ASKER

Vinny,

Your latest example had already been viewed by me. Unfortunately, the Stdx properties of the Skipline method are naccessible under the Wscript host.. CScript host only. But they are of limited value.

Ken
ASKER CERTIFIED SOLUTION
Avatar of VincentPuglia
VincentPuglia

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mficorp

ASKER

Thanks Vinny, I consider the issued resolved. Apparently VBScript Objects, Properties and Methods fall short of the capability that I need. I appreciate your effort and you did provide the answer I was looking for, though I did seek additional info.

Ken