Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to iterate through files in VB Script, opening each one for reading

Posted on 2008-10-02
3
Medium Priority
?
1,398 Views
Last Modified: 2008-10-03
At Line 18 I read in all of the files in a directory into a collection called 'FileNames'  I then try to iterate through all F in FileNames.  I get an error on Line 25 because "F does not support .AtEndOfStream.  I think I'm just slightly off syntactically, but how can I iterate through this collection of files, opening each one for reading as I go?


dim FileSys
 
DirName = "C:\BackupVerify\" 
 
Set FileSys = CreateObject("Scripting.FileSystemObject")
 
'if the C:\BackupVerify directory does not exist, create it
If Not FileSys.FolderExists(DirName) Then
Set CreatedDir = FileSys.CreateFolder(DirName)
End If
 
'------------------------------------------------------------
 
'iterate through all files in the C:\BackupVerify directory
 
Set FileRetriever = FileSys.GetFolder(DirName)
Set FileNames = FileRetriever.Files
 
For Each F in FileNames
 
Dim FileContents()
i = 0
 
'read the contents of each file into an array
Do Until F.AtEndOfStream
Redim Preserve FileContents(i)
FileContents(i) = F.ReadLine
i = i + 1
Loop
F.Close
 
'look at the date from the file
Set PrevDate = FileContents(0)
 
'look at the current date
Set CurrDate = Date()
 
'calcualte the difference between the two dates in number of days
Set Date_Diff = DateDiff("d" , CurrDate , PrevDate )
 
'if the backup test file is older than 180 days, delete it
If Date_Diff > 180 Then
	FileSys.DeleteFile(DirName & "BackupVerify" & PrevDate & ".txt")
	End If
 
Next 
 
'-----------------------------------------------------------
 
DateStamp = FormatDateTime(Date(),1)
TimeStamp = Time()
 
FileName = DirName & "BackupVerify" & DateStamp & ".txt"
WScript.echo filename
Set FileWriter = FileSys.CreateTextFile(FileName, True)
 
FileWriter.WriteLine(DateStamp)
FileWriter.WriteLine(TimeStamp)
 
FileWriter.Close
 
'WRITE TO ERROR LOG IF FILE

Open in new window

0
Comment
Question by:jkovba
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 400 total points
ID: 22629114
You don't need that - it's for reading an input file...or if you're trying to open each file...
dim FileSys
 
DirName = "C:\BackupVerify\" 
 
Set FileSys = CreateObject("Scripting.FileSystemObject")
 
'if the C:\BackupVerify directory does not exist, create it
If Not FileSys.FolderExists(DirName) Then
Set CreatedDir = FileSys.CreateFolder(DirName)
End If
 
'------------------------------------------------------------
 
'iterate through all files in the C:\BackupVerify directory
 
Set FileRetriever = FileSys.GetFolder(DirName)
Set FileNames = FileRetriever.Files
 
For Each F in FileNames
 
Dim FileContents()
i = 0
 
'read the contents of each file into an array
Dim objFile : Set objFile = FileSys.OpenTextFile(F)
Do Until objFile.AtEndOfStream
  Redim Preserve FileContents(i)
  FileContents(i) = F.ReadLine
  i = i + 1
Loop
objFile.Close
 
'look at the date from the file
Set PrevDate = FileContents(0)
 
'look at the current date
Set CurrDate = Date()
 
'calcualte the difference between the two dates in number of days
Set Date_Diff = DateDiff("d" , CurrDate , PrevDate )
 
'if the backup test file is older than 180 days, delete it
If Date_Diff > 180 Then
        FileSys.DeleteFile(DirName & "BackupVerify" & PrevDate & ".txt")
        End If
 
Next 
 
'-----------------------------------------------------------
 
DateStamp = FormatDateTime(Date(),1)
TimeStamp = Time()
 
FileName = DirName & "BackupVerify" & DateStamp & ".txt"
WScript.echo filename
Set FileWriter = FileSys.CreateTextFile(FileName, True)
 
FileWriter.WriteLine(DateStamp)
FileWriter.WriteLine(TimeStamp)
 
FileWriter.Close
 
'WRITE TO ERROR LOG IF FILE

Open in new window

0
 
LVL 9

Expert Comment

by:gregcmcse
ID: 22636046
The only addition I'd make to SirBounty's code is a much easier way of handling the array (and should execute MUCH faster).  Use the split function to create an array with each line in its own element in one shot as below.  REDIM PRESERVE is horrifically expensive in terms of execution cycles.

Note:  You MAY get an error on the split due to line 21.  If you do, change the line:

Dim FileContents()
  to
Dim FileContents

or just delete it entirely.
strTemp = ""
Do Until objFile.AtEndOfStream
  strTemp = F.ReadAll
Loop
FileContents = Split(strTemp,vbCrLf)
 
  

Open in new window

0
 

Author Comment

by:jkovba
ID: 22637927
Thank you SirBounty for the precise solution, and thanks also to gregmcse for a great suggestion to improve performance.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question