开发者

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

开发者 https://www.devze.com 2023-02-14 15:56 出处:网络
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 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 ne开发者_如何学Ced 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号