Vbscript - 读取 ini 或文本文件的特定部分
我想将一些地址存储在文本文件中,然后根据组成员身份读取文件的特定部分。我已经完成了所有小组成员资格的工作,所以我不需要任何帮助。
但我不确定是否应该使用纯文本文件或 INI 文件?
问题是,帖子地址分为两行或三行,我需要换行。
我尝试使用纯文本文件,但无法正确换行。
那么 INI 文件会更好吗?
INI 文件可能如下所示:
[London] Address 1 Postbox 3245 58348 London [Copenhagen] Address 2 Postbox 2455 5478347 Copenhagen
不过,我不太确定这在 INI 文件中是否可能,也许我还需要命名每一行。或者,我可以使用纯文本文件并搜索单词 [london],然后读取每一行,直到出现换行符。然后将所有这些行存储在我将传递的变量中?
你们会怎么解决这个问题?
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.
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.
So INI files would be preferable?
The INI file could look like this:
[London] Address 1 Postbox 3245 58348 London [Copenhagen] Address 2 Postbox 2455 5478347 Copenhagen
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我编写了一个小型 VBScript 类,用于处理以这种格式编写的“真实”ini 文件:
该类的代码是:
用法示例:
当文件不存在或包含无效行时,代码会抛出各种错误,错误几乎很多清除时,可以通过使用此类代码来“抑制”错误并且不显示错误页面:
I have written a small VBScript Class that handles "real' ini files written with such format:
The code for the class is:
Usage example:
The code throw various errors when the file does not exist or contains invalid lines, the errors are pretty much clear. It's possible to "suppress" the errors and not display error page by using such code when consuming:
我可能会使用 CSV 文件,其中每一行代表一个国家/地区。
如果您可以轻松识别数据,那么您可能会拥有更具描述性的列名称(即 Street1、Street2、Town、Postcode 等)。
这种文件格式也很容易阅读,因为您一次只读取输入文件的一行,并使用类似的方法将其分割。
为了使其更容易使用,您可以使用字典对象并使用国家/地区作为键和数组作为价值
谢谢,
马切伊
I would probably use CSV file instead where each row will represent a country.
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
To make it even easier to work with you could use dictionary object and use country as a key and array as a value
Thanks,
Maciej
您可以将地址存储在一行中,并使用特殊字符(例如下划线)来指示换行符。读取地址时,只需要将特殊字符替换为换行符即可。
这允许您存储更多行或没有邮箱行的地址。根据我的经验,像“我们的地址总是这样的信息恰好有两行,第一行是总是邮箱”通常是不正确的......
You could store the addresses in one line and use a special character, for example an underscore, to indicate a line break. When you read the address, you just need to replace the special character with a line break.
That allows you to store addresses with more lines or without a postbox line, as well. In my experience, information like "our addresses always have exactly two lines and the first one is always a postbox" is very often incorrect...
我使用一个小型可执行文件来启动本机 api: GetPrivateProfileString 和 WritePrivateProfileString。
可执行文件的调用方式如下:
另请参阅使用 VbScript 以静默方式运行命令行并获取输出?。
这是可执行文件的代码:
您需要使用适用于 Windows 的 ac 编译器来编译它。我是用 gcc 完成的,但是 ms 的免费编译器也应该可以工作。如果带有 32 位可执行文件的此页面仍然可用,则您可以尝试一下,但责任自负。黑客已经访问过我的网站一次。
I use a small executable that launches native api for that: GetPrivateProfileString and WritePrivateProfileString.
The executable is called like that:
See also Running command line silently with VbScript and getting output?.
This is the code of the executable:
You need to compile it using a c compiler for Windows. I did it with gcc, but a free compiler from ms should also work. If this page with a 32-bit executable is still available, you may give it a try, but on your own responsibility. Hackers already visited my site once.