[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 432
  • Last Modified:

How to close a text file with VBS

Seems pretty simple, but this is blowing up on me.  I have a text file that I want to operate on a line at a time.  So for starters just opening the file, getting the number of lines, then overrighting each line, then I want to close the file.  I get error messages that the system doesn't like the .Close statement. I have taken out the writing, just want to open, read, swap each line, then close.  I have done a lot of VBS coding, but nothing with files before.
Code attached.  VBS-Sample.vbs
0
Mike Caldwell
Asked:
Mike Caldwell
  • 7
  • 5
  • 2
  • +1
1 Solution
 
Martin LissRetired ProgrammerCommented:
Just use Close by itself.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
"Variable is undefined."

OS = WIN7-64
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Posted code might be more convenient than a file.
Option Explicit
 
Dim objFSO, strTextFile, strData, strLine, arrLines, nLines, n
CONST ForWriting = 2
CONST ForReading = 1
 
'name of the text file
 strTextFile = "D:\Junk\StuffToReadAndWRite.txt"
 
'Create a File System Object
 Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Open the text file - strData now contains the whole file
 strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll
 
'Split the text file into lines
 arrLines = Split(strData,vbCrLf)


 
'Count the number of lines
 nLines = 0
 For Each strLine in arrLines
 nLInes = nLines + 1
 Next
 
For n = 0  to 5			' Just to test the code
arrLines(n) = "Substituted"
Next

strData.close		' Now just close the file;worry about writing later



'Cleanup
 Set objFSO = Nothing

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
HainKurtSr. System AnalystCommented:
here how you should use:


strData = objFSO.OpenTextFile(strTextFile,ForReading).ReadAll

-->

Dim MyFile
Set MyFile = objFSO.OpenTextFile(strTextFile,ForReading)
strData = MyFile.ReadAll
....
MyFile.Close


http://msdn.microsoft.com/en-us/library/314cz14s(v=vs.85).aspx
0
 
HainKurtSr. System AnalystCommented:
also have a look at this link to get how to use ReadAll

http://msdn.microsoft.com/en-us/library/t58aa4dd(v=VS.85).aspx
0
 
Bill PrewCommented:
This should be closer to what you need.

Option Explicit
 
CONST ForWriting = 2
CONST ForReading = 1
 
Dim objFSO, strTextFile, strData, strLine, arrLines, n, objFile
 
'name of the text file
strTextFile = "D:\Junk\StuffToReadAndWRite.txt"
 
'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Open the text file - strData now contains the whole file
objFile = objFSO.OpenTextFile(strTextFile,ForReading)
strData = objFile.ReadAll
objFile.close

'Split the text file into lines
arrLines = Split(strData,vbCrLf)
 
'Count the number of lines
' IF YOU NEED A COUNT OF THE ARRAY ELEMENTS USE UBound(arrLines), IT WILL 
' RETURN THE HIGHEST INDEX USED (KEEP IN MIND THE FIRST INDEX IS ZERO)
 
For n = 0  to 5			' Just to test the code
   arrLines(n) = "Substituted"
Next

'Cleanup
Set objFile = Nothing
Set objFSO = Nothing

Open in new window

~bp
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
"Object doesn't support this property or method."

This is popping at the line
strData=myFile.Readall
0
 
Bill PrewCommented:
You might also want to look at this solution for a sort of template that I often use for these types of edit a file line by line jobs.

http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_27225198.html#a36267234

~bp
0
 
Bill PrewCommented:
One correction to my code posted, I forgot a SET.

Option Explicit
 
CONST ForWriting = 2
CONST ForReading = 1
 
Dim objFSO, strTextFile, strData, strLine, arrLines, n, objFile
 
'name of the text file
strTextFile = "D:\Junk\StuffToReadAndWRite.txt"
 
'Create a File System Object
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
'Open the text file - strData now contains the whole file
Set objFile = objFSO.OpenTextFile(strTextFile,ForReading)
strData = objFile.ReadAll
objFile.close

'Split the text file into lines
arrLines = Split(strData,vbCrLf)
 
'Count the number of lines
' IF YOU NEED A COUNT OF THE ARRAY ELEMENTS USE UBound(arrLines), IT WILL 
' RETURN THE HIGHEST INDEX USED (KEEP IN MIND THE FIRST INDEX IS ZERO)
 
For n = 0  to 5                 ' Just to test the code
   arrLines(n) = "Substituted"
Next

'Cleanup
Set objFile = Nothing
Set objFSO = Nothing

Open in new window

~bp
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
"Object doesn't support this property or method" at:

objFile = objFSO.OpenTextFile(strTextFile,ForReading)

This makes no sense.  I keep running into error messages for procedures that came right from a book, on line posting, or you fellows here.  Is 64-bit WIN7 VBS different than standard?  BP: did you run this code at your end and it runs?
0
 
Bill PrewCommented:
I did run it and got no errors.

How are you running it, and did you save it as a VBS extension file?

~bp
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Saved as VBS, then either double click or click and "Open".
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
I did a copy and paste, then put in the real file name.
0
 
Mike CaldwellDirector of Business Development, AnaquaAuthor Commented:
Fast response, works great.  Thanks.
0
 
Bill PrewCommented:
Great, glad that helped, thanks for the feedback.

~bp
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

  • 7
  • 5
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now