Reading Text Files

Microsoft® Windows® 2000 Scripting Guide

Method

Description

Read

Reads the specified number of characters and then stops. For example, the following command would read the first 12 characters of the first line ("Alerter,Shar") into the variable strText and then stop:strText = objTextFile.Read(12)

ReadLine

Reads one line from a text file and then stops before reaching the newline character. For example, the following command would read the first line ("Alerter,Share Process,Running,Auto,LocalSystem") into the variable strText and then stop:strText = objTextFile.ReadLine

To read an entire text file on a line-by-line basis, place the ReadLine function within a loop.

ReadAll

Reads the entire contents of a text file into a variable.

Skip

Skips the specified number of characters and then stops. For example, the following command would skip the first 12 characters. Any subsequent read operations would begin with the 13th character and continue from there ("e Process,Running,Auto,LocalSystem"):objTextFile.Skip(12)

SkipLine

Skips an entire line in a text file. For example, this code reads the first and third lines of a text file, skipping the second line:strText = objTextFile.Readline objTextFile.SkipLine strText = objTextFile.Readline

Listing 4.37 Verifying the Size of a File

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("C:\Windows\Netlogon.log")
If objFile.Size > 0 Then
 Set objReadFile = objFSO.OpenTextFile("C:\Windows\Netlogon.log", 1)
 strContents = objReadFile.ReadAll
 Wscript.Echo strContents
 objReadFile.Close
Else
 Wscript.Echo "The file is empty." 

End If


Listing 4.38 Reading a Text File Using the ReadAll Method

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", ForReading)
strContents = objFile.ReadAll
Wscript.Echo strContents
objFile.Close


Listing 4.39 Reading a Text File Using the ReadLine Method

Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\FSO\ServerList.txt", 1)
Do Until objFile.AtEndOfStream
 strLine = objFile.ReadLine
 Wscript.Echo strLine
Loop
objFile.Close


Listing 4.40 "Reading" a Text File from the Bottom to the Top

Dim arrFileLines()

i = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.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
 Wscript.Echo arrFileLines(l)
Next


6/19/2002   Success
6/20/2002   Failure
6/21/2002   Failure
6/22/2002   Failure
6/23/2002   Success


6/23/2002 Success

6/22/2002   Failure
6/21/2002   Failure
6/20/2002   Failure
6/19/2002   Success



Listing 4.41 Reading a Fixed-Width Text File


Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1)
Do Until objFile.AtEndOfStream
 objFile.Skip(5)
 strCharacters = objFile.Read(1)
 Wscript.Echo strCharacters
 objFile.SkipLine
Loop


C:\FSO\ScriptLog.txt
XXXXX1XXXXXXXXXXXXXX
XXXXX2XXXXXXXXXXXXXXXXXXX
XXXXX3XXXXXXXXXXXX
XXXXX4XXXXXXXXXXXXXXXXXXXXXXXXX


1

2

3

4



Posted by n3015m
:
BLOG main image
'네오이즘'의 보안LAB 블로그입니다........... n3oism@gmail.com by n3015m

카테고리

분류 전체보기 (228)
[ HappyDevTool ] (29)
[ HappyToolRelease ] (4)
[Book] (6)
[ Security Studies ] (0)
- CII (2)
- BigData (2)
- Web Hacking (10)
- SQL Injection (25)
- Mobile Security (9)
- Network (6)
- OperatingSystem (4)
- Malware & Reversing (4)
- Phishing (5)
- Compliance (0)
- Programming (13)
- Tools (13)
- IoT (6)
- etc (21)
[Pentration Testing] (3)
[OS X] (4)
[ Security Trends ] (16)
[ Fixing Guideline ] (7)
My Way, My Life (34)
About Me (2)

최근에 올라온 글

최근에 달린 댓글

최근에 받은 트랙백

Total :
Today : Yesterday :