'creat by 席飛劍(小席老師)
'操作文本文件,操作fso對(duì)象(文件對(duì)象操作)
函數(shù)代碼
創(chuàng)建文件
dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.CreateTextFile("C:\test.txt", true) '第二個(gè)參數(shù)表示目標(biāo)文件存在時(shí)是否覆蓋
f.Write("寫入內(nèi)容")
f.WriteLine("寫入內(nèi)容并換行")
f.WriteBlankLines(3) '寫入三個(gè)空白行(相當(dāng)于在文本編輯器中按三次回車)
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) '第二個(gè)參數(shù) 1 表示只讀打開,第三個(gè)參數(shù)表示目標(biāo)文件不存在時(shí)是否創(chuàng)建
f.Skip(3) '將當(dāng)前位置向后移三個(gè)字符
f.SkipLine() '將當(dāng)前位置移動(dòng)到下一行的第一個(gè)字符,注意:無參數(shù)
response.Write f.Read(3) '從當(dāng)前位置向后讀取三個(gè)字符,并將當(dāng)前位置向后移三個(gè)字符
response.Write f.ReadLine() '從當(dāng)前位置向后讀取直到遇到換行符(不讀取換行符),并將當(dāng)前位置移動(dòng)到下一行的第一個(gè)字符,注意:無參數(shù)
response.Write f.ReadAll() '從當(dāng)前位置向后讀取,直到文件結(jié)束,并將當(dāng)前位置移動(dòng)到文件的最后
if f.atEndOfLine then
response.Write("一行的結(jié)尾!")
end if
if f.atEndOfStream then
response.Write("文件的結(jié)尾!")
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) '第二個(gè)參數(shù) 2 表示重寫,如果是 8 表示追加
f.Write("寫入內(nèi)容")
f.WriteLine("寫入內(nèi)容并換行")
f.WriteBlankLines(3) '寫入三個(gè)空白行(相當(dāng)于在文本編輯器中按三次回車)
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("目標(biāo)文件存在")
else
response.Write("目標(biāo)文件不存在")
end if
set fso = nothing
移動(dòng)文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.MoveFile("C:\test.txt", "D:\test111.txt") '兩個(gè)參數(shù)的文件名部分可以不同
set fso = nothing
復(fù)制文件
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
call fso.CopyFile("C:\test.txt", "D:\test111.txt") '兩個(gè)參數(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") '目標(biāo)文件夾的父文件夾必須存在
set fso = nothing
判斷文件夾是否存在
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
if fso.FolderExists("C:\Windows") then
response.Write("目標(biāo)文件夾存在")
else
response.Write("目標(biāo)文件夾不存在")
end if
set fso = nothing
刪除文件夾
dim fso
set fso = server.CreateObject("Scripting.FileSystemObject")
fso.DeleteFolder("C:\test") '文件夾不必為空
set fso = nothing
這篇文章就介紹到這,更多的大家可以查看腳本之家以前發(fā)布的關(guān)于vbs txt操作的相關(guān)文章。
您可能感興趣的文章:- VBS合并一個(gè)文件夾里的TXT的實(shí)現(xiàn)代碼
- vbs解決處理TXT文本數(shù)據(jù)相關(guān)問題實(shí)現(xiàn)代碼
- VBS實(shí)現(xiàn)將Excel表格保存為txt文本
- TXT轉(zhuǎn)HTM、HTML「TXT轉(zhuǎn)網(wǎng)頁」的vbs實(shí)現(xiàn)代碼
- 用vbs產(chǎn)生的若干個(gè)隨機(jī)數(shù)并寫入到一個(gè).txt中的代碼
- 將HTML或txt文件轉(zhuǎn)換為ASP輸出的vbs代碼
- 用VBS將一篇txt后綴的內(nèi)容保存為html格式
- vbs實(shí)現(xiàn)的支持拖動(dòng)的txt文本切割器