How to parse one text file to multiple files based on data contained within - vbscript

I have an enormous text file that I'd like to parse into other files - I think the only thing I have on this box (company computer) to use is VBS, so here's my question:
I have text file with a bunch of network configurations that looks like this:
"Active","HostName","IPAddress"
!
.......
end
This repeats itself throughout the file, but obviously for each configuration different data will occur within the "..." It always says "Active" for each configuration as well.
I want to create save files of the type HostName.cfg for each configuration and save all of the text between and including the ! and "end" . The line with the three quoted attributes doesn't need to be copied.
I'm still learning VBS so I'd appreciate any help in the matter. Thanks!

Here are some useful file reading functions and statements:
Freefile
Returns an integer which you should assign to a variable and then use in an Open statement.
Open <string> For Input As <integer>
Opens the specified file using the specified file handle. Don't use the same file handle twice without closing it in between.
Line Input #<integer>, <variable>
Reads one line of the file into a string.
Input #<integer>, <variable>, <variable>, <variable>
Reads one line of the file delimited by commas into several variables.

I once had to read a text file while omitting starting and ending lines. Though I didn't need to write it anywhere, FSO is simple enough that you will be able to figure it out. Here's some code for reading a file and link for writing to file via FSO: link
'Create a FSO object and open the file by providing the file path
Dim fso, f, filePath, line
filePath = "test.txt"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filePath)
'Skip through the first two lines
f.readline
f.readline
'Read till the end of file, if the line is not "end", split it based on ","
while not f.AtEndOfStream
line = f.readline()
if line <> "end" then
arr = split(line, ",")
'Write the arr to file
end if
wend
f.Close

Related

Reading Code inside Action in QTP

I want to read the code written inside an action in QTP,just as we read text file using FileSystem Object.
Is there any way to read code written in action line by line ?
So basically an action is attached to a QTP-Testcase. When you save this test case at a certain location, the code you have written is saved inside the directory named action1 or whatever action you were editing. In this folder you will find a file named script.AVCHD file. Open this in notepad and you will find your code inside it. Now all you got to do is write a simple visual basic, (or just any script you like) that will open this file and readall of the content into a variable. see if this helps. thanks.
For each test there will script.mts inside the Action1 folder.
get the script text line
filename = "C:\YourUFTTest\Action1\script.mts"
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(filename)
Do Until f.AtEndOfStream
msgbox f.ReadLine
Loop
f.Close

Inserting Lines into a Non-Text File

I am trying to insert lines into the middle of a non-text file (the extension of the file is "dxf"). I am using vbscript to do this.
Everywhere I have look, I come across the FileSystemObject.OpenTextFile. However, when I try to use this on a dxf file, it is causing an error: Exception 80070057 (I believe this is an invalid file) .
Here is my code:
Dim file
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
If fileexists(dxfFile$) Then
Set file = fso.OpenTextFile(dxfPath, ForAppending, True)
file.WriteLine("<PORTLIST TESTING>ASDFLKJ")
file.Close
End If
dxfFile$ is not a valid VBscript variable name; use dxfFile, file or dfxPath (consistently)
FileExists is a method of the FileSystemObject; you need to call fso.FileExists
Neither dxfFile, nor dfxPath, nor ForAppending are defined
Calling .OpenTextFile with an undefined/empty first/filespec parameter throws an error 5 - Invalid procedure call or argument
You can't insert lines by appending them; modifying files 'in the middle' is especially clumsy in VBScript; loading the whole file into memory, editing, writing it back may work for you
.DFX file come in ASCII or binary format; if the latter, you can't use the FileSystemObject (see ADODB.Stream)

Validate text file using VB

Could you please advice me on getting the VB script for my below mentioned scenario!!!
Scenario:
VB script needs to open the text file and reads all the lines in the text file but main conditions are for example if the text file named read.000 so the VB script opens the file named read.000 then the validation of the text file should starts with reading the first line of the text file and that first line in the text file is same as the file name read.000 so VB script validation should exactly match first line of file with text file name(read.000). also, the file name can be any read.001 or read.002 whatever the file name it should be same inside the file which present in first line of the file.
if the condition satisfied then only it needs to go for second validation.
Once file name validation is satisfied then VB script needs to validate the text file structure for example, in the text file read.000 the second line starts "01 John lagoon Canada"
In the above example 01 -> represent serial number which should be only two characters if more than two characters then validation get fail
John -> represents first name which should be only 4 characters if its more than 4 then validation needs to get fail same applicable for lagoon.
Could you please advice me on the above request with VB script.
Well hope that i'm getting your problem right, i'll do something like this:
Dim fso, rfile, line
Set fso = CreateObject("Scripting.FileSystemObject")
Set rfile = fso.OpenTextFile(pathtoyourfile, 1)
Do While rfile.AtEndOfStream <> true
line = rfile.ReadLine
if line = fso.GetFileName(pathtoyourfile) then
do your next Validation or something else'must be speficied
else
do something or end 'must be speficied
End if
Loop
rfile.close

VB Script Text File Prepend

Anyone know how to quickly prepend (add two new lines of text) to the start of an existing text file using either VB Script or a Bat file? Most elegant solution gets the tick.
How about this:
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile("test.txt", 1)
ReadAllTextFile = f.ReadAll
Set f = fso.OpenTextFile("test.txt", 2, True)
f.WriteLine("Blaaa")
f.WriteLine("Blaaaa some more...")
f.Write(ReadAllTextFile)
Source: Tek Tips
Check José Basilios answer for code and reference to the FSO. You will be using that.
BUT: I wouldn't go the ReadAllTextFile = f.ReadAll route, since that could be a few Gigabytes (who knows?).
INSTEAD:
open a new file
write prepended lines
read line by line from old file, writing into new file
(close both files)
delete old file
rename new file -> old file

Vb6 Performing an operation based on each line of a text file

I have a text file with a series of commands each one is on a diferent line what I need to do is go through the text file line by line and for each line perform a set of operations. How would I loop through line by line?
Example:
Text file contains:
johndoe.log
Apples and organes.log
monkies and zebras.log
script would grab line 1(johndoe.log)
create a new text file named johndoe.log
go to line two
create a new text file named apples and organes.log
etc... until the text file is complete
I know how to do everything except the loop that performs an operation on each line of the text file :(
and I know its oranges, typoed and went with it.
In classic VB6:
Dim LineData as String
Dim FileHandle as Integer
FileHandle = FreeFile
Open "C:\Test.txt" For Input As #FileHandle
Do While Not EOF(FileHandle)
Line Input #FileHandle, LineData
' Do whatever with LineData
Loop
Close #FileHandle
Or you can look at the FileSystemObject

Resources