Solved

Navigating the Memory Buffer

Posted on 2003-10-27
12
389 Views
Last Modified: 2010-04-06
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
0
Comment
Question by:mficorp
  • 7
  • 5
12 Comments
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9629415
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
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9629457
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();
}
0
 

Author Comment

by:mficorp
ID: 9629499
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
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:mficorp
ID: 9629544
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
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9629587

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
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9629602
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
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9629714
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
0
 

Author Comment

by:mficorp
ID: 9629726
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
0
 
LVL 15

Expert Comment

by:VincentPuglia
ID: 9629762
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
0
 

Author Comment

by:mficorp
ID: 9629774
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
0
 
LVL 15

Accepted Solution

by:
VincentPuglia earned 500 total points
ID: 9646088
Hi Ken,
Sorry it's been awhile, but the real world...you know...

Does this help any?

http://www.tutorial-web.com/asp/fso/textstream.asp?property=SkipLine

Set fso = CreateObject("Scripting.FileSystemObject")
filename="c:\test\test.txt"
Set tso = fso.OpenTextFile(filename,1,false,0)
tso.SkipLine
response.write tso.ReadLine


google search results:
http://www.google.com/search?hl=en&ie=ISO-8859-1&q=filesystemobject+skipline

Vinny
0
 

Author Comment

by:mficorp
ID: 9656371
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
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Xsl Nested Loop 4 30
Windows Standards for file storage. 3 31
Google adds suggestions 3 69
Create animated movies for web page 18 86
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question