Link to home
Start Free TrialLog in
Avatar of bandu
bandu

asked on

How to write a string to a text file

Hello,

I need to take a variable and store it in a text file for retrieval after a reboot of a process. Below is a snip of the code I am using and the trouble is when it should write to the file I get an error code 54 Bad File Mode.

When I step thru the UstrProduct_ID shows the correct value, it just wont write it to the file that was created.


Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.CreateTextFile("c:\catalog.txt", True)


Shell ("\\6183_CD_Server\test\initailcopy.exe"), vbMaximizedFocus


Repeat:
strProduct_ID = InputBox("Please Enter The Bundle System Catalog String. Use UPPER CASE Letters Only.", "6183 Install Process", "6183-")
UstrProduct_ID = UCase(strProduct_ID)

Set TextStream = FileSystemObject.OpenTextFile("c:\catalog.txt")
TextStream.Writeline (UstrProduct_ID)
TextStream.Close
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Try this additional Parameter:

Set TextStream = FileSystemObject.OpenTextFile("c:\catalog.txt", IOMode.ForAppending)

The default is ForReading, thus you cannot write to the file
Avatar of bandu
bandu

ASKER

Ok, I added the IOMode and now I get an error 424 Object Required when it passes the above line
Set TextStream = FileSystemObject.OpenTextFile("c:\catalog.txt", IOMode.ForAppending)
try this

public Sub WriteToCatalog(pzText as string)
open "c:\catalog.txt" for append as #1
write #1, pzText
close #1
hi bandu !!!
the following code is not using filesystem objects but the following two functions can be successfully used for writing and reading from the txt file. hope this would solve your purpose.

cheers!!!!!!!!!!
Amitabh

'To Write in the file
'=====================
Public sub generate_txtfile()
On Error GoTo fl_err:
Dim p_fileno As Integer
Dim p_str As String,p_filepath as string

p_str = "writing text to the file which will be read later"
p_filepath= "c:\bandu\nameofthefile.txt"

p_fileno = FreeFile()
Open p_filepath For Output As #p_fileno 'creating the file
Print #p_fileno, p_str  'this will write the string in file
Close #p_fileno
Exit Sub
fl_err:
MsgBox Err.Description, , "ERROR"
End sub

'To read from the file
'======================
Public sub read_txtfile()
On Error GoTo readfl_err:
Dim p_fileno As Integer
Dim p_str As String, p_filepath As String

p_filepath = "c:\winnt\profiles\dlsyfsap\desktop\nameofthefile.txt"

p_fileno = FreeFile()
Open p_filepath For Input As #p_fileno 'opening the file
Line Input #p_fileno, p_str  'this will read the line in variable
Close #p_fileno
MsgBox CStr(p_str)
Exit Sub
readfl_err:
MsgBox Err.Description, , "ERROR"
End sub
Avatar of bandu

ASKER

McBeth,

I tried your method and it writes with no trouble. The only problem that remains is that the output to the file is "6183-ZZZZZZZZZZZ" and I need the "" removed so that when I read it back it is only the actual string. I guess I could read back only the characters minus the " but I want this as clean as I can get it.
bandu,
yes there are "" in the textfile, but if you read out
the string the quotes arn't read out :

public function GetFromCatalog() as string
Dim pzText As String
Open "C:\catalog.txt" For Input As #1
Input #1, pzText
Close #1
GetFromCatalog= pzText
exit function
replace write by print...
thanks angelIII that's the point i forgot...
ASKER CERTIFIED SOLUTION
Avatar of Ark
Ark
Flag of Russian Federation image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bandu

ASKER

This solved the trouble and works great. I must note that while the other answers worked, I wanted to stay with FileSystemStream because of other requirements in my program.
Thanks