VBScript 文件操作代碼小結(jié)
來源:易賢網(wǎng) 閱讀:1510 次 日期:2016-07-18 15:10:52
溫馨提示:易賢網(wǎng)小編為您整理了“VBScript 文件操作代碼小結(jié)”,方便廣大網(wǎng)友查閱!

下面是一些vbs下常用到的一些文件操作代碼,整理了下方便以后開發(fā)使用,提高效率。

創(chuàng)建文本文件

代碼如下:

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.CreateTextFile("C:\FSO\ScriptLog.txt")

檢察文件是否存在

代碼如下:

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists("C:\FSO\ScriptLog.txt") Then

Set objFolder = objFSO.GetFile("C:\FSO\ScriptLog.txt")

Else

Wscript.Echo "File does not exist."

End If

刪除文本文件

代碼如下:

Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.DeleteFile("C:\FSO\ScriptLog.txt")

重命名文件

代碼如下:

Set objFSO = CreateObject("Scripting.FileSystemObject")

objFSO.MoveFile "C:\FSO\ScriptLog.txt" , "C:\FSO\BackupLog.txt"

文本操作

讀取全部內(nèi)容

代碼如下:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\Scripts\Test.txt", ForReading)

strContents = objFile.ReadAll

objFile.Close

一行行的讀取文本文件內(nèi)容

代碼如下:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTextFile = objFSO.OpenTextFile ("c:\scripts\servers.txt", ForReading)

Do Until objTextFile.AtEndOfStream

strComputer = objTextFile.ReadLine

Wscript.Echo strComputer

Loop

objTextFile.Close

追加文本文件一行內(nèi)容

代碼如下:

Const ForAppending = 8

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objTextFile = objFSO.OpenTextFile ("C:\Scripts\Service_Status.txt", ForAppending, True)

objTextFile.WriteLine("追加的內(nèi)容")

objTextFile.Close

有用的幾個(gè)函數(shù):

替換:將Jim替換成James。

代碼如下:

strNewText = Replace(strText, "Jim ", "James ")

用逗號(hào)分隔字符串:

代碼如下:

arrpath=split(strDN,",")

wscript.echo arrpath(0)

幾個(gè)實(shí)例:

讀取文本文件指定的行內(nèi)容(讀第四行內(nèi)容存到strLine變量中)

代碼如下:

Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject") Set objTextFile = objFSO.OpenTextFile("mylogfile.log", ForReading)

For i = 1 to 3 objTextFile.ReadLine Next

strLine = objTextFile.ReadLine Wscript.Echo strLine

objTextFile.Close

更多信息請(qǐng)查看腳本欄目
易賢網(wǎng)手機(jī)網(wǎng)站地址:VBScript 文件操作代碼小結(jié)
由于各方面情況的不斷調(diào)整與變化,易賢網(wǎng)提供的所有考試信息和咨詢回復(fù)僅供參考,敬請(qǐng)考生以權(quán)威部門公布的正式信息和咨詢?yōu)闇?zhǔn)!
關(guān)于我們 | 聯(lián)系我們 | 人才招聘 | 網(wǎng)站聲明 | 網(wǎng)站幫助 | 非正式的簡要咨詢 | 簡要咨詢須知 | 加入群交流 | 手機(jī)站點(diǎn) | 投訴建議
工業(yè)和信息化部備案號(hào):滇ICP備2023014141號(hào)-1 云南省教育廳備案號(hào):云教ICP備0901021 滇公網(wǎng)安備53010202001879號(hào) 人力資源服務(wù)許可證:(云)人服證字(2023)第0102001523號(hào)
云南網(wǎng)警備案專用圖標(biāo)
聯(lián)系電話:0871-65317125(9:00—18:00) 獲取招聘考試信息及咨詢關(guān)注公眾號(hào):hfpxwx
咨詢QQ:526150442(9:00—18:00)版權(quán)所有:易賢網(wǎng)
云南網(wǎng)警報(bào)警專用圖標(biāo)