I am having issues using GetFolder and OpenTextFile

ploaiza
ploaiza used Ask the Experts™
on
I have the following code:
Set oFolders = CreateObject("Scripting.FileSystemObject")
Set oRootFolder = oFolders.GetFolder(sBaanServerPath)    
For Each oFile in oRootFolder.Files    
i = 0
Set oReadFile = CreateObject("Scripting.FileSystemObject")
Set oInputFile = oReadFile.OpenTextFile(oFile.path, ForReading)
   Do Until oInputFile.AtEndOfStream
     aLines(i).ReadLine
     i = i + 1
   Loop
Next

But it is failing when I do the OpenTextFile. Does anyone know why? Is it now allowed to use GetFolders and OpenTextFile in this way?

Thanks for the help
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2012
Commented:
Try this:

Set oFS = CreateObject("Scripting.FileSystemObject")
Set oRootFolder = oFS.GetFolder(sBaanServerPath)    
For Each oFile in oRootFolder.Files    
     i = 0
     Set oInputFile = oFS.OpenTextFile(oFile.path, ForReading)
     Do Until oInputFile.AtEndOfStream
          aLines(i).ReadLine
          i = i + 1
     Loop
     oInputFile.Close
Next

Otherwise, post what error you get.

Anthony

Commented:
Give this a try:

Dim sBaanServerPath As String
sBaanServerPath = "c:\"

Dim oFolders, oRootFolder, oFile, oReadFile, oInputFile, i, aLines()
Const ForReading = 1

Set oFolders = CreateObject("Scripting.FileSystemObject")
Set oRootFolder = oFolders.GetFolder(sBaanServerPath)
For Each oFile In oRootFolder.Files
i = 0
Set oReadFile = CreateObject("Scripting.FileSystemObject")
Set oInputFile = oReadFile.OpenTextFile(oFile.Path, ForReading)
  Do Until oInputFile.AtEndOfStream
    ReDim Preserve aLines(i + 1)
    aLines(i) = oInputFile.ReadLine
    i = i + 1
  Loop
Next

As you can see I just set the root folder to my c:\ drive for testing purposes.

Author

Commented:
acperkins, I tried removing the extra CreateObject and it worked. Thank you.

Author

Commented:
Actually I was looking at the wrong Event log. It is still not working. It is a VB script and so I am debugging it by placing some Event logs after each line, and it just does not execute:
Set oInputFile = oFS.OpenTextFile(oFile.path, ForReading)

Everything has been declared and I know that it is pulling the right path. Do you have any other idea of what could be making the script stop there?

Author

Commented:
I found the error. It was with using "ForReading". I decided to use just the number 1 instead of the text and it worked this time.

Thanks all

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial