Login   /   Register

Pick-out and Save Specific Lines in a Text File

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

You’d like to have a script read through the file, toss out all the lines that begin with Success, and then save the file, a file which will then hold only information about the operations that failed.

Find and Replace Text in a Text File

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

Although we can’t directly search and replace text inside a text file, we can do the next best thing. We can: 1) open up a text file; 2) read the text into a variable; 3) do a search-and-replace on that variable; and 4) re-save the text file. We can even do all that from the command line, although we’ll hold off on that for a moment. Instead, let’s start with a simple script that carries out the search and replace:

Create a CSV file

April 1st, 2008 by daniva
     1 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 5
Loading ... Loading ...

CSV is short for comma-separated-values file, a text file in which individual elements separated by commas. For example, say you have a CSV file consisting of user first names, last names, and job titles; that file might look something like this:

Add two blank lines between each line in a file

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

Suppose we have a text file – left over from a previous column – that consists of a bunch of names…

Add a Line to the Top of a Text File

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

ForReading and ForWriting – that we’ll use when working with our text file.

Combine multiple text files

April 1st, 2008 by daniva
     1 votes, average: 1 out of 51 votes, average: 1 out of 51 votes, average: 1 out of 51 votes, average: 1 out of 51 votes, average: 1 out of 5
Loading ... Loading ...

To combine File1.log and File2.log into a single file (which we’ll call Output.txt) we’ll have to first read File1.log and append the contents of that file to output.txt, then we’ll have to read File2.log and append the contents of that file to Output.txt.

Tally items in a text file

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

What you’re looking for is a script that can read through the file and report the number of occurrences for each item; in other words, a script that can return a report…

Read just the last line of a text file

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

Reading the last line of a file is a workaround. There is no script that reads just the last line of a text file. But here’s a script that will make it look like you read just the last line of a text file:

Query a Text File and Retrieve Records

April 1st, 2008 by daniva
     1 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 5
Loading ... Loading ...

Is it important that your text files be formatted like this? It’s not just important, it’s crucial. We’re going to use database techniques to retrieve information from the file, and to do that the file needs to be delimited in some way (in this case, using the comma as the delimiter). Ideally, your files should include a header row as well. As long as your text file looks like this.

Open text file as Unicode or as ASCII

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

When you create a new Function Library and save it to your hard disk, the files are saved in Unicode. i have found out that, when i was trying to load an entire library file dynamically

Count how many Times the Word "Error" Appears in a Text File

April 1st, 2008 by daniva
     1 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 51 votes, average: 3 out of 5
Loading ... Loading ...

the first thought is to use the InStr function to see if the word “Error” appears anywhere in each line of the log file; you could then keep a running tally of the number of times you found the word, a technique remarkably similar to the one we demonstrated in yesterday’s column. That was a good idea, but, as you discovered, there was one major flaw in the plan: technically your log file has only one line in it. Consequently, your script always reported that it found one instance of the word Failure, regardless of how many instances actually existed. “But I’m stuck,” you wrote, “because there’s no way to break this single line into multiple lines.”

Count the number of lines in a text file

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

The FileSystemObject is extremely useful, but it also has its limitations; one of the major limitations is the fact that it can only read a file from top to bottom. Unlike, say, the Tail utility, you can’t ask the FileSystemObject to read a file from the bottom up.

Read a text file from the bottom up

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

The FileSystemObject is extremely useful, but it also has its limitations; one of the major limitations is the fact that it can only read a file from top to bottom. Unlike, say, the Tail utility, you can’t ask the FileSystemObject to read a file from the bottom up.

Print text files using QTP

April 1st, 2008 by daniva
     0 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 50 votes, average: 0 out of 5
Loading ... Loading ...

You don’t need Microsoft Word just to print a few text files. Instead, you can use the Shell object – which is built right into the operating system – to print text files. And what’s cool about this is the fact that you can print these files even though the Shell object doesn’t have a Print method.