VBScript-读取特定部分的Ini或文本文件 [英] Vbscript - Read ini or text file for specific section

查看:149
本文介绍了VBScript-读取特定部分的Ini或文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在文本文件中存储一些地址,然后根据组成员身份读取文件的特定部分.我已经完成了所有的组成员资格工作,因此不需要任何帮助.

I want to store some addresses in a text file and then read specific portions of the file, based on group membership. I've done all of the group membership stuff so I don't need any help for that.

但是我不确定应该使用纯文本文件还是INI文件?
关键是,发信地址在两三行中,我需要换行.
我尝试使用纯文本文件,但无法正确地换行.

But I'm not sure if I should use a plain text file or an INI file?
The thing is, the post addresses are in two or three lines and I need line break.
I tried using a plain text file, but I couldn't manage to get a line break correctly.

那么最好使用INI文件?

So INI files would be preferable?

INI文件可能如下所示:

The INI file could look like this:


[London]
Address 1
Postbox 3245
58348 London

[Copenhagen]
Address 2
Postbox 2455
5478347 Copenhagen

我不太确定这是否可以在INI文件中使用,也许我也需要为每行命名.或者,我可能会使用纯文本文件并搜索单词[london],然后读取每一行,直到出现换行符为止.然后将所有这些行存储在我要传递的变量中?

I'm not quite sure if this is possible in an INI file though, perhaps I need to name each line as well. OR, I could possibly use a plain text file and search for the word [london] and then read each line until there's a line break. Then store all of those lines in a variable that I'll pass along?

你们将如何解决这个问题?

How would you guys solve this?

推荐答案

我可能会改用CSV文件代替,每行代表一个国家.

I would probably use CSV file instead where each row will represent a country.

Country,Address1,Address2,Address3,Address4
London,Address 1,Postbox 3245,58348 London
Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen

如果您可以轻松识别数据,则可能会有更多描述性的列名(例如Street1,Street2,Town,邮政编码等).

If you can easily identify your data then you could probably have more descriptive column names (i.e. Street1, Street2, Town, Postcode, etc.).

这种文件格式也很容易阅读,因为您一次只能读取一行输入文件,并使用类似的方式将其分割

This file format is also easy to read since you only read one line of the input file at a time and split it using something like

aAddress = split(sLine, ",")

要使其更容易使用,可以使用字典对象,并使用country作为键,并使用数组作为值

To make it even easier to work with you could use dictionary object and use country as a key and array as a value

'sLine should be read from input file'
sLine = "Copenhagen,Address 2,Postbox 2455,5478347,Copenhagen"

'Create dictionary for addresses'
Set dic = CreateObject("Scripting.Dictionary")

'Split line into array'
aAddressParts = Split(sLine, ",") 

'Remove the first element of the array'
sValues = Mid(sLine, InStr(sLine, ",")+1)
aValues = Split(sValues, ",")

'Add new entry into dictionary'
dic.Add aAddressParts(0), aValues

'Usage'
MsgBox "Address for Copenhagen: " & vbNewLine & _
    Join(dic("Copenhagen"), "," & vbNewLine)

谢谢, Maciej

Thanks, Maciej

这篇关于VBScript-读取特定部分的Ini或文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆