[转]Scripts to manage Text Files
来源:互联网 发布:淘宝网 等级 编辑:程序博客网 时间:2024/06/05 16:00
具体用法:新建一个txt文档,将脚本保存在其中,然后将文件类型保存为.VBS,点击运行。
Checking theSize of a File Before Reading It
Demonstration scrīpt that uses theFileSystemObject to ensure that a text file is not empty beforeattempting to read it. scrīpt must be run on the localcomputer.
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.CloseElse Wscrīpt.Echo "The file is empty."End If
Creatingand Naming a Text File
Demonstration scrīpt that uses theFileSystemObject's GetTempName method to generate a file name, andthen creates a file by that name.
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")strPath = "C:\FSO"strFileName = objFSO.GetTempNamestrFullName = objFSO.BuildPath(strPath, strFileName)Set ōbjFile = objFSO.CreateTextFile(strFullName)objFile.CloseobjFSO.DeleteFile(strFullName)
Creating aText File
Demonstration scrīpt that creates a new,empty text file. scrīpt must be run on the localcomputer.
Set ōbjFSO = CreateObject("scrīpting.FileSystemObject")Set ōbjFile = objFSO.CreateTextFile("C:\FSO\scrīptLog.txt")
Generating aFile Name
Demonstration scrīpt that uses theFileSystemObject's GetTempName method to generate random filenames. 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 strTempFileNext
Reading a TextFile Character by Character
Demonstration scrīpt that uses theFileSystemObject to read a text file character-by-character, andindividually echo those characters to the screen. scrīpt must berun 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 strCharactersLoop
Reading a Text File into an Array
Demonstration scrīpt that uses the VBscrīptSplit command to read a line from a commas-separated values file,and then place the individual items in that line into anarray.
Const ForReading = 1Set ō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) NextLoop
Reading a Text File from the BottomUp
Demonstration scrīpt that uses theFileSystemObject to read a text file, and then to echo the textfile in inverse order (that is, beginning with the last line in thetext file and ending with the first line).
Dim arrFileLines()i = 0Set ō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 + 1LoopobjFile.CloseFor l = Ubound(arrFileLines) to LBound(arrFileLines) Step -1 Wscrīpt.Echo arrFileLines(l)Next
Writing Datato a Text File
Demonstration scrīpt that retrieves thestatus for all the services installed on a computer, and then savesthe service name and status to a text file.
Const ForAppending = 8Set ō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)NextobjTextFile.Close
- [转]Scripts to manage Text Files
- HDOJ 1062 Text Reverse
- HDOJ 1321 Reverse Text
- Manage Database Projects With Vi…
-  
-
- About Trace Files
- 【转】sublime text 3 注册码
- sublime text 3 破解
- Sublime Text 2 快键健
- [转] 彼尔…
- POJ 1050 To the Max
- Android tips ( to be updated)
- Geoscope - map IP to location
- 解决android sdk manage…
- 1062 Text Reverse
- sublime text 3
- How to get a good idea to perfor…
- QTP打开网址和文件的语句
- 描述编程中的Description对象
- 分享VBS技术Sharing VBScripting T…
- VS2008 无法在web服务器上启动调试 基础连接已经关闭 发送时发生错误
- linux下查看最消耗CPU、内存的进程
- [转]Scripts to manage Text Files
- 如何解决返回数据库查询纪录条数的…
- QTP在EXCEL里面添加超联接的方法
- QTP Action级别的参数调用
- [转]QTP处理定时运行程序的办法
- qtp 使用Oracle数据库做测试数据驱…
- QTP中weblist检查和随机选取weblis…
- 关于java数据类型的转换
- QTP 学习笔记