vbs操作txt文本文件常用方法與函數(shù)代碼
'creat by 席飛劍(小席老師)
'操作文本文件,操作fso對象(文件對象操作)
函數(shù)代碼
創(chuàng)建文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) '第二個參數(shù)表示目標文件存在時是否覆蓋
f.Write("寫入內容")
f.WriteLine("寫入內容并換行")
f.WriteBlankLines(3) '寫入三個空白行(相當于在文本編輯器中按三次回車)
f.Close()
set f = nothing
set fso = nothing
打開并讀文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二個參數(shù) 1 表示只讀打開,第三個參數(shù)表示目標文件不存在時是否創(chuàng)建
f.Skip(3) '將當前位置向后移三個字符
f.SkipLine() '將當前位置移動到下一行的第一個字符,注意:無參數(shù)
response.Write f.Read(3) '從當前位置向后讀取三個字符,并將當前位置向后移三個字符
response.Write f.ReadLine() '從當前位置向后讀取直到遇到換行符(不讀取換行符),并將當前位置移動到下一行的第一個字符,注意:無參數(shù)
response.Write f.ReadAll() '從當前位置向后讀取,直到文件結束,并將當前位置移動到文件的最后
if f.atEndOfLine then
  response.Write("一行的結尾!")
end if
if f.atEndOfStream then
  response.Write("文件的結尾!")
end if
f.Close()
set f = nothing
set fso = nothing
打開并寫文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 2, false) '第二個參數(shù) 2 表示重寫,如果是 8 表示追加
f.Write("寫入內容")
f.WriteLine("寫入內容并換行")
f.WriteBlankLines(3) '寫入三個空白行(相當于在文本編輯器中按三次回車)
f.Close()
set f = nothing
set fso = nothing
判斷文件是否存在
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FileExists("C:\test.txt") then
  response.Write("目標文件存在")
else
  response.Write("目標文件不存在")
end if
set fso = nothing
移動文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") '兩個參數(shù)的文件名部分可以不同
set fso = nothing
復制文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") '兩個參數(shù)的文件名部分可以不同
set fso = nothing
刪除文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFile("C:\test.txt")
set fso = nothing
創(chuàng)建文件夾
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.CreateFolder("C:\test") '目標文件夾的父文件夾必須存在
set fso = nothing
判斷文件夾是否存在
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
  response.Write("目標文件夾存在")
else
  response.Write("目標文件夾不存在")
end if
set fso = nothing
刪除文件夾
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\test") '文件夾不必為空
set fso = nothing
這篇文章就介紹到這,更多的大家可以查看我們以前發(fā)布的關于vbs txt操作的相關文章。
上一篇:VBS腳本實現(xiàn)遍歷批量替換多目錄多文件內容的代碼
欄 目:vb
下一篇:VBS基礎篇 - vbscript class類的定義與使用
本文標題:vbs操作txt文本文件常用方法與函數(shù)代碼
本文地址:http://www.jygsgssxh.com/a1/vb/7110.html
您可能感興趣的文章
- 01-10下載文件到本地運行的vbs
 - 01-10VBS中的正則表達式的用法大全 <font color=red>原創(chuàng)&
 - 01-10VBS中SendKeys的基本應用
 - 01-10VBScript教程 第十一課深入VBScript
 - 01-10用VBSCRIPT控制ONSUBMIT事件
 - 01-10VBScript語法速查及實例說明
 - 01-10VBS中Select CASE的其它用法
 - 01-10VBScript教程 第七課使用條件語句
 - 01-10vbscript 可以按引用傳遞參數(shù)嗎?
 - 01-10VBScript教程 第二課在HTML頁面中添加VBscript代碼
 


閱讀排行
本欄相關
- 01-10下載文件到本地運行的vbs
 - 01-10飄葉千夫指源代碼,又稱qq刷屏器
 - 01-10SendKeys參考文檔
 - 01-10什么是一個高效的軟件
 - 01-10VBS中的正則表達式的用法大全 &l
 - 01-10exe2swf 工具(Adodb.Stream版)
 - 01-10VBS中SendKeys的基本應用
 - 01-10用VBSCRIPT控制ONSUBMIT事件
 - 01-10VBScript教程 第十一課深入VBScript
 - 01-10VBScript語法速查及實例說明
 
隨機閱讀
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
 - 08-05DEDE織夢data目錄下的sessions文件夾有什
 - 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
 - 08-05織夢dedecms什么時候用欄目交叉功能?
 - 04-02jquery與jsp,用jquery
 - 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
 - 01-10使用C語言求解撲克牌的順子及n個骰子
 - 01-10C#中split用法實例總結
 - 01-10delphi制作wav文件的方法
 - 01-11ajax實現(xiàn)頁面的局部加載
 


