[转]Scripts to manage Text Files

具体用法:新建一个txt文档,将脚本保存在其中,然后将文件类型保存为.VBS,点击运行。

Checking the Size of a File Before Reading It


Demonstration scrīpt that uses the FileSystemObject to ensure that a text file is not empty before attempting to read it. scrīpt must be run on the local computer.

Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjFile = objFSO.GetFile("C:\Windows\Netlogon.log")
If objFile.Size > 0 Then
    Set ōbjReadFile = objFSO.OpenTextFile("C:\Windows\Netlogon.log", 1)
    strContents = objReadFile.ReadAll
    Wscrīpt.Echo strContents
    objReadFile.Close
Else
    Wscrīpt.Echo "The file is empty."
End If


 

Creating and Naming a Text File


Demonstration scrīpt that uses the FileSystemObject's GetTempName method to generate a file name, and then creates a file by that name.

Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
strPath = "C:\FSO"
strFileName = objFSO.GetTempName
strFullName = objFSO.BuildPath(strPath, strFileName)
Set ōbjFile = objFSO.CreateTextFile(strFullName)
objFile.Close
objFSO.DeleteFile(strFullName)


 

Creating a Text File


Demonstration scrīpt that creates a new, empty text file. scrīpt must be run on the local computer.

Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjFile = objFSO.CreateTextFile("C:\FSO\scrīptLog.txt")


 

Generating a File Name


Demonstration scrīpt that uses the FileSystemObject's GetTempName method to generate random file names. scrīpt must be run on the local computer.

Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
For i = 1 to 10
    strTempFile = objFSO.GetTempName
    Wscrīpt.Echo strTempFile
Next


 

Reading a Text File Character by Character


Demonstration scrīpt that uses the FileSystemObject to read a text file character-by-character, and individually echo those characters to the screen. scrīpt must be run on the local computer.

Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjFile = objFSO.OpenTextFile("C:\FSO\New Text Document.txt", 1)
Do Until objFile.AtEndOfStream
    strCharacters = objFile.Read(1)
    Wscrīpt.Echo strCharacters
Loop


 

Reading a Text File into an Array


Demonstration scrīpt that uses the VBscrīpt Split command to read a line from a commas-separated values file, and then place the individual items in that line into an array.

Const ForReading = 1
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjTextFile = objFSO.OpenTextFile _
    ("c:\scrīpts\servers and services.txt", ForReading)
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrServiceList = Split(strNextLine , ",")
    Wscrīpt.Echo "Server name: " & arrServiceList(0)
    For i = 1 to Ubound(arrServiceList)
        Wscrīpt.Echo "Service: " & arrServiceList(i)
    Next
Loop


 

Reading a Text File from the Bottom Up


Demonstration scrīpt that uses the FileSystemObject to read a text file, and then to echo the text file in inverse order (that is, beginning with the last line in the text file and ending with the first line).

Dim arrFileLines()
i = 0
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjFile = objFSO.OpenTextFile("C:\FSO\scrīptLog.txt", 1)
Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFile.ReadLine
     i = i + 1
Loop
objFile.Close
For l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1
    Wscrīpt.Echo arrFileLines(l)
Next


 

Writing Data to a Text File


Demonstration scrīpt that retrieves the status for all the services installed on a computer, and then saves the service name and status to a text file.

Const ForAppending = 8
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")
Set ōbjTextFile = objFSO.OpenTextFile _
    ("c:\scrīpts\service_status.txt", ForAppending, True)
Set colServices =  GetObject("winmgmts:").ExecQuery _
    ("Select * from Win32_Service")
For Each objService in colServices    
    objTextFile.WriteLine(objService.DisplayName & vbTab & _
        objService.State)
Next
objTextFile.Close
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值