返回列表 回复 发帖

[贴士] 读写TXT文本内容

在从零开始学起时,在我硬盘上一些曾经帮助我很多的小纸条!--Sz

读写文本的例子。可做为小精灵的读取自定义参数。
sub 写文本,这里做为主程序来使用,大家需要用到的话可以把它最为一个子程序来调用。


//sub 写文本
px_1="xxx=(2) 2是参数用来作为XX功能"
VBS set file=CreateObject("Scripting.FileSystemObject")
VBS const forwriting=2
VBS const forreading=1
//假如配置文本存在,则自动创建文本并写入,假如存在,则自动读取括号内的参数
If file.FileExists("d:\配置文本.txt")=0
    VBS set file=CreateObject("Scripting.FileSystemObject")
//创建文本的位置
    VBS set myfile=file.createtextfile("d:\配置文本.txt")
//写入文本的位置
    VBS set myfile=file.opentextfile("d:\配置文本.txt",forwriting)
//写文本的内容
    VBS myfile.writeline(px_1)
    VBS myfile.Close
    Gosub 读文本
Else
    Gosub 读文本
EndIf
//sub 写文本

Sub 读文本
    VBS set file=CreateObject("Scripting.FileSystemObject")
    VBS Set myfile=file.OpenTextFile("d:\配置文本.txt",ForReading)
   //读第一行
   //px1
    VBS nw=myfile.ReadLine
   //取字符串,读取()内的参数
       use_len=instr(nw,"(")
    use_len_1=instr(nw,")")
    use_0=mid(nw,use_len+1,use_len_1-use_len-1)
   //读取出来的值赋给脚本中需要使用到的变量   
   px_1=use_0
   //px_1=cint(px_1)   读出来的是字符,需要用到数字的话请把变量转为整型
   VBS myfile.Close
Sub 读文本
简单就好!
返回列表