Write all values from a listbox or file listbox to a text file

Status
Not open for further replies.

gecko_au2003

Banned
Messages
50
I have managed to retrieve all files from a directory and insert them into a file list box or a listbox but I am having problems writing each song title to a text file, any visual basic code examples would be very much appreciated !!

Thank you
 
Dim fso, fldTemp, LogFile, strFile, fObj
'Create a fs scripting object to hold the temp file
Set fso = CreateObject("Scripting.FileSystemObject")
strFile = "c:\outfile.txt"

If Dir(strFile) = "" Then
Set LogFile = fso.CreateTextFile(strFile, False)
Else
Set fObj = fso.GetFile(strFile)
Set LogFile = fObj.OpenAsTextStream(8) 'ForAppending)
Set fObj = Nothing
End If

LogFile.writeline TextToWrite

LogFile.Close


'dealloc objs
Set LogFile = Nothing
Set fldTemp = Nothing
Set fso = Nothing
 
The code I posted will write the vaule of the variable "TextToWrite" to the file identified by the variable strFile.

You can put it behind a button or in a module as a sub and call that sub from what ever event inits the writing of the data to the file. Be sure to set the TextToWrite to the data you want written out and strFile to the path and name of the file (e.g., c:\myDir\MyFile.txt )
 
Status
Not open for further replies.
Back
Top Bottom