分享

【新提醒】TXT文件用法大全【荐】1

 淳爷们 2013-08-28
(1)覆盖TXT文件指定某一行内容
  1. // 欲写入的数据
  2. neirong="感谢您使用由《按键用户》制作的脚本"
  3. // 写入第几行
  4. jihang=5
  5. Text = Plugin.File.ReadFileEx("路径\文件名.txt")
  6. txtArray=Split(Text,"|")
  7. txt=""
  8. i=0
  9. Rem z
  10. If Len(txtArray(i))>=1
  11.     If i=jihang-1
  12.         // 覆盖
  13.         txt = txt & neirong & vbCrLf
  14.     Else 
  15.         txt = txt & txtArray(i) & vbCrLf
  16.     EndIf 
  17.     i=i+1
  18.     Goto z
  19. Else 
  20.     If i<=jihang
  21.         MessageBox "少于"&jihang&"行"
  22.         EndScript 
  23.     EndIf 
  24. EndIf 
  25. // 删除旧文檔
  26. Call Plugin.File.DeleteFile("路径\文件名.txt")
  27. // 储存新文檔
  28. Call Plugin.File.WriteFileEx("路径\文件名.txt", txt)
复制代码

(2)插入内容到TXT文件的指定某一行
  1. //欲写入的数据
  2. neirong="感谢您使用此脚本"
  3. //写入第几行
  4. jihang=5
  5. Text = Plugin.File.ReadFileEx("路径\文件名.txt")
  6. txtArray=Split(Text,"|")
  7. txt=""
  8. i=0
  9. // 若旧文檔大于jihang行
  10. If UBound(txtArray)>=1
  11.     For jihang-1
  12.         txt = txt & txtArray(i) & vbCrLf
  13.         i=i+1
  14.     EndFor 
  15.     txt = txt & neirong & vbCrLf
  16.     For UBound(txtArray)-(jihang-1)
  17.         txt = txt & txtArray(i) & vbCrLf
  18.         i=i+1
  19.     EndFor 
  20. Else 
  21.     // 若旧文檔小于jihang行
  22.     For UBound(txtArray)
  23.         txt = txt & txtArray(i) & vbCrLf
  24.         i=i+1
  25.     EndFor 
  26.     txt = txt & neirong & vbCrLf
  27. EndIf 
  28. // 删除旧文檔
  29. Call Plugin.File.DeleteFile("路径\文件名.txt")
  30. // 储存新文檔
  31. Call Plugin.File.WriteFileEx("路径\文件名.txt", txt)
复制代码

(3)读取TXT文件指定某一行的第?到第?个字
  1. UserVar t=2 "读出txt第几行文本"
  2. UserVar i=5 "从第几个字开始读取"
  3. UserVar O=8 "结束读取到第几个字"
  4. Text = Plugin.File.ReadFileEx("路径\文件名.txt")
  5. txtArray=Split(Text,"|")
  6. I=I-1
  7. O=O-I:T=T-1
  8. For O
  9.     str=str & Mid(txtArray(t),1+i,1)
  10.     i=i+1
  11. EndWhile 
  12. MessageBox str
  13. EndScript
复制代码

(4)新建一个TXT文件
  1. set fso=CreateObject("Scripting.FileSystemObject")
  2. const forwriting=2
  3. const forreading=1
  4. set myfile=fso.createtextfile("路径\文件名.txt")
复制代码

(5)判断一个文件是否存在
  1. IsFile = Plugin.File.IsFileExit("路径\文件名.txt")
  2. If IsFile = True
  3.     MessageBox "找到"
  4. Else 
  5.     MessageBox "没找到"
  6. EndIf 
复制代码

(6)给TXT文件写入内容
  1. set fso=CreateObject("Scripting.FileSystemObject")
  2. set myfile=fso.createtextfile("路径\文件名.txt")
  3. myfile.writeline("内容")
  4. Call Plugin.File.WriteFileEx("路径\文件名.txt", "内容")
复制代码

(7)读取TXT文件指定某一行内容
  1. Text = Plugin.File.ReadFileEx("路径\文件名.txt")
  2. txtArray=Split(Text,"|")
  3. // 显示第5行,就写 txtArray(4) 。 显示第6行。就写 txtArray(5)
  4. MessageBox txtArray(0)
复制代码

8)读取TXT文件全部内容
  1. I=0
  2. Text = Plugin.File.ReadFileEx("路径\文件名.txt")
  3. txtArray=Split(Text,"|")
  4. While Len(txtArray(I))>=1
  5.     RE=RE & txtArray(I)
  6.     I=I+1
  7. EndWhile 
  8. MessageBox RE
复制代码

(9)读取txt文件一共多少行
  1. I=0
  2. Text = Plugin.File.ReadFileEx("路径\文件名.txt")
  3. txtArray=Split(Text,"|")
  4. While Len(txtArray(I))>=1
  5.     I=I+1
  6. EndWhile 
  7. MessageBox i
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多