如何在vbs中操作txt文本文件-創(chuàng)新互聯(lián)

這篇文章將為大家詳細(xì)講解有關(guān)如何在vbs中操作txt文本文件,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

目前創(chuàng)新互聯(lián)已為成百上千的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間網(wǎng)站運(yùn)營(yíng)、企業(yè)網(wǎng)站設(shè)計(jì)、武都網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

創(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

打開(kāi)并讀文件

dim fso, f
set fso = server.CreateObject("Scripting.FileSystemObject")
set f = fso.OpenTextFile("C:\test.txt", 1, false) '第二個(gè)參數(shù) 1 表示只讀打開(kāi),第三個(gè)參數(shù)表示目標(biāo)文件不存在時(shí)是否創(chuàng)建
f.Skip(3) '將當(dāng)前位置向后移三個(gè)字符
f.SkipLine() '將當(dāng)前位置移動(dòng)到下一行的第一個(gè)字符,注意:無(wú)參數(shù)
response.Write f.Read(3) '從當(dāng)前位置向后讀取三個(gè)字符,并將當(dāng)前位置向后移三個(gè)字符
response.Write f.ReadLine() '從當(dāng)前位置向后讀取直到遇到換行符(不讀取換行符),并將當(dāng)前位置移動(dòng)到下一行的第一個(gè)字符,注意:無(wú)參數(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

打開(kāi)并寫文件

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

關(guān)于如何在vbs中操作txt文本文件就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到。

網(wǎng)站標(biāo)題:如何在vbs中操作txt文本文件-創(chuàng)新互聯(lián)
網(wǎng)站路徑:http://muchs.cn/article24/ddojje.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、ChatGPT、網(wǎng)站排名、網(wǎng)站改版、網(wǎng)站策劃、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)