如何使用 VBscript 写入或读取具有自定义扩展名的外部文件(例如,可以使用 .cyc 代替 .txt 扩展名)?
我不知道怎么办,似乎无法弄清楚。 读取文件时,是否可以只读取文件的一部分? 例如,如果我有一行
string1=Hello World!
在我的文件内部,我的脚本如何仅从 string1 行读取以及如何将文本值分配给 vbscript 文件中的字符串?
那么,如何将单个值写入我的文件?
为了澄清,我基本上尝试使用外部文件作为配置/数据文件。
这可能是一个非常菜鸟的问题,如果是的话我真的很抱歉。
编辑:这是一个由两部分组成的问题,我需要知道上面指定的读取和写入文件的代码,以及如何使用自定义扩展。
How do you write or read to external files with custom extensions (like, instead of a .txt extension it could have .cyc for example) using VBscript?
I don't know how, and cannot seem to figure it out. For reading a file, is it possible to only read part of a file? For example, if I have a line
string1=Hello World!
inside of my file, how does my script only read from the string1 line and how can it assign the text value to a string in the vbscript file?
Then, how can I write a single value to my file?
To clarify, I am basically attempting to use external files as configuration/data files.
This is probably a really nooby question, and I am really sorry if it is.
EDIT: This is a two part question, I need to know the code to read and write files as specified above, and how to use custom extensions with it.
发布评论
评论(4)
如果您想在随机位置读取和写入文本文件,最好的选择是使用 INI 文件。
INI 文件包含如下所示的条目:
要读取 ini 文件,您需要一个可以像这样调用的函数:
...它将把“John Doe”放入 s 中。
执行此操作的代码在这里:
http://cwashington.netreach.net/depo/view.asp?Index= 553
写入INI文件的工作方式相同。
这是另一个示例:
创建一个如下所示的 INI 文件:
您可以根据需要在每个部分中拥有任意多个不同的部分和项目。 要检索所需的值,您所要做的就是使用要检索的值的节名称和项名称来调用 ReadINI。
If you want to read and write from the text file at random locations, your best bet is to use INI files.
An INI file contains entries that look like this:
To read an ini file, you need a function that you can call like this:
...which will put "John Doe" in s.
The code to do this is here:
http://cwashington.netreach.net/depo/view.asp?Index=553
Writing the INI file works the same way.
Here is another example:
creates an INI file that looks like this:
You can have as many different sections and items within each section as you want. All you have to do to retrieve the value that you want is to call ReadINI with the section name and item name of the value you want to retrieve.
您所要做的就是在文件名末尾包含扩展名,如下所示:
当您打开文件进行写入时。
下面是一些示例代码,用于打开一个新文件,向其中写入一行文本,然后关闭该文件:
下面是读回该文件的代码:
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/other/textfiles/
All you should have to do is include the extension on the end of the filename, like this:
when you open the file for writing.
Here is some sample code that opens a new file, writes a line of text to it, and closes the file:
Here is the code to read it back:
There are some more examples at http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/other/textfiles/
以下类从 VB.Net 读取和写入 INI 文件:
The following class reads and writes INI files from VB.Net:
Cyclone:
这段代码已经过测试,所以我知道它有效。 我从 http://www.robvanderwoude.com/vbstech_files_ini.php 得到它
Cyclone:
This code has been tested, so I know it works. I got it from http://www.robvanderwoude.com/vbstech_files_ini.php